COMP 121 Week 12 Decorator and Adapter Patterns

  • Slides: 16
Download presentation
COMP 121 Week 12: Decorator and Adapter Patterns

COMP 121 Week 12: Decorator and Adapter Patterns

Decorator Design Pattern n Intent ¨ Augment n objects with new responsibilities Use when:

Decorator Design Pattern n Intent ¨ Augment n objects with new responsibilities Use when: ¨ Extension by subclassing is impractical n Want to add responsibilities to individual objects, not an entire class ¨ Responsibilities n n can be withdrawn Decorator implements the same interface as the object it decorates A “shell” or “wrapper” around an object ¨ Requests that come in to the decorator are “forwarded” to the object it decorates

Key to Decorator Design Pattern n The key to a successful Decorator is to

Key to Decorator Design Pattern n The key to a successful Decorator is to have a set of objects defined by an interface Decorator implements the interface and takes an object of that interface type as a parameter of its constructor, which it saves internally When a method is called on the decorator ¨ May do some pre-processing before calling the same method on the decorated object ¨ May do some post-processing on the results returned from the same method in the decorated object

Benefits of Using the Decorator Pattern n More flexibility than inheritance ¨ Responsibilities can

Benefits of Using the Decorator Pattern n More flexibility than inheritance ¨ Responsibilities can be added or removed at runtime n Avoids feature-laded classes high up in the hierarchy ¨A “pay-as-you-go” approach to adding responsibilities Can define a simple class n Add functionality incrementally with decorators n

Decorator Example (Sunglasses) n Sunglasses ¨ People “decorate” themselves in many different ways n

Decorator Example (Sunglasses) n Sunglasses ¨ People “decorate” themselves in many different ways n Clothing, makeup, jewelry, glasses ¨ Sunglasses intercept incoming light and provide modified light to the eyes n The eyes still get basically the same type of input ¨ n A stream of light into the eyes Input (light) is transformed by the sunglasses but is still a form of light ¨ Not converted into sound or smell ¨ Eyes take in light whether or not the decorator (pair of sunglasses) is present n n This "transparent" property of Decorator is what allows Decorators to be chained together Person may wear contact lenses AND sunglasses

Decorator Example (Audio Enhancers) n Vocal filters ¨ Electronic devices or software can be

Decorator Example (Audio Enhancers) n Vocal filters ¨ Electronic devices or software can be used to enhance, morph, or disguise your voice n n Change vocal pitch and timbre or apply other effects such as volume or vibrato Used by singers and kidnappers ¨ Filter changes the audio stream n Input is an audio stream n Output is an audio stream ¨ Audio may be passed through a the sound qualities desired n series of filters to get Chain of decorators ¨ Used with musical instruments , too

Decorator Example (Java File I/O) n Input and Output Streams ¨ Basic I/O classes

Decorator Example (Java File I/O) n Input and Output Streams ¨ Basic I/O classes are Input. Stream, Output. Stream, Reader and Writer ¨ Need to add behaviors to the stream to get the desired results Buffered. Input. Stream adds buffering for performance n Data. Input. Stream adds input of primitive data types n File. Input. Stream in = new File. Input. Stream("test. dat"); Buffered. Input. Stream bin = new Buffered. Input. Stream(in); Data. Input. Stream dbin = new Data. Input. Stream(bin)

Question: n How does the Decorator design pattern differ from the Strategy design pattern?

Question: n How does the Decorator design pattern differ from the Strategy design pattern?

Answer: n The Decorator pattern changes the “skin” of an object ¨ The decorator

Answer: n The Decorator pattern changes the “skin” of an object ¨ The decorator is a wrapper or outer layer that changes the behavior of an object ¨ The object doesn’t know about its decorators n The Strategy pattern changes the “guts” of an object ¨ The strategy is changed by using a different strategy object that alters or extends the behavior of an object ¨ The object knows about the available strategies

Adapter Design Pattern n Intent Convert the interface of a class into another interface

Adapter Design Pattern n Intent Convert the interface of a class into another interface that the clients expect ¨ Lets classes work together that otherwise couldn’t because of incompatible interfaces ¨ n Use when: You want to use an existing class and its interface doesn’t match the one you need ¨ You want to create a reusable class that cooperates with unrelated or unforeseen classes (classes that don’t have compatible interfaces) ¨ n Another type of “shell” or “wrapper” around an object ¨ Requests that come in to the adapter are “forwarded” to the object being adapted, but the actual method being called is different

Adapter Example (Socket Wrench) n Socket Wrench ¨A socket attaches to a ratchet, provided

Adapter Example (Socket Wrench) n Socket Wrench ¨A socket attaches to a ratchet, provided that the size of the drive is the same ¨ Typical drive sizes in the United States are 1/2" and 1/4" A 1/2" drive ratchet will not fit into a 1/4" drive socket unless an adapter is used n A 1/2" to 1/4" adapter has a 1/2" female connection to fit on the 1/2" drive ratchet, and a 1/4" male connection to fit in the 1/4" drive socket n

Adapter Example (Travel Adapter Kits) In different parts of the world, different wattages and

Adapter Example (Travel Adapter Kits) In different parts of the world, different wattages and outlet types make it impossible to use personal appliances (shavers, hair dryers, etc. ) without modification n A travel adapter kit allows you to: n ¨ Plug your appliance into a converter to convert the wattage ¨ Plug the converter into the wall outlet by adapting the plug type

Adapter Example (Replacing 3 rd Party Software) n n The adapter pattern is commonly

Adapter Example (Replacing 3 rd Party Software) n n The adapter pattern is commonly used in applications where some third party software is being replaced with some other software Rather than rewrite the application to use the new software interface, use an adapter to: ¨ Adapt the old interface to the new interface n The application code calls the old interface, but is now making those requests via the adapter n The adapter converts the requests to requests that are compatible with the new software n The adapter may do some pre- or post-processing in order to adapt request

Question: n How does the Adapter design pattern differ from the Decorator design pattern?

Question: n How does the Adapter design pattern differ from the Decorator design pattern?

Answer: The Decorator pattern only changes the object’s responsibilities, not its interface n The

Answer: The Decorator pattern only changes the object’s responsibilities, not its interface n The Adapter pattern changes the object’s interface, often without changing its responsibilities n

Any Questions?

Any Questions?