Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Homeworks/Factory_Design_Pattern/factory.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions Homeworks/Factory_Design_Pattern/factory_expample.rb
Original file line number Diff line number Diff line change
@@ -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
52 changes: 52 additions & 0 deletions Homeworks/Structural_patterns/Adapter_expample.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file added Homeworks/Structural_patterns/adapter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Homeworks/behavior_design_patterns/Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions Homeworks/behavior_design_patterns/template.rb
Original file line number Diff line number Diff line change
@@ -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 '<html>'
end

def output_head
puts ' <head>'
puts " <title>#{@title}</title>"
puts ' </head>'
end

def output_body_start
puts '<body>'
end

def output_line(line)
puts " <p>#{line}</p>"
end

def output_body_end
puts '</body>'
end

def output_end
puts '</html>'
end
end


report = HTMLReport.new
report.output_report

report = PlainTextReport.new
report.output_report