Upload
gavin-price
View
214
Download
0
Tags:
Embed Size (px)
Citation preview
DESIGN PATTERNS
COMMONLY USED PATTERNS
What is a design pattern ?
Defining certain rules to tackle a particular kind of problem in software development environment.
A pattern addresses a recurring design problem that arises in a specific design
situation and presents a solution to it.
Types Of Design Patterns
The Gang Of Four Design PatternsThe EJB Design PatternsThe Sun J2EE Design patternsThe Application Design patterns
Elements of a Design Pattern
NameProblemSolutionConsequences
Classification
Creational Patterns – are ones that create objects for you,rather than having you instantiate objects directly
Structural Patterns – help you compose groups of objects into larger structures.
Behavioral Patterns – help you define the communication between objects in your system and how the flow is controlled in a complex program.
GoF Design Patterns
Key Rules
1. Program to an interface and not to an implementation
2. Favor Object Composition over Inheritance.
Creational Patterns
FactoryAbstract FactoryBuilderPrototypeSingleton
Factory
Returns an instance of one of several possible classes depending on the data provided to it.
Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of
data.
Abstract Factory
Abstraction over the Factory Design Pattern
Abstract factory is the factory that returns one of several factories.
One of the example to this could be a look-
and-feel in Java.
UML Diagram
Pros & Cons
Pros Cons1. Shields clients from
concrete classes.2. Easy to switch product
family at runtime – just change concrete factory
3. “keep it in the family” – enforces product family grouping
1. Adding a new product means changing factory interface + all concrete factories
Builder
Separates the construction process of a complex object from its representation.
Used when the construction process is same but the representations may differ.
UML Diagram
Example -- UMLRTFReader
ParseRTF()
while(t=get the next token){
switch t.Type{
CHAR: builder->ConvertCharacter(t.Char)FONT: builder->ConventFontCharnge(t.Font)PARA: Builder->ConventParagraph() }}
TextConverter
ConvertCharacter(char)
ConvertFontChange(Font)
ConvertParagraph()
ASCIIConverter
ConvertCharacter(char)
GetASCIIText()
TextConverter
ConvertCharacter(char)
ConvertFontChange(Font)
ConvertParagraph()
GetTeXText()
TextWidgestConverter
ConvertCharacter(char)
ConvertFontChange(Font)
ConvertParagraph()
GetTextWidget()
ASCIIText TeXText TextWidget
builders
How is it different from Abstract Factory ?
Builder Abstract FactoryIt focuses on constructing a complex object step-by-step
Returns a complex object as a final result
AF emphasizes on creating a family of related objects
Product gets returned immediately.
Prototype
Used when creation of an object is time consuming or very complex.In java you can use this by implementing Cloneable interface.
Pros. Cons.
1. Shields clients from concrete classes
2. The object is the factory - i.e. Product and Creator combined (saves coding a Creator for every Product)
3. Pre-configured object instances – instead of create/set member every time.
1. Requires Memory to hold prototype.
2. Clone() is hard to Implement.3. Many prototypes must be
passed.
Singleton
Ensure a class has only one instance and provide a global point of access to it.
Structural Patterns
Describes how classes and objects can be combined to make larger structures.
Class Patterns – Inheritance
Object Patterns – Composition
Structural Patterns
AdapterBridgeCompositeFaçadeProxyFlyweightDecorator
Adaptor Pattern
Converting an interface into another interface that its client is expected to see.
Two flavor of this pattern come as: Class Adaptor Object Adaptor
Java Adaptors
Class Adaptor
Object Adaptor
Bridge Pattern
Separates Interface from its implementation so that both can be changed independently.
UML Diagram
Composite Pattern
Allows clients to treat both single components and collection of components identically.
Represents recursive data structures.
Compose objects into tree structures to represent part-whole hierarchies.
UML Diagram
Façade Pattern
Defines a high level interface that makes a sub system easier to use.
Does not prevent an advanced user to use low level functionality.
J2EE – Session Façade & Message Façade
UML Diagram
Proxy
Provide a surrogate or placeholder for another object to control access to it.
Provides identical interface as the original object has.
Controls access to the original object and may be responsible for creating & destroying it
UML Diagram
Decorator
Adding responsibilities to objects dynamically and without having to create a new class.
Also known as “Wrapper”
Used when sub-classing is impractical.
Java I/O Streams is a good example.
UML Diagram
Flyweight
Use sharing to support large numbers of fine-grained objects efficiently.
Reusability of existing objects.
Flyweight objects have two states intrinsic state & extrinsic state, that make them reusable.
UML Diagram
Examplepublic class StringTest {
public static void main(String[] args) { String fly = "fly", weight = "weight"; String fly2 = "fly", weight2 = "weight";
System.out.println(fly == fly2); System.out.println(weight == weight2);
String distinctString = fly + weight; System.out.println(distinctString == "flyweight");
String flyweight = (fly + weight).intern(); System.out.println(flyweight == distinctString ); }}
// TRUE // TRUE
// FALSE
// TRUE
Strategy Pattern
an object and its behavior are separated and put into two different classes.
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
UML Diagram
Thank You !