Design Patterns Definition: Pattern:  A representation of a proven solution. Problem Applicable Forces Solution Consequenc...
Design Patterns Definition: Anti-Pattern:  A solution pair not a problem solution pair Anti Pattern Solution Contextual Ca...
Design Patterns Fundamental Design Patterns Creational Design Patterns Partitioning Patterns Behavioral Patterns Concurren...
Delegation  ** Interface  Marker Interface  Immutable Proxy  Fundamental Design Patterns (FDP)
FDP  - Delegation Synopsis:   Delegation is a fundamental method to extend and reuse a classes functionality ( behavior or...
FDP  - Delegation class x public void methodforx () {  } class y x  _x = new x(); …… _x.methodforx ( ) DELEGATION
FDP  - Delegation  -  Example Delegator Delegatee uses 1 1 Instance Class or   Instance
FDP  - Delegation  -  Example FlightSegmen t LuggageCompartment 1.1 checkluggage() 1 1 1.  checkluggage() Instance
FDP  - Delegation  -  Example // Instance of this class represent a flight segment. class FlightSegment { LuggageCompartme...
Delegation  ** Interface  -  Allows classes with some similar data to use polymorphism to execute behavior.  Marker Interf...
Creational Design Patterns Factory Method ** Abstract Factory Builder Prototype  Singleton Object Pool **
CDP  - Factory Method Synopsis:  Need for a class to reuse with arbitrary data types. Reusable class remains independent o...
CDP  - Factory Method Solution: Proxy object and the service providing object must either be instances of a common super c...
CDP  - Factory Method - Example You have an application such as MS Office. You want to perform some common functions with ...
Manage  Files file commands Manage Doc  Files doc file commands Manage Xls  Files xls file commands Manage Ppt  Files ppt ...
CDP  - Factory Method - Example Document Application edits 1 * MyDocument May be doc,  xls, or ppt.
Write to  Socket stream CDP  - Factory Method - Example Socket But you also wish to have strings in which you encrypt the ...
Write to  Encrypted Socket encrypted  stream CDP  - Factory Method - Example EncryptedSocket encrypted  string Encrypt  Da...
CDP  - Factory Method - Example Encrypt  Data string Write to  Encrypted Socket encrypted  stream EncryptedSocket encrypte...
CDP  - Factory Method - Example Encrypt  Data string Write to  Encrypted Socket encrypted  stream EncryptedSocket encrypte...
CDP  - Factory Method - Example Encryption Socket encrypt, decrypt 1 * DESEncryption EncryptedSocket Transcription Encrypt...
CDP  - Object Pool Synopsis:   Manages reuse of objects when a type of object is expensive to create or only limited numbe...
Allow  Database Access database access Suppose you have a database systems that need to allow only a  limited number of ac...
CDP  - Object Pool Client manage objects ReusablePool uses Reusable Reusable Pool
Partitioning Patterns Layered Initialization ** Filter  Composite
PP  - Layered Initialization Synopsis: You need multiple implementations with common logic in super and specialized in sub...
PP  - Layered Initialization Context: You have a piece of logic that requires partial execution prior to determining which...
PP  - Layered Initialization Forces:  A specialized class must be chosen to process complex data.  Constructor of the spec...
PP  - Layered Initialization <ul><li>Objects that performs logic common to all cases is initialized  </li></ul><ul><li>Ini...
Resolve Business Rules query  request PP  - Layered Initialization trigger  to resolve  business rule Suppose you have a b...
Resolve Business Rules query  request PP  - Layered Initialization trigger  to resolve  business rule You need to initiali...
PP  - Layered Initialization DataQueryFactoryIF DataQuery DataQueryImplFactory OracleQuery DB2Query ….. DataQueryImplIF us...
Behavioral Patterns Chain of Responsibility State  Command Null Object Little Language Strategy ** Mediator Template Metho...
Write to  Socket stream Socket But you also wish to have strings in which you encrypt the data. And you write an encrypted...
Document Application edits 1 * MyDocument May be doc,  xls, or ppt. BP  - Observer
Concurrency Patterns Single Threaded Execution Guarded Suspension Balking Scheduler Read/Write Lock Producer-Consumer Two-...
Upcoming SlideShare
Loading in...5
×

Design Patterns

383

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
383
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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.
  • Design Patterns

    1. 1. Design Patterns Definition: Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits
    2. 2. Design Patterns Definition: Anti-Pattern: A solution pair not a problem solution pair Anti Pattern Solution Contextual Causes Refactored Solution Consequences Benefits Symptoms and Consequences
    3. 3. Design Patterns Fundamental Design Patterns Creational Design Patterns Partitioning Patterns Behavioral Patterns Concurrency Patterns
    4. 4. Delegation ** Interface Marker Interface Immutable Proxy Fundamental Design Patterns (FDP)
    5. 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. 6. FDP - Delegation class x public void methodforx () { } class y x _x = new x(); …… _x.methodforx ( ) DELEGATION
    7. 7. FDP - Delegation - Example Delegator Delegatee uses 1 1 Instance Class or Instance
    8. 8. FDP - Delegation - Example FlightSegmen t LuggageCompartment 1.1 checkluggage() 1 1 1. checkluggage() Instance
    9. 9. FDP - Delegation - Example // Instance of this class represent a flight segment. class FlightSegment { LuggageCompartment luggage; /** * Check a piece of luggage * @param piece The piece of luggage to be checked. * @exception LuggageException if piece cannot be checked. */ void checkLuggage(Luggage piece) throws LuggageException { luggage.checkLuggage(piece); } // checkLuggage(Luggage) } // class FlightSegment DELEGATION
    10. 10. 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. Fundamental Design Patterns (FDP)
    11. 11. Creational Design Patterns Factory Method ** Abstract Factory Builder Prototype Singleton Object Pool **
    12. 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. 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. 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. 15. Manage Files file commands Manage Doc Files doc file commands Manage Xls Files xls file commands Manage Ppt Files ppt file commands Make one Function CDP - Factory Method - Example
    16. 16. CDP - Factory Method - Example Document Application edits 1 * MyDocument May be doc, xls, or ppt.
    17. 17. Write to Socket stream CDP - Factory Method - Example 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. string Suppose you have a process which reads and writes a DataStream to a socket. Read from Socket stream Socket string
    18. 18. Write to Encrypted Socket encrypted stream CDP - Factory Method - Example EncryptedSocket encrypted string Encrypt Data string Write to Encrypted Socket encrypted stream EncryptedSocket encrypted string Read from Socket encrypted stream Socket encrypted string Decrypt Data decrypted string But now you realize that there are several different encryption algorithms and codes you wish to use.
    19. 19. CDP - Factory Method - Example Encrypt Data string Write to Encrypted Socket encrypted stream EncryptedSocket encrypted String #1 algorithm # 1 The process to encrypt differs in using many different different algorithms (function/method) and type of string output must be written for each type. Encrypt Data string Write to Encrypted Socket encrypted stream EncryptedSocket encrypted String #2 algorithm # 2 Encrypt Data string Write to Encrypted Socket encrypted stream EncryptedSocket encrypted String #n algorithm # n
    20. 20. CDP - Factory Method - Example Encrypt Data string Write to Encrypted Socket encrypted stream EncryptedSocket encrypted String algorithm # 1 algorithm # 2 algorithm # n The factory pattern allows you to have a framework that will handle any type of algorithm and encrypted data. concrete product
    21. 21. CDP - Factory Method - Example Encryption Socket encrypt, decrypt 1 * DESEncryption EncryptedSocket Transcription EncryptionFactory EncryptionFactoryIF creates requestCreation creates 1 1 * * * 1 Concrete Product Product Factory Socket EncryptedSocket Interface Creation Requester 1 2 3 4 5 6
    22. 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. 23. Allow Database Access database access Suppose you have a database systems that need to allow only a limited number of accesses to the database at one time. CDP - Object Pool database access requested You must write a counting semaphore to protect this resource from having more than the limited access.
    24. 24. CDP - Object Pool Client manage objects ReusablePool uses Reusable Reusable Pool
    25. 25. Partitioning Patterns Layered Initialization ** Filter Composite
    26. 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. 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. 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. 29. PP - Layered Initialization <ul><li>Objects that performs logic common to all cases is initialized </li></ul><ul><li>Initialization concludes by determining the class to instantiate </li></ul><ul><li>Specialized class constructor performs next layer of initialization logic. </li></ul><ul><li>After all initialization, one top-level object exist for logic </li></ul><ul><li>If method needs more specialized logic, it calls method one layer down </li></ul><ul><li>Consequences: </li></ul><ul><li>Complexity of initialization of objects using data requires analysis before initialization can proceed. </li></ul>
    30. 30. Resolve Business Rules query request PP - Layered Initialization trigger to resolve business rule 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. Select Database Perform Oracle Query Perform DB2 Query Perform n Query Oracle query DB2 query n query You cannot perform query until you know what type of database
    31. 31. Resolve Business Rules query request PP - Layered Initialization trigger to resolve business rule You need to initialize the database prior to query. Select Database Perform Oracle Query Perform DB2 Query Perform n Query Oracle query DB2 query n query Initialize Database
    32. 32. PP - Layered Initialization DataQueryFactoryIF DataQuery DataQueryImplFactory OracleQuery DB2Query ….. DataQueryImplIF uses Data Query factory method object appears like this. ServiceImpFactoryIF ServiceImpFactory Service ServiceImpIIF request creation creates
    33. 33. Behavioral Patterns Chain of Responsibility State Command Null Object Little Language Strategy ** Mediator Template Method Snapshot Visitor Observer **
    34. 34. Write to Socket stream 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. string REDO Suppose you have a process which reads and writes a DataStream to a socket. Read from Socket stream Socket string BP - Observer
    35. 35. Document Application edits 1 * MyDocument May be doc, xls, or ppt. BP - Observer
    36. 36. Concurrency Patterns Single Threaded Execution Guarded Suspension Balking Scheduler Read/Write Lock Producer-Consumer Two-Phase Termination
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×