Jia Chapter 9

Download Report

Transcript Jia Chapter 9

COP 3331 Object Oriented Analysis
and Design
Chapter 9 – Patterns
Jean Muhammad
Reference – Object Oriented Software Development Using Java - Jia
Overview


State Design Pattern
Factory Design Pattern
Reference – Object Oriented Software Development Using Java - Jia
State Design Pattern




Category: Behavioral Design Pattern
Intent: Allow an object to alter its behavior when its
internal state changes.
Also known as: Objects for states.
Applicability: Use this state when
–
–
an object’s behavior depends upon its state and it must
change it’s behavior at run time, depending upon that state.
methods have large, multipart conditional statements that
depend on the object’s state (ie switch statements).
Reference – Object Oriented Software Development Using Java - Jia
State Design Pattern

Participants in the State Design Pattern:
–
–
–
Context: Defines the current state by maintaining
an instance of a ConcreteState.
State: Defines an interface for encapsulating the
behavior associated with a particular state of the
context.
ConcreteState: place where each class
implements a behavior associated with a state of
context.
Reference – Object Oriented Software Development Using Java - Jia
Context
State
requested
handle
state.handle()
ConcreteStateA
ConcreteStateB
handle()
handle()
Reference – Object Oriented Software Development Using Java - Jia
Factory Design Pattern




Category: Creational Design Pattern
Intent: To define an interface for creating an object
but defer instantiation to the subclasses.
Also known as: Virtual constructor.
Applicability: Use this state when
–
–
a class cannot anticipate the class of objects it must create.
a class defers to it’s subclasses to specify the objects to
create.
Reference – Object Oriented Software Development Using Java - Jia
Factory Design Pattern

Participants in the State Design Pattern:
Product: Defines the interface of the objects to be created.
– ConcreteProduct: Implements the Product interface, and
may provide default implementation.
– Creator: Defines one or more factory method that create
abstract products, that is, objects of type Product.
– ConcreteCreator: Overrides the factory method to return an
instance of a ConcreteProduct.
The Factory design pattern involves a factory class whose
sole responsibility is to create objects.
–
Reference – Object Oriented Software Development Using Java - Jia
Product
Creator
factoryMethod()
anOperation()
Product = factoryMethod()
ConcreteCreator
ConcreteProduct
factoryMethod()
Return new ConcreteProduct()
Reference – Object Oriented Software Development Using Java - Jia