Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | chandra-bhushan |
View: | 215 times |
Download: | 0 times |
of 25
8/14/2019 4050_Design_Pattern.ppt
1/25
List of Design Patterns
Creational Patterns
Abstract Factory
Builder
Facto ry Method
Prototype Singleton
Structural Patterns Adapter
Bridge
Composite (Sammys Slides)
Decorator (Sammys Slides)
Facade
Flyweight Proxy
Behavioral Patterns
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer State
Strategy
Template Method
Visitor
8/14/2019 4050_Design_Pattern.ppt
2/25
FACTORY METHOD
(Class Creational) Intent:
Define an interface for creating an object, but
let subclasses decide which class to
instantiate.
Factory Method lets a class defer instantiation
to subclasses.
8/14/2019 4050_Design_Pattern.ppt
3/25
FACTORY METHOD
(Class Creational) Motivation:
Framework use abstract classes to define and
maintain relationships between objects
Framework has to create objects as well -
must instantiate classes but only knows about
abstract classes - which it cannot instantiate
Factory method encapsulates knowledge ofwhich subclass to create - moves this
knowledge out of the framework
8/14/2019 4050_Design_Pattern.ppt
4/25
docsDocument
Open()
Close()
Save()
Revert()
Application
MyDocument
CreateDocument()
NewDocument()
OpenDocument()
MyApplication
CreateDocument()
Document* doc=CreateDocument();
docs.Add(doc);
doc->Open();
return new MyDocument
FACTORY METHOD
Motivation
8/14/2019 4050_Design_Pattern.ppt
5/25
Applicability
Use the Factory Method pattern when
a class cant anticipate the class of objects it
must create.
a class wants its subclasses to specify the
objects it creates.
classes delegate responsibility to one of
several helper subclasses, and you want tolocalize the knowledge of which helper
subclass is the delegate.
8/14/2019 4050_Design_Pattern.ppt
6/25
Product
Creator
ConcreteProduct
FactoryMethod()
AnOperation()
ConcreteCreator
FactoryMethod()
...
product = FactoryMethod()
...
return new ConcreteProduct
FACTORY METHOD
Structure
8/14/2019 4050_Design_Pattern.ppt
7/25
Participants
Product
Defines the interface of objects the factory method creates
ConcreteProduct
Implements the product interface
Creator
Declares the factory method which returns object of type product
May contain a default implementation of the factory method
Creator relies on its subclasses to define the factory method so
that it returns an instance of the appropriate Concrete Product.
ConcreteCreator
Overrides factory method to return instance of ConcreteProduct
8/14/2019 4050_Design_Pattern.ppt
8/25
8/14/2019 4050_Design_Pattern.ppt
9/25
Factory Exampleclass 350Z implements Car; // fast car
class Ram implements Car; // truck
class Accord implements Car; // family car
Car fast = new 350Z(); // returns fast car
public class carFactory {public static Car create(String type) {
if (type.equals("fast")) return new 350Z();
if (type.equals("truck")) return new Ram();
else if (type.equals(family) return new Accord();}
}
Car fast = carFactory.create("fast"); // returns fast car
8/14/2019 4050_Design_Pattern.ppt
10/25
SINGELTON
(Object Creational) Intent:
Ensure a class only has one instance, and provide a
global point of access to it.
Motivation: Some classes should have exactly one instance
(one print spooler, one file system, one window
manager)
A global variable makes an object accessible butdoesnt prohibit instantiation of multiple objects
Class should be responsible for keeping track of its
sole interface
8/14/2019 4050_Design_Pattern.ppt
11/25
Applicability
Use the Singleton pattern when
there must be exactly one instance of a class,
and it must be accessible to clients from a
well-known access point.
when the sole instance should be extensible
by subclassing, and clients should be able to
use an extended instance without modifyingtheir code.
8/14/2019 4050_Design_Pattern.ppt
12/25
Singleton
return uniquelnstancestatic Instance()
SingletonOperation()
GetSingletonData()
Static uniquelnstance
singletonData
SINGLETON
Structure
8/14/2019 4050_Design_Pattern.ppt
13/25
Participants and Collaborations
Singleton:
Defines an instance operation that letsclients access its unique interface
Instance is a class operation (static inJava)
May be responsible for creating its own
unique instance Collaborations:
Clients access a Singleton instance solely
through Singletons Instance operation.
8/14/2019 4050_Design_Pattern.ppt
14/25
Singleton Example
public class Employee {
public static final int ID = 1234; // ID is a singleton
}
public final class MySingleton {
// declare the unique instance of the classprivate static MySingleton uniq = new MySingleton();
// private constructor only accessed from this class
private MySingleton() { }
// return reference to unique instance of classpublic static MySingleton getInstance() {
return uniq;
}
}
8/14/2019 4050_Design_Pattern.ppt
15/25
Adapter Pattern
Definition
Convert existing interfaces to new interface
Where to use & benefits
Help match an interface
Make unrelated classes work together
Increase transparency of classes
8/14/2019 4050_Design_Pattern.ppt
16/25
Adapter Pattern
Example
Adapter from integer Set to integer Priority
Queue
Original
Integer set does not support Priority Queue
Using pattern
Adapter provides interface for using Set as PriorityQueue
Add needed functionality in Adapter methods
8/14/2019 4050_Design_Pattern.ppt
17/25
Adapter Example
public interface PriorityQueue { // Priority
Queue
void add(Object o);
int size();Object removeSmallest();
}
8/14/2019 4050_Design_Pattern.ppt
18/25
Adapter Example
public class PriorityQueueAdapter implements PriorityQueue {Set s;
PriorityQueueAdapter(Set s) { this.s = s; }
public void add(Object o) { s.add(o); }
int size() { return s.size(); }
public Integer removeSmallest() {
Integer smallest = Integer.MAX_VALUE;Iterator it = s.iterator();
while ( it.hasNext() ) {
Integer i = it.next();
if (i.compareTo(smallest) < 0)
smallest = i;}
s.remove(smallest);
return smallest;
}
}
8/14/2019 4050_Design_Pattern.ppt
19/25
Observer Pattern
Classification & Applicability A behavioral (object) pattern:
Concerns objects and their behavior.
Applicability
Vary and reuse 2 different abstractions
independently.
Change to one object requires change in (one
or more) other objectswhose identity is notnecessarily known
8/14/2019 4050_Design_Pattern.ppt
20/25
Observer PatternStructure
Subject
attach (Observer)
detach (Observer)
Notify ()
Observer
Update()
Concrete Observer
Update()
observerState
Concrete Subject
GetState()
SetState()
subjectState
observers
subject
For all x in observers{
x.Update(); }
observerState=
subject.getState();
8/14/2019 4050_Design_Pattern.ppt
21/25
Observer Pattern - Participants
Subject Has a list of observers;
interfaces for attaching/detaching an observer
Observer An updating interface for objects that gets notified ofchanges in a subject.
ConcreteSubject
Stores state of interest to observers Sends notification when state changes.
ConcreteObserver Implements updating interface.
8/14/2019 4050_Design_Pattern.ppt
22/25
Observer Pattern - Collaborations
:ConcreteSubject :ConcreteObserver-1 :ConcreteObserver-2
GetState()
Notify()
Update()
SetState()
GetState()
Update()
Ob P tt
8/14/2019 4050_Design_Pattern.ppt
23/25
Observer Pattern -
Implementationinterface Observer
{void update (Observable sub, Object arg)
}
Java terminology for Subject.
public void addObserver(Observero) {}
public void deleteObserver (Observer o) {}
public void notifyObservers(Object arg){}
class Observable {
}
public boolean hasChanged() {}
8/14/2019 4050_Design_Pattern.ppt
24/25
Observer Pattern -
Implementationpublic PiChartView implements Observer {
void update(Observable sub, Object arg) {
// repaint the pi-chart
}}
A Concrete Observer.
class StatsTable extends Observable{
public boolean hasChanged() {
// override to decide when it is considered changed
}
}
8/14/2019 4050_Design_Pattern.ppt
25/25