diff --git a/Homeworks/behavioral_design_patterns/area.rb b/Homeworks/behavioral_design_patterns/area.rb new file mode 100644 index 0000000..08b26ad --- /dev/null +++ b/Homeworks/behavioral_design_patterns/area.rb @@ -0,0 +1,43 @@ +# Template method Design Pattern +# https://cacoo.com/diagrams/fnlz5bSWSRPtuoAF + +class Area + def initialize *args ; end + + def calculate ; end + + def area + calculate() + end +end + +class RectangularArea < Area + def initalize(longitude, width) + @longitude = longitude + @width = width + end + + def calculate + (@longitude + @width) * 2 + end +end + +def SquareArea < Area + def initialize(edge) + @edge = edge + end + + def calculate + (@edge * 4) + end +end + +def CircleArea < Area + def initialize(radius) + @radius = radius + end + + def calculate + (@radius * @radius * 2) + end +end diff --git a/Homeworks/behavioral_design_patterns/stratetry_pattern.PNG b/Homeworks/behavioral_design_patterns/stratetry_pattern.PNG new file mode 100644 index 0000000..17c9a73 Binary files /dev/null and b/Homeworks/behavioral_design_patterns/stratetry_pattern.PNG differ diff --git a/Homeworks/behavioral_design_patterns/template_method_pattern.PNG b/Homeworks/behavioral_design_patterns/template_method_pattern.PNG new file mode 100644 index 0000000..52b6a99 Binary files /dev/null and b/Homeworks/behavioral_design_patterns/template_method_pattern.PNG differ diff --git a/Homeworks/behavioral_design_patterns/transport_algorithm.rb b/Homeworks/behavioral_design_patterns/transport_algorithm.rb new file mode 100644 index 0000000..4ede800 --- /dev/null +++ b/Homeworks/behavioral_design_patterns/transport_algorithm.rb @@ -0,0 +1,27 @@ +# Strategy design pattern +# https://cacoo.com/diagrams/SYmlKFaYO7TY5ZiV +class TransportAlgorithm + def tranfer ;end +end + +def TransportationToAirportByTaxi + def initialize(TransportAlgorithm transport_algorithm) + @transport_algorithm = transport_algorithm + end + + def tranfer() + @transport_algorithm.tranfer() + end +end + +def TransportToAirportByBus < TransportAlgorithm + def tranfer + # something code + end +end + +class TransportationToAirport < TransportAlgorithm + def tranfer + # something code + end +end \ No newline at end of file diff --git a/Homeworks/creational_design_patterns/prototype_patterns.png b/Homeworks/creational_design_patterns/prototype_patterns.png new file mode 100644 index 0000000..19d3add Binary files /dev/null and b/Homeworks/creational_design_patterns/prototype_patterns.png differ diff --git a/Homeworks/creational_design_patterns/school_prototypes.rb b/Homeworks/creational_design_patterns/school_prototypes.rb new file mode 100644 index 0000000..2232061 --- /dev/null +++ b/Homeworks/creational_design_patterns/school_prototypes.rb @@ -0,0 +1,22 @@ +#Design Patterns: Prototype + +class SchoolPrototypes + def initialize *args; end + + def clone + #code something + end +end + + +class TeamPrototypes < SchoolPrototypes + def clone + #code something + end +end + +class GradePrototypes < SchoolPrototypes + def clone + #code something + end +end \ No newline at end of file diff --git a/Homeworks/structural_design_patterns/Pay .png b/Homeworks/structural_design_patterns/Pay .png new file mode 100644 index 0000000..0762365 Binary files /dev/null and b/Homeworks/structural_design_patterns/Pay .png differ diff --git a/Homeworks/structural_design_patterns/pay.rb b/Homeworks/structural_design_patterns/pay.rb new file mode 100644 index 0000000..f298288 --- /dev/null +++ b/Homeworks/structural_design_patterns/pay.rb @@ -0,0 +1,55 @@ +# Old code +module PayAPI + def pay() + # your code here + end +end + +class PayWithVietcomBank + include PayAPI + def start_pay() + # your code here + PayAPI.pay() + end +end + +class PayWithVietinBank + include PayAPI + def start_pay() + # your code here + PayAPI.pay() + end +end + +# Problem: If change name of pay method in PayAPI, you need change this name in all class use it +# Soluntion: Use adapter pattern +# Code refactored + +module PayAPI + def pay() + # your code here + end +end + +module CustomPayAPI + include PayAPI + def custom_pay() + PayAPI.pay() + end +end + +class PayWithVietcomBank + include CustomPayAPI + def start_pay() + # your code here + CustomPayAPI.custom_pay() + end +end + +class PayWithVietinBank + include CustomPayAPI + def start_pay() + # your code here + CustomPayAPI.custom_pay() + end +end \ No newline at end of file