Bài đăng

Đang hiển thị bài đăng từ Tháng 3, 2015

Template Method Pattern

You want to build a program to teach your children to know animals. You write class animal like that: class Animal def initalize ( name , age , species ) @name = name @age = age @species = species end def say if ( @species == "dog" ) puts "Gau...gau..gau..." end if ( @species == "cat" ) puts "meo...meo...meo" end end end After that, you want to add more infomation about animals in your program. It’s a nightmare if you want to add more and more species in your old code. Luckily, you can use template method patter to do it simply, easily. Like that class Animal def initialize ( name , age ) @name = name @age = age end def say raise 'Called say method in subclass' end end class Dog < Animal def say puts "Gau gau gau" end end class Cat < Animal def say puts "meo ..