Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: ". Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:"— Presentation transcript:

1 . Plab – Tirgul 12 Design Patterns

2 Design Patterns u The De-Facto Book on Design Patterns:

3 What Is a Design Pattern? u A design pattern is a descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context. u A solution of a problem in a context. u A pattern is made by four elements:  name name  problem problem  solution solution  consequences consequences

4 Why a Design Pattern? u Helping new designers to have a more flexible and reusable design (description of others design experience). u Improving the documentation and maintenance of existing system by furnishing an explicit specification of class and object interactions and their intent. u Common terminology

5 Design Patterns classification u Creational  Abstract Factory  Factory Method  Singleton u Structural  Decorator u Behavioral  Strategy

6 Abstract Factory u Provides an interface for creating objects without specifying their concrete classes. u For example:  A factory allowed creating checkers pieces indirectly.  Simple switching to checkers where pieces move according to different rules.

7 Abstract Factory Structure AbstractFactory createA() createB() ConcreteFactory1 createA() createB() ConcreteFactory2 createA() createB() AbstractProductA ProdA2ProdA1 AbstractProductB ProdB2ProdB1 Creates Client

8 Notation u Part of the UML (Unified Modeling Language) Inheritance Association Aggregation Composite aggregation

9 Factory Method u Provides an interface by which instantiation is deferred to a subclass (or even another object). u Example:  Used in Abstract Factory, but also can be used independently.

10 Factory Method Structure ProductCreator method1() method2() FactoryMethod() Creates ConcreteProduct ConcreteCreator FactoryMethod()

11 Singleton u Ensures a single instance exists, and allows to access it. u For example:  Usually only one instance of ConcreteFactory is required.

12 Singleton Structure Singleton static getInstance() method1() method2() static uniqueInstance return uniqueInstance

13 Decorator u Attaching additional features to an object dynamically and independently. u 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.

14 Decorator Structure Component operation() ConcreteComponent operation() Decorator operation() ConcreteDecoratorA operation() AddedBehavior () ConcreteDecoratorB operation() AddedBehavior () component->operation() component Decorator::operation() AddedBehavior()

15 Strategy u Encapsulates a family of algorithms, making them interchangeable. u 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.

16 Strategy Structure Context Strategy Algorithm() strategy StrategyA Algorithm() StrategyB Algorithm()


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

Similar presentations


Ads by Google