COP 3331 Object Oriented Analysis and Design Chapter 7 – Design

Download Report

Transcript COP 3331 Object Oriented Analysis and Design Chapter 7 – Design

COP 3331 Object Oriented Analysis
and Design
Chapter 10 – Patterns
Jean Muhammad
Reference – Object Oriented Software Development Using Java - Jia
Overview






Abstract Factory Pattern
Prototype Pattern
Builder Pattern
Command Pattern
Adapter Pattern
Composite Pattern(8.3.2)
Reference – Object Oriented Software Development Using Java - Jia
Abstract Factory



Category: Creational Design Pattern
Intent: Provide an interface for creating a family of related or
dependent objects without specifying their concrete classes
Applicability: Use this pattern when
–
–
–
–
a system should be created independent of its components or
products.
a system should be configurable with one of multiple
interchangeable families of products.
a family of related products should not be mixed with similar
products from different families.
only the interfaces of the products are exposed, while the
implementation of the products is not revealed.
Reference – Object Oriented Software Development Using Java - Jia
Abstract Factory

Participants in the Abstract Factory Pattern:
–
–
–
–
–
AbstractFactory – Defines methods that create abstract
products.
ConcreteFactory – Implements methods that create
concrete methods
AbstractProduct – Defines the interface for a type of product
and may provide default implementation
ConcreteProduct – Defines a product to be created by the
corresponding concrete factory and implements the
AbstractProduct interface.
Client – Uses only AbstractFactory and AbstractProduct
Reference – Object Oriented Software Development Using Java - Jia
AbstractFactory
Client
makeProductA()
makeProductB()
AbstractProductA
ConcreteFactory1
makeProductA()
makeProductB()
ConcreteFactory2
makeProductA()
makeProductB()
ConcreteProductA1
ConcreteProductA2
AbstractProductB
ConcreteProductB1
ConcreteProductB2
Reference – Object Oriented Software Development Using Java - Jia
Prototype



Category: Creational Design Pattern
Intent: To specify the kinds of objects to create using
prototypical instance and create new instances by
cloning this prototype
Applicability: Use this pattern when
–
–
–
a system should be independent of how its components or
products are created
the classes to instantiate are specified at run-time
you want to avoid building a class hierarchy of factories that
parallels the class hierarchy of products.
Reference – Object Oriented Software Development Using Java - Jia
Prototype Design Pattern

Participants in the Prototype Pattern:
–
–
–
Prototype:which defines interfaces of objects to
be created
ConcretePrototype: Implements prototype
interface
Client: Creates instances by cloning the
prototype.
Reference – Object Oriented Software Development Using Java - Jia
Cloneable
Client
prototype
Prototype
aMethod()
clone()
ConcretePrototype1
ConcreteProtype2
clone()
clone()
p = prototype.clone()
Reference – Object Oriented Software Development Using Java - Jia
Builder Design Pattern



Category: Creational Design Pattern
Intent: To separate the construction of a complex
object from the implementation of its parts so that the
same construction process can create complex
objects from different implementation of parts
Applicability: Use this pattern when
–
–
Creating complex objects should be independent of the
parts.
the construction process should allow various
implementations of the parts used for construction.
Reference – Object Oriented Software Development Using Java - Jia
Building Pattern

Participants in the Builder Pattern:
–
–
–
–
Builder – Defines interface for creating parts.
ConcreteBiulder – Constructs and assemble
parts.
Director – Constructs a product using the Builder
interface
Product – Represents the complex object under
construction.
Reference – Object Oriented Software Development Using Java - Jia
Director
Builder
Product buildProduct()
buildPart()
Product
ConcreteBuilder1
ConcreteBuilder2
buildPart()
buildPart()
Reference – Object Oriented Software Development Using Java - Jia
Command Design Pattern




Category: Behavioral Pattern
Intent: Encapsulate the action of an object, so that
action can be passed as parameters, queued, and
possibly undone.
Also known as: Action
Applicability: Use this pattern when
–
–
–
Actions need to be passed as parameters.
Actions need to be queued and then executed later
Actions may need to be undone
Reference – Object Oriented Software Development Using Java - Jia
Command Design Pattern

Participants in the Command Design Pattern:
–
–
–
–
–
Command: Defines an interface to perform or
undo an action
Receiver: Knows how to perform the action
ConcreteCommand: Delegates the execution of
the action to the Receiver
Client: Creates the concrete commands and binds
the concrete commands to their receivers
Invoker: Ask the command to carry out the action
Reference – Object Oriented Software Development Using Java - Jia
Client
Invoker
Command
execute()
Receiver
action()
ConcreteCommand
execute()
receiver.action()
Reference – Object Oriented Software Development Using Java - Jia
Adapter Pattern




Category: Structural Design Pattern
Intent: Convert the interface of a class into
another interface that clients expect
Also known as: Wrapper and confused with a
bridge
Applicability: Use this pattern when
–
To use an existing class with an interface different
from the desired interface
Reference – Object Oriented Software Development Using Java - Jia
Adapter Pattern

Participants in the Adapter Pattern:
–
–
–
–
Target: Defines the interface used by the client
Client: Uses objects conforming to the Target
interface
Adaptee: the existing class to be re-used
Adapter: Which adapts the interface of the
Adaptee to Target.
Reference – Object Oriented Software Development Using Java - Jia
Client
Target
Adaptee
doTask()
performTask()
Adapter
doTask()
performTask()
Reference – Object Oriented Software Development Using Java - Jia
Design Composite



Category: Structural Design Pattern
Intent: Compose objects into tree structures to
represent a part-whole hierarchy. Composite lets
clients treat individual objects and composite
objects.
Applicability: Use this pattern when
–
–
you want to represent a part-whole hierarchy of objects.
When you want clients to be able to ignore the difference
between composite objects and individual objects.
Reference – Object Oriented Software Development Using Java - Jia
Composite Design Pattern

Participants in the Composite Design Pattern:
–
–
–
–
Component which declares the common interface for all classes in
the composite; implements default behavior common to all
classes, as appropriate; and (optionally) defines an interface for
accessing a component’s parent in hierarchy.
Leave which defines a behavior for primitive objects.
Composite, which declares an interface for accessing and
managing its child compoonents, defines behavior for components
having children, stores child components, and implements childrelated operations in the Component interface.
Client, which manipulates objects in the composition through the
Component interface
Reference – Object Oriented Software Development Using Java - Jia
Component
Client
*
operation()
add(Component)
remove(Component)
getChild(int)
Leaf
Composite
Operation()
operation()
add(Component)
remove(Component)
getChild(int)
Reference – Object Oriented Software Development Using Java - Jia