Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Definition:

Similar presentations


Presentation on theme: "Design Patterns Definition:"— Presentation transcript:

1 Design Patterns Definition:
Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits

2 Design Patterns Definition:
Anti-Pattern: A solution pair not a problem solution pair Contextual Causes Anti Pattern Solution Symptoms and Consequences Refactored Solution Consequences Benefits

3 Design Patterns Fundamental Design Patterns Creational Design Patterns
Partitioning Patterns Behavioral Patterns Concurrency Patterns

4 Fundamental Design Patterns (FDP)
Delegation ** Interface Marker Interface Immutable Proxy

5 FDP - Delegation Synopsis:
Delegation is a fundamental method to extend and reuse a classes functionality ( behavior or methods) Context: Allows instances of a class to play multiple roles. Solution: Reuse and extend behavior using delegation

6 FDP - Delegation class x public void methodforx () { } class y
x _x = new x(); …… _x.methodforx ( ) DELEGATION

7 FDP - Delegation - Example
Delegator uses 1 Delegatee 1 Class or Instance Instance

8 FDP - Delegation - Example
1. checkluggage() 1.1 checkluggage() FlightSegment 1 LuggageCompartment 1 Instance

9 FDP - Delegation - Example
// Instance of this class represent a flight segment. class FlightSegment { LuggageCompartment luggage; /** * Check a piece of luggage piece The piece of luggage to be checked. LuggageException if piece cannot be checked. */ void checkLuggage(Luggage piece) throws LuggageException { luggage.checkLuggage(piece); } // checkLuggage(Luggage) } // class FlightSegment DELEGATION

10 Fundamental Design Patterns (FDP)
Delegation ** Interface - Allows classes with some similar data to use polymorphism to execute behavior. Marker Interface - used for utilities – allows investigation into class information without knowing they are an instance of a particular class. Immutable – forbids any of an object’s state information to change after the object is created. Proxy - forces method calls to an object indirectly.

11 Creational Design Patterns
Factory Method ** Abstract Factory Builder Prototype Singleton Object Pool **

12 CDP - Factory Method Synopsis:
Need for a class to reuse with arbitrary data types. Reusable class remains independent of the classes it instantiates by delegating the choice of which class to instantiate to another object and referring to the newly created object through a common interface. Context: Creates a framework to support instantiations of various data types.

13 CDP - Factory Method Solution:
Proxy object and the service providing object must either be instances of a common super class or implement a common interface.

14 CDP - Factory Method - Example
You have an application such as MS Office. You want to perform some common functions with all the files. Example of common functions might be open file, save file, etc. The only difference is that the functions are done on different file types such as word doc files, excel xls files and PowerPoint ppt files. You could write several independent functions for each of the different types but the code would be very similar with only the data type as a difference. Factory method allows you to build a framework for common functions with only a few classes that reuse methods for each type.

15 CDP - Factory Method - Example
Manage Doc Files doc file commands Manage Xls Files xls file commands Manage Ppt Files ppt file commands file commands Manage Files Make one Function

16 CDP - Factory Method - Example
edits Document 1 Application * MyDocument May be doc, xls, or ppt.

17 CDP - Factory Method - Example
Suppose you have a process which reads and writes a DataStream to a socket. Write to Socket stream string Socket Read from Socket stream string Socket But you also wish to have strings in which you encrypt the data. And you write an encrypted DataStream and read back an encrypted Data Stream decrypt it.

18 CDP - Factory Method - Example
encrypted stream encrypted string Write to Encrypted Socket EncryptedSocket encrypted stream string encrypted string Encrypt Data Write to Encrypted Socket EncryptedSocket encrypted stream decrypted string encrypted string Decrypt Data Read from Socket Socket But now you realize that there are several different encryption algorithms and codes you wish to use.

19 CDP - Factory Method - Example
The process to encrypt differs in using many different different algorithms (function/method) and type of string output must be written for each type. encrypted stream encrypted String #1 Encrypt Data Write to Encrypted Socket string EncryptedSocket algorithm # 1 encrypted stream encrypted String #2 Encrypt Data Write to Encrypted Socket string EncryptedSocket algorithm # 2 encrypted stream encrypted String #n Encrypt Data Write to Encrypted Socket string EncryptedSocket algorithm # n

20 CDP - Factory Method - Example
The factory pattern allows you to have a framework that will handle any type of algorithm and encrypted data. concrete product encrypted stream encrypted String Encrypt Data Write to Encrypted Socket string EncryptedSocket algorithm # 1 algorithm # n algorithm # 2

21 CDP - Factory Method - Example
Socket Socket 4 6 Product encrypt, decrypt Encryption 1 EncryptedSocket EncryptedSocket 1 * Creation Requester DESEncryption 1 5 * Here I create an extension to the socket clas to encrypt the stream of bytes written or read from the socket. We call the class encrypted socke and it supports muoltiple encryption algorithms. Because I want to be able to work with multiple encryption algorithms WITHOUT knowing in advance what classes encapsulate those algorithms it suggest the FACOTRY METHOD PATTERN. 1. The scenario first calls the EncryptedSocket to either encrypt or decrypt a message. This class then gets an object that contains the correct algorithm from the EncryptionFactorIF interface class which creates instances of the Encryption Class through the EncryptionFactory class. In the factory class it calls a method called createEncryptionMethod() to create the appropriate instance for the message Once the proper instance of Encryption is called then the method executes to either encrypt or decrypt the message. The Transcription occurs The message is sent to the socket. Transcription Concrete Product * creates 1 creates 1 EncryptionFactory 3 Factory * EncryptionFactoryIF requestCreation Interface 2

22 CDP - Object Pool Synopsis:
Manages reuse of objects when a type of object is expensive to create or only limited number needed. Context: You wish to limit access to a resource. Solution: Create a reusable class to collaborate with other objects for a limited amount of time. Create a reusable pool to manage reusable objects for use by client objects.

23 CDP - Object Pool Suppose you have a database systems that need to allow only a limited number of accesses to the database at one time. Allow Database Access database access requested database access You must write a counting semaphore to protect this resource from having more than the limited access.

24 CDP - Object Pool Client ReusablePool Reusable Reusable Pool
manage objects Client ReusablePool uses Reusable Pool Reusable A client calls the Client Object and the client object then calls a method in reusalbePool called acquireReusable(). This Pool is designed to be a singleton class with a collection pool for the limited amount of instances of the reusable resource. If there are any reusable objects left the Pool class creates an instance, if not then it waits until a reusable object is returned.

25 Partitioning Patterns
Layered Initialization ** Filter Composite

26 PP - Layered Initialization
Synopsis: You need multiple implementations with common logic in super and specialized in subs. However the common logic decides which specialized subclass to create. Therefore layered initialization encapsulates common and specialized logic to create the multiple implementations.

27 PP - Layered Initialization
Context: You have a piece of logic that requires partial execution prior to determining which subclass methods might be used. You need to layer the initializations of the objects to process the complex logic or complex data.

28 PP - Layered Initialization
Forces: A specialized class must be chosen to process complex data. Constructor of the specialized classes and their sub classes are invoked after it has been decided which specialized class to instanciate. Solution: Essence of this pattern is to layer the initializations of the objects participating in the pattern.

29 PP - Layered Initialization
Objects that performs logic common to all cases is initialized Initialization concludes by determining the class to instantiate Specialized class constructor performs next layer of initialization logic. After all initialization, one top-level object exist for logic If method needs more specialized logic, it calls method one layer down Consequences: Complexity of initialization of objects using data requires analysis before initialization can proceed.

30 PP - Layered Initialization
Suppose you have a business rule engine which must select a type of database on which to query to resolve issues in the rule base. Perform Oracle Query Oracle query trigger to resolve business rule query request Select Database Perform DB2 Query Resolve Business Rules DB2 query n query Perform n Query You cannot perform query until you know what type of database

31 PP - Layered Initialization
You need to initialize the database prior to query. Initialize Database Perform Oracle Query Oracle query trigger to resolve business rule query request Select Database Perform DB2 Query Resolve Business Rules DB2 query n query Perform n Query

32 DataQueryImplFactory
PP - Layered Initialization ServiceImpFactoryIF request creation DataQueryFactoryIF DataQuery ServiceImpFactory uses DataQueryImplFactory creates Service OracleQuery DB2Query ….. ServiceImpIIF DataQueryImplIF Data Query factory method object appears like this.

33 Behavioral Patterns Chain of Responsibility State Command Null Object
Little Language Strategy ** Mediator Template Method Snapshot Visitor Observer **

34 BP - Observer REDO Suppose you have a process which reads and writes
a DataStream to a socket. Write to Socket stream string Socket Read from Socket stream string Socket But you also wish to have strings in which you encrypt the data. And you write an encrypted DataStream and read back an encrypted Data Stream decrypt it.

35 BP - Observer Document Application MyDocument May be doc, xls, or ppt.
edits Document 1 Application * MyDocument May be doc, xls, or ppt.

36 Concurrency Patterns Single Threaded Execution Guarded Suspension
Balking Scheduler Read/Write Lock Producer-Consumer Two-Phase Termination


Download ppt "Design Patterns Definition:"

Similar presentations


Ads by Google