Design Patterns


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Design Patterns

  1. 1. Design Patterns Introduction to Design Patterns Rafael Coutinho - [email_address]
  2. 2. Agenda <ul><ul><li>Motivation </li></ul></ul><ul><ul><li>Brief history </li></ul></ul><ul><ul><li>Definition </li></ul></ul><ul><ul><li>Categories </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>Conclusion </li></ul></ul><ul><ul><li>Questions </li></ul></ul>
  3. 3. Motivation <ul><li>Context: Home to live in </li></ul><ul><li>Problem: Protect home from rain </li></ul><ul><ul><li>Solutions: </li></ul></ul><ul><ul><ul><li>Live in a cave </li></ul></ul></ul><ul><ul><ul><li>Live in a tent </li></ul></ul></ul><ul><ul><ul><li>Build a house with a roof </li></ul></ul></ul>
  4. 4. Motivation <ul><ul><li>It’s a solution, not a receipt </li></ul></ul>
  5. 5. Motivation <ul><li>Advantages of a standard design </li></ul><ul><ul><li>Speed up the development process </li></ul></ul><ul><ul><li>More robust solution </li></ul></ul><ul><ul><li>Easy to maintain </li></ul></ul><ul><ul><li>Easy to evolve </li></ul></ul><ul><ul><li>Allow beginners to act as experienced professional </li></ul></ul>
  6. 6. Brief history <ul><li>1977 - Christopher Alexander - Architecture </li></ul><ul><ul><li>A Pattern Language: Towns, Buildings, Construction </li></ul></ul><ul><li>Mid-to-late 1980 – Object oriented use </li></ul><ul><li>1995 - Design Patterns: Elements of Reusable Object-Oriented Software </li></ul><ul><ul><li>Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides </li></ul></ul><ul><ul><li>Gang of Four </li></ul></ul>
  7. 7. Definition <ul><li>Christopher Alexander </li></ul><ul><li>&quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&quot; </li></ul>
  8. 8. Definition <ul><li>A design pattern is defined using the following template: </li></ul><ul><ul><li>Pattern name and classification: A conceptual handle and category for the pattern </li></ul></ul><ul><ul><li>Intent: What problem does the pattern address? </li></ul></ul><ul><ul><li>Also known as: Other common names for the pattern </li></ul></ul><ul><ul><li>Motivation: A scenario that illustrates the problem </li></ul></ul><ul><ul><li>Applicability: In what situations can the pattern be used? </li></ul></ul><ul><ul><li>Structure: Diagram UML </li></ul></ul><ul><ul><li>Participants: Classes and objects in design </li></ul></ul>
  9. 9. Definition <ul><li>A design pattern is defined using the following template: </li></ul><ul><ul><li>Collaborations: How classes and objects in the design collaborate </li></ul></ul><ul><ul><li>Consequences: What objectives does the pattern achieve? What are the tradeoffs? </li></ul></ul><ul><ul><li>Implementation: Implementation details to consider, language-specific issues </li></ul></ul><ul><ul><li>Sample code: Sample code in a selected language (Smalltalk, Java or C++) </li></ul></ul><ul><ul><li>Known uses: Examples from the real world </li></ul></ul><ul><ul><li>Related patterns: Comparison and discussion of related patterns </li></ul></ul>
  10. 10. Sample <ul><li>Singleton – Creational Pattern </li></ul>Pattern name and Classification <ul><li>Ensure a class only has one (or a limited number of) instance, and provide a global point of access to it. </li></ul>Intention
  11. 11. Singleton <ul><li>Singleton </li></ul><ul><ul><li>define a getInstance operation that lets clients access its unique instance. </li></ul></ul>Structure and Participants
  12. 12. Singleton <ul><li>Controlled access to sole instance. </li></ul><ul><li>Improvement over global variables. </li></ul><ul><li>Permits a variable number of instances </li></ul>Consequences
  13. 13. Singleton Implementation and Code public class Singleton { private static Singleton instance; private Singleton(){ } public static synchronized Singleton getInstance() { if (instance == null ) { instance = new Singleton(); } return instance; } }
  14. 14. Singleton Motivation Printers Files Applicability <ul><li>One instance of a class </li></ul><ul><li>Well-known access point </li></ul>
  15. 15. Singleton <ul><li>Examples: </li></ul><ul><ul><li>java.lang.Runtime </li></ul></ul><ul><ul><ul><li>getRuntime </li></ul></ul></ul><ul><ul><li>Known Uses </li></ul></ul><ul><li>Abstract Factory </li></ul><ul><ul><li>Related Patterns </li></ul></ul>
  16. 16. Categories <ul><ul><li>Fundamental </li></ul></ul><ul><ul><li>Creational </li></ul></ul><ul><ul><li>Structural </li></ul></ul><ul><ul><li>Behavioral </li></ul></ul>
  17. 17. Fundamental Patterns <ul><li>They are fundamental in the sense that they are widely used by other patterns or are frequently used in a large number of programs. </li></ul><ul><li>Interface </li></ul><ul><li>Immutable </li></ul><ul><li>Proxy* </li></ul>
  18. 18. Structural Patterns <ul><li>A re concerned with how classes and objects are composed to form larger structures </li></ul><ul><li>Structural class patterns use inheritance to compose interfaces or implementations </li></ul><ul><li>Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new functionality </li></ul>Proxy Facade Composite Decorator Adapter
  19. 19. Behavioral Patterns <ul><li>Patterns that identify common communication patterns between objects and realize these patterns. </li></ul><ul><li>By doing so, these patterns increase flexibility in carrying out this communication </li></ul><ul><li>Related to interaction and responsability. </li></ul><ul><li>Design Patterns (GoF): </li></ul><ul><ul><li>Observer </li></ul></ul><ul><ul><li>Strategy </li></ul></ul><ul><ul><li>Visitor </li></ul></ul><ul><ul><li>Chain of Responsability </li></ul></ul><ul><ul><li>Command </li></ul></ul><ul><ul><li>Iterator </li></ul></ul>
  20. 20. Creational Patterns <ul><li>Creational design patterns abstract the instantiation process </li></ul><ul><li>Help make a system independent of how its objects are created, composed, and represented </li></ul><ul><li>Encapsulate knowledge about which concrete classes the system uses. </li></ul><ul><li>Hide how instances of classes are created and put together </li></ul><ul><li>Design Patterns: </li></ul><ul><ul><li>Singleton </li></ul></ul><ul><ul><li>Abstract Factory </li></ul></ul><ul><ul><li>Factory Method </li></ul></ul><ul><ul><li>Prototype </li></ul></ul>
  21. 21. Some More Patterns Samples <ul><li>Structural </li></ul><ul><ul><li>Decorator </li></ul></ul><ul><li>Creational </li></ul><ul><ul><li>Abstract Factory </li></ul></ul>
  22. 22. Decorator <ul><li>Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality </li></ul>Intention Motivation
  23. 23. Decorator <ul><li>To add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects </li></ul><ul><li>For responsibilities that can be withdrawn </li></ul><ul><li>When extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing </li></ul>Applicability
  24. 24. Decorator Structure and Participants
  25. 25. Decorator <ul><li>Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request </li></ul><ul><li>More flexibility than static inheritance </li></ul><ul><li>Avoids feature-laden classes high up in the hierarchy </li></ul><ul><li>A decorator and its component aren't identical </li></ul><ul><li>Lots of little objects </li></ul>Consequences
  26. 26. Decorator Implementation and Code public class LowerCaseInputStream extends FilterInputStream { public LowerCaseInputStream( InputStream in) { super ( in ); } public int read() throws IOException { int c = super .read(); return ( c == -1 ? c : Character .toLowerCase( ( char )c ) ); } public int read( byte [] b, int offset, int len) throws IOException { int result =, offset, len); for ( int i = offset; i < offset+result; i++) { b[i] = ( byte ) Character .toLowerCase( ( char )b[i] ); } return result; } } public class InputTest { public static void main( String [] args) { int c; try { InputStream in = new LowerCaseInputStream( new BufferedInputStream ( new FileInputStream ( “test.txt” ) ) ); while ( (c = >= 0 ) { System . out . print ( ( char )c ); } in.close(); } catch ( IOException e) { e.printStackTrace(); } } }
  27. 27. Decorator <ul><li>Adapter </li></ul><ul><li>Composite </li></ul><ul><li>Strategy </li></ul><ul><ul><li>Known Uses </li></ul></ul><ul><ul><li>Related Pattners </li></ul></ul>
  28. 28. Abstract Factory <ul><li>Provide an interface for creating families of related or dependent objects without specifying their concrete classes. </li></ul>Intention Motivation GUI
  29. 29. Abstract Factory <ul><li>independence of how its products are created, composed and represented </li></ul><ul><li>a system should be configured with one of multiple families of products </li></ul><ul><li>a family of related product objects is designed to be used together. </li></ul><ul><li>provide a class library of products, and reveal just their interfaces, not their implementations. </li></ul>Applicability
  30. 30. Abstract Factory Structure and Participants
  31. 31. Abstract Factory <ul><li>It isolates concrete classes. </li></ul><ul><li>It makes exchanging product families easy. </li></ul><ul><li>It promotes consistency among products. </li></ul><ul><li>Supporting new kinds of products is hard. (new methods) </li></ul>Consequences
  32. 32. Abstract Factory <ul><li>public interface class DAOFactory { </li></ul><ul><li> public User getUser(); </li></ul><ul><li>} </li></ul><ul><li>public class MySQLDAOFactory implements DAOFactory { </li></ul><ul><li>public User getUser() { </li></ul><ul><li>MySqlConnection conn = new MySqlConnection(); </li></ul><ul><li> User u = new MySqlUser(); </li></ul><ul><li> … </li></ul><ul><li> u.setName(rs.getString(“NAME”)); </li></ul><ul><li>return u ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>public class Client { public static void main(String[] args){ DAOFactory factory = new MySQLDAOFactory(); User userDao = factory.getUser(); } } public class Db2DAOFactory implements DAOFactory { public User getUser() { Db2Connection conn = new Db2Connection(); User u = new Db2User(); … u.setName(rs.getString(“NAME”)); return u ; } }
  33. 33. Abstract Factory Structure and Participants
  34. 34. Abstract Factory <ul><li>Factory Method </li></ul><ul><li>Singleton </li></ul><ul><li>Prototype </li></ul><ul><ul><li>Known Uses </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>SocketFactory.getDefault() </li></ul></ul></ul><ul><ul><li>Related Patterns </li></ul></ul>
  35. 35. Rational Software Architect
  36. 36. Conclusion <ul><li>Design Patterns – it’s based on three aspects: Context, problem and solution </li></ul><ul><li>Can be implemented in N ways </li></ul><ul><li>Speed up development </li></ul><ul><li>Allow easier maintenance </li></ul><ul><li>Easy to extend the solution </li></ul><ul><li>Improve the solution </li></ul>
  37. 37. Resources <ul><li>Design Patterns bestseller book: Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides , Design Patterns: Elements of Reusable Object-Oriented Software , Addison-Wesley, 1994. ISBN 0-201-63361-2 </li></ul><ul><li>Wikipedia website http:// and http:// . </li></ul><ul><li>Object Oriented System course from Department of Computer Science of University of Texas at San Antonio . </li></ul><ul><li>Joseph Bergin professor at Pace University personal website </li></ul>
  38. 38. Questions