16
. Plab – Tirgul 12 Design Patterns

Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:

  • View
    230

  • Download
    0

Embed Size (px)

Citation preview

.

Plab – Tirgul 12Design Patterns

Design Patterns

The De-Facto Book on Design Patterns:

What Is a Design Pattern? A design pattern is a descriptions of communicating objects and

classes that are customized to solve a general design problem in a particular context.

A solution of a problem in a context.

A pattern is made by four elements: name problem solution consequences

Why a Design Pattern?

Helping new designers to have a more flexible and reusable design (description of others design experience).

Improving the documentation and maintenance of existing system by furnishing an explicit specification of class and object interactions and their intent.

Common terminology

Design Patterns classification

Creational Abstract Factory Factory Method Singleton

Structural Decorator

Behavioral Strategy

Abstract Factory

Provides an interface for creating objects without specifying their concrete classes.

For example: A factory allowed creating checkers pieces

indirectly. Simple switching to checkers where pieces

move according to different rules.

Abstract Factory Structure

AbstractFactory

createA()

createB()

ConcreteFactory1

createA()

createB()

ConcreteFactory2

createA()

createB()

AbstractProductA

ProdA2 ProdA1

AbstractProductB

ProdB2 ProdB1

Creates

Creates

Client

Notation

Part of the UML (Unified Modeling Language)

Inheritance

Association

Aggregation

Composite aggregation

Factory Method

Provides an interface by which instantiation is deferred to a subclass (or even another object).

Example: Used in Abstract Factory, but also can be

used independently.

Factory Method Structure

Product Creator

method1()method2()

FactoryMethod()

Creates

ConcreteProductConcreteCreator

FactoryMethod()

Singleton

Ensures a single instance exists, and allows to access it.

For example: Usually only one instance of

ConcreteFactory is required.

Singleton Structure

Singleton

static getInstance()method1()method2()

static uniqueInstance

return uniqueInstance

Decorator

Attaching additional features to an object dynamically and independently.

For example: Suppose we want to add additional

behaviour to checkers pieces, e.g., counting number of moves performed or number of enemy pieces captured.

Decorator StructureComponent

operation()

ConcreteComponent operation()

Decoratoroperation()

ConcreteDecoratorA

operation()

AddedBehavior()

ConcreteDecoratorB

operation() AddedBehavior()

component->operation() component->operation() component

Decorator::operation()

AddedBehavior()

Decorator::operation()

AddedBehavior()

Strategy Encapsulates a family of algorithms, making them

interchangeable. Used when:

many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.

you need different variants of an algorithm. an algorithm uses data that clients shouldn't know about. a class defines many behaviors, and these appear as

multiple conditional statements in it's operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

Strategy Structure

Context

StrategyAlgorithm()strategy

StrategyAAlgorithm()

StrategyBAlgorithm()