Created
February 3, 2019 10:40
-
-
Save binary10ve/a6e6eeeb36cb2f5a3388d7110d42fad5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GoogleBackUp | |
def backup | |
puts("Back Up with Google") | |
super | |
end | |
end | |
module BoxBackUp | |
def backup | |
puts("Back up with BOX ") | |
super | |
end | |
end | |
class Phone | |
def backup | |
puts("Default backup stragegy") | |
end | |
end | |
one_plus = Phone.new | |
one_plus.backup() | |
one_plus.extend(GoogleBackUp) | |
one_plus.backup() | |
one_plus.extend(BoxBackUp) | |
one_plus.backup() | |
module GoogleBackUp | |
def backup | |
puts("Back Up with Google") | |
super | |
end | |
end | |
module BoxBackUp | |
def backup | |
puts("Back up with BOX ") | |
super | |
end | |
end | |
class Phone | |
def backup | |
puts("Default backup stragegy") | |
end | |
end | |
one_plus = Phone.new | |
one_plus.backup() | |
one_plus.extend(GoogleBackUp) | |
one_plus.backup() | |
one_plus.extend(BoxBackUp) | |
one_plus.backup() | |
=begin | |
Advantages: | |
The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime. | |
The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original class; decorating can provide new behavior at runtime for individual objects. | |
Decorator offers a pay-as-you-go approach to adding responsibilities. Instead of trying to support all foreseeable features in a complex, customizable class, you can define a simple class and add functionality incrementally with Decorator objects. | |
Disadvantages: | |
Decorators can complicate the process of instantiating the component because you not only have to instantiate the component, but wrap it in a number of decorators. | |
It can be complicated to have decorators keep track of other decorators, because to look back into multiple layers of the decorator chain starts to push the decorator pattern beyond its true intent. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment