Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Creational Patterns
Factory Method
Intent:
Want to hide the details of sub-class specific
implementations from Client
A simplified Abstract Factory where families of related
objects are not relevant
Two kinds:
Polymorphic (by inheritance)
We already saw this in the Abstract Factory
Builder Metaphor
UML Structure
Collaborations
Want to separate
database interactions (Director)
from the specific formats (Builder)
Factory Method
hide the details of sub-class specific implementations
from Client
inheritance or discriminator
Builder
Multi-step object creation
Separate the construction steps from the low-level
representation of the constructed object
Structural Patterns
Adapter (last class)
Composite (last class and review from 210)
Decorator (today and may be review from
210)
Proxy
1
7
composes concrete
decorators
Client
Participants
Component
the interface of a core object
Concrete component
an implementation of the component interface
Decorator
abstract class which wraps another Component
Concrete Decorator
an implementation of Decorator which adds new
behavior before/after core behavior
through delegation
Sample problem
You need to implement a point-of-sale system for
a coffee shop. The coffee shop has some basic
beverages, but customers can customize their
drinks by choosing what kind of milk they want, if
they want flavoured syrup, etc.
You could create a class for each drink, but there
are so many possible combinations that the
number of classes would quickly get out of hand.
22
23
24
Definition: Delegation
Class S which implements/extends type T
has a field x of type T
Common Uses
Transformers
Encode/decode
Compress/decompress
Filtering
Interceptors
Logging
Alert monitoring
Security checking
Concrete component
Standard output/Standard input
Decorator
wraps a Console and delegates to it
Concrete Decorator
convert to upper case
time stamped logging
etc
Decorator activity
Draw a UML class diagram for the system on the following
slide. Use the Decorator Pattern for some classes (perhaps as
part of a pizza delivery app) .
Once youre finished drawing the UML diagram, write the
Java code to implement the system.
29
Decorator activity
1. interface PizzaOrder {
boolean isVegan(); //true if it has no cheese or meat
boolean hasMeat();
double price(); //dollars
} //Dont need a hasCheese method
2. BasicMediumPizza
A regular pizza, nothing special, costs $10.00
This pizza includes cheese by default (in real life), no need to additionally specify
cheese (in the code)
3. NoCheese: Modifies a pizza so no cheese is used at no additional charge
4. Pepperoni : Modifies a pizza so pepperoni is added, for $1.00
5. Client : Creates a basic medium pizza, no cheese, with pepperoni. Checks the price
and checks if it is vegan.
6. Do you need any other types?
30