diff --git a/Homeworks/Factory_Design_Pattern/factory.jpg b/Homeworks/Factory_Design_Pattern/factory.jpg new file mode 100644 index 0000000..c69c397 Binary files /dev/null and b/Homeworks/Factory_Design_Pattern/factory.jpg differ diff --git a/Homeworks/Factory_Design_Pattern/factory_expample.rb b/Homeworks/Factory_Design_Pattern/factory_expample.rb new file mode 100644 index 0000000..cc6ef32 --- /dev/null +++ b/Homeworks/Factory_Design_Pattern/factory_expample.rb @@ -0,0 +1,45 @@ +class Shape + def draw + raise 'must implement draw() method in subclass' + end +end + +class Rectangle < Shape + def draw + puts 'Inside Rectangle::draw() method.' + end +end + +class Square < Shape + def draw + puts 'Inside Square::draw() method.' + end +end + +class Circle < Shape + def draw + puts 'Inside Circle::draw() method.' + end +end + +class ShapeFactory + def get_shape(type) + case type + when 'Circle' then Circle.new + when 'Rectangle' then Rectangle.new + when 'Square' then Square.new + end + end +end + +shape_factory = ShapeFactory.new + +circle = shape_factory.get_shape('Circle') +circle.draw + +rectangle = shape_factory.get_shape('Rectangle') +rectangle.draw + +square = shape_factory.get_shape('Square') +square.draw +#Chi can su dung doi tuong cua lop ShapeFactory de tao cac Shape ma khong can chi ro cu the Shape nao se duoc tao diff --git a/Homeworks/Structural_patterns/Adapter_expample.rb b/Homeworks/Structural_patterns/Adapter_expample.rb new file mode 100644 index 0000000..3a2c91e --- /dev/null +++ b/Homeworks/Structural_patterns/Adapter_expample.rb @@ -0,0 +1,52 @@ +class Meat + attr_accessor :name, :protein, :weight + def initialize name, protein, weight + @name = name + @weight = weight + @protein = protein + end + + def take_calo + @weight * @protein *4 / 100 + end +end + +class Human + attr_accessor :energy + def initialize + @energy = 100 + end + + def eat meat + @energy += meat.take_calo + end +end + +class Cow + attr_accessor :weight + + def initialize weight + @weight = weight + end +end + +class MeatAdapter + attr_accessor :protein, :weight + + def initialize cow + @cow = cow + @weight = cow.weight + @protein = 4 + end + + def take_calo + @weight * @protein + end +end + +human = Human.new +meat = Meat.new "pig", 16, 100 +human.eat meat +cow = Cow.new 1000 +meat_cow = MeatAdapter.new cow +human.eat meat_cow diff --git a/Homeworks/Structural_patterns/adapter.png b/Homeworks/Structural_patterns/adapter.png new file mode 100644 index 0000000..c0970fd Binary files /dev/null and b/Homeworks/Structural_patterns/adapter.png differ diff --git a/Homeworks/behavior_design_patterns/Template.png b/Homeworks/behavior_design_patterns/Template.png new file mode 100644 index 0000000..87c6997 Binary files /dev/null and b/Homeworks/behavior_design_patterns/Template.png differ diff --git a/Homeworks/behavior_design_patterns/template.rb b/Homeworks/behavior_design_patterns/template.rb new file mode 100644 index 0000000..a661282 --- /dev/null +++ b/Homeworks/behavior_design_patterns/template.rb @@ -0,0 +1,93 @@ +class ReportTemplate + def initialize + @title = 'Monthly Report' + @text = ['Sample text 1', 'Sample text 2', 'Sample text 3'] + end + + def output_report + output_start + output_head + output_body_start + @text.each { |line| output_line(line) } + output_body_end + output_end + end + + def output_start + end + + def output_head + output_line(@title) + end + + def output_body_start + end + + def output_line(_line) + raise 'Called abstract method: output_line' + end + + def output_body_end + end + + def output_end + end +end + +class PlainTextReport < ReportTemplate + def output_start + end + + def output_head + puts "**** #{@title} ****" + puts + end + + def output_body_start + end + + def output_line(line) + puts line + end + + def output_body_end + end + + def output_end + end +end + +class HTMLReport < ReportTemplate + def output_start + puts '' + end + + def output_head + puts '
' + puts "#{line}
" + end + + def output_body_end + puts '' + end + + def output_end + puts '' + end +end + + +report = HTMLReport.new +report.output_report + +report = PlainTextReport.new +report.output_report