Anda di halaman 1dari 30

Object-Oriented

Programming:
Design Patterns
Ma. Diane S. Rivera
diane.rivera.ext@nsn.com

1 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Course Objectives
Describe the purpose and uses of design patterns

Understand creational pattern concepts in order to help make systems


independent of how its objects are created

Understand structural pattern concepts in order to compose classes and


objects into larger structures

Understand behavioral pattern concepts in order to manage algorithms and


assign responsibilities to objects

2 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Introduction
Software Design Patterns
• abstractions that help structure system designs
• templates for how to solve a problem that occurs in many different
situations or applications
• NOT CODE REUSE, but code design that can be easily created from a
design pattern

Object-Oriented Design Patterns


• show relationships and interactions between classes or objects without
specifying the final application classes or objects that are involved
• standardization of commonly agreed best practices to solve specific
design problems
3 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID
Confidential
Uses and Importance
1. For abstraction over implementation
2. Design stage helper
3. Easier way to facilitate communication over a design choice as normalization
technique
4. For developers to implement good design patterns within applications
5. Reduce the use of inefficient and obscure solutions
6. Speeds up developers' design

Main Categories
1. Creational 2. Structural 3. Behavioral

4 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Patterns
Deal with object creation mechanisms, trying to create
objects in a manner suitable to the situation. Controls
object creation.

5 31/05/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Pattern: Builder

• separates the
construction of a
complex object from its
representation so that
the same construction
process can create
different representations

6 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Pattern: Factory Method

• defines an interface for


creating an object, but
let subclasses decide
class to instantiate

7 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Pattern: Abstract Factory

• creates an instance of
several families of
classes
• useful in situations that
require creation of many
different types of objects,
all derived from a
common base type

8 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Pattern: Prototype

• declares an abstract
base class that specifies
a pure virtual “clone”
method, and maintains a
dictionary of all
“clonable” concrete
derived classes.
• Implementation: Declare
an abstract base class
that specifies a pure
virtual clone() method

9 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Creational Pattern: Singleton
Checklist:
• define a private static attribute
in the "single instance" class
• define a public static accessor
function in the class
• do initialization in the
accessor function
• define all constructors to be
• a class of which only a single protected or private
instance can exist
• provides a global point of access of • clients may only use the
that instance accessor function to
• logger classes manipulate the Singleton
10 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID
Confidential
Structural Patterns
Deal with class and object composition. Structural class-
creation patterns use inheritance to compose interfaces.
Structural object-patterns define ways to compose objects
to obtain new functionality.

11 31/05/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Adapter

• convert the interface of a


class into another
interface clients expect
• match interfaces of
different classes
• impedance match an old
component to a new
system

12 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Bridge

• decouples an abstraction
from its implementation
so that the two can vary
independently
• synonym for the
"handle/body" idiom
• interface object is the
"handle" and
implementation class is
the "body"

13 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Composite

• compose objects into


tree structures to
represent whole-part
hierarchies
• lets client treat individual
objects and
compositions of objects
uniformly

14 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Decorator

• adds responsibilities to
objects dynamically
• provides flexible
alternative to
subclassing for
extending functionality
• client-specified
embellishment of a core
object by recursively
wrapping it

15 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Facade

• provides a unified
interface to a set of
interfaces in a
subsystem.
• defines a higher-level
interface that makes the
subsystem easier to use

16 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Structural Pattern: Proxy

• provides a surrogate or
placeholder for another
object to control access
to it
• uses an extra level of
indirection to support
distributed, controlled, or
intelligent access

17 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Patterns
Deal with class's objects communication. These are
patterns that are most specifically concerned with
communication between objects.

18 31/05/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Chain of Responsibility

• avoids coupling the


sender of a request to its
receiver by giving more
than one object a
chance to handle the
request
• chains the receiving
objects and pass the
request along the chain
until an object handles it

19 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Command

• encapsulates a
command request as an
object thereby letting you
parameterize clients with
different requests, queue
or log requests, and
support undoable
operations

20 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Iterator

• sequentially access the


elements of a collection
• provide a way to access
the elements of an
aggregate object
sequentially without
exposing its underlying
representation

21 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Mediator

• defines simplified
communication between
classes
• defines an object that
encapsulates how a set
of objects interact
• promotes loose coupling
by keeping objects from
referring to each other
explicitly; lets you vary
their interaction
independently

22 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Observer

• a way of notifying
change to a number of
classes
• define a one-to-many
dependency between
objects so that when one
object changes state, all
its dependents are
notified and updated
automatically

23 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: State

• allow an object to alter


its behavior when its
internal state changes;
the object will appear to
change its class
• an object-oriented state
machine

24 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Strategy

• defines a family of
algorithms, encapsulates
each one, and make one
interchangeable
• lets the algorithm vary
independently from the
clients that use it
• capture the abstraction
in an interface, bury
implementation details in
derived classes

25 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Behavioral Pattern: Template Method

• defines the skeleton of an


algorithm in an operation,
deferring some steps to
client subclasses
• lets subclasses redefine
certain steps of an algorithm
without changing the
algorithm's structure
• base class declares
algorithm "placeholders" and
derived classes implement
the placeholders

26 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Miscellaneous Design
Patterns

27 31/05/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Miscellaneous Design Patterns

Flyweight
• a fine-grained instance used for efficient sharing
• uses sharing to support large numbers of fine-grained objects efficiently
(cache and reuse existing class instances)
• pattern for saving memory

Interpreter
• given a language, define a representation for its grammar along with an
interpreter that uses the representation to interpret sentences in the
language
• same with composite pattern only with different application or intent

28 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
Miscellaneous Design Patterns

Memento
• without violating encapsulation, capture and externalize an object's
internal state so that the object can be returned to this state later
• promotes undo or rollback to full object status

Visitor
• defines a new operation to a class without changing the classes of the
elements on which it operates; makes use of double dispatch
• technique for recovering lost type information

29 5/31/2019 © Nokia 2014 - File Name - Version - Creator - DocID


Confidential
31 31/05/2019 © Nokia 2014 - File Name - Version - Creator - DocID
Confidential

Anda mungkin juga menyukai