Design Patterns<br />Don’t reinvent the wheel<br />Hanoi - April, 2009<br />Duong Trong Tan, (tandt@fpt.edu.vn)<br />
What are design patterns?<br />They<br /> are common ways of structuring programs<br />typically deal with a small number ...
Why design patterns?<br />Reuse solutions<br />Vocabulary for development<br />Better software<br />
Who?<br />Alexandre Christopher<br />‘Gang of Four’:  Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides<br />
What?<br />Gang of Four common design patterns<br />Some new J2EE design patterns<br />
How can design patterns  be derived?<br />They do use the Design Principles<br /><ul><li> Programming to  interfaces not  ...
 Favor composition over inheritance
 Encapsulate what varies
 Strive for loosely coupled between objects that interact
 Classes should OPEN for extension and CLOSE for modification
 Depends on abstraction, not concrete classes
  Don't call us, we'll call you
 A class should have only one reason to change</li></li></ul><li>Common patterns<br />Design Patterns<br />Categories<br /...
Singleton<br />public static Counter getInstance() {<br />    if(uInstance == null) {<br />this.uInstance = new Counter();...
The purpose of Singleton<br />Makes sure only one instance of an object exists within an application. <br />A better solut...
Factory Method<br />Defines an interface for creating an object but lets subclasses decide which class to instantiate.<br ...
Benefits of Factories<br />We can choose what to instantiate at runtime<br />We can code to an interface<br />Don’t have t...
Builder<br />C<br />Separate the construction of a complex object from its representation so that the same construction pr...
Façade<br />S<br />Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interf...
The DBFacade Example<br />Common JDBC uses:<br />Open a connection to DB<br />Close a connection<br />Get all tables from ...
DatabaseFacade Outlook<br />Client<br />uses<br />DatabaseFacade<br />JDBC API<br />Connection<br />ResultSet<br />ResultS...
Decorator<br />S<br />BufferedReader in = new BufferedReader(new FileReader(“foo.in"));<br />
Strategy<br />B<br />Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the...
Observer<br />B<br />Define a one-to-many dependency between objects so that when one object changes state, all its depend...
MVC<br />What does it hire?<br />
Command<br />B<br />“Let’s hide (encapsulate) the way we call (invoke) methods.”<br />
Example: Menus<br />Consider an application menu system.<br />How do we de-couple our menu system from our document class?...
AllGoFpatterns<br />
Upcoming SlideShare
Loading in …5
×

Design patterns in brief

3,855 views

Published on

This presentation shows some basics of design patterns and explanation of some common used design patterns such as Singleton, Observer, Command, etc.

Published in: Education, Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,855
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
111
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • Initial session for Java developers @ fRun project.
  • MVC = Model + View + ControllerTan: In desktop apps(ie. Swing, WinForm), it may hire: Observer for connecting 3 layers, Command for views, Singleton or Application control (see Java Desktop Application Framework on Java.net), Strategy for models and many others (if needed).
  • Design patterns in brief

    1. 1. Design Patterns<br />Don’t reinvent the wheel<br />Hanoi - April, 2009<br />Duong Trong Tan, (tandt@fpt.edu.vn)<br />
    2. 2. What are design patterns?<br />They<br /> are common ways of structuring programs<br />typically deal with a small number of classes, but can be of any size<br />provide a communication framework in which ideas can be discussed at a high level<br />can specify how objects are created, how they interact, or how they are structured<br />
    3. 3. Why design patterns?<br />Reuse solutions<br />Vocabulary for development<br />Better software<br />
    4. 4. Who?<br />Alexandre Christopher<br />‘Gang of Four’: Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides<br />
    5. 5. What?<br />Gang of Four common design patterns<br />Some new J2EE design patterns<br />
    6. 6. How can design patterns be derived?<br />They do use the Design Principles<br /><ul><li> Programming to interfaces not implementations
    7. 7. Favor composition over inheritance
    8. 8. Encapsulate what varies
    9. 9. Strive for loosely coupled between objects that interact
    10. 10. Classes should OPEN for extension and CLOSE for modification
    11. 11. Depends on abstraction, not concrete classes
    12. 12. Don't call us, we'll call you
    13. 13. A class should have only one reason to change</li></li></ul><li>Common patterns<br />Design Patterns<br />Categories<br />Singleton<br />Builder<br />Factory method<br />Facade<br />Decorator<br />Observer<br />Command<br />Strategy<br />Creational: How to create objects (higher level than constructors)<br />Behavioral: Describing how objects interact<br />Structural: Common relationships in class diagrams<br />C<br />S<br />B<br />
    14. 14.
    15. 15. Singleton<br />public static Counter getInstance() {<br /> if(uInstance == null) {<br />this.uInstance = new Counter();<br /> return this.uInstance;<br /> } else {<br /> return this.uInstance;<br /> }<br />}<br />C<br />
    16. 16. The purpose of Singleton<br />Makes sure only one instance of an object exists within an application. <br />A better solution compared to global variables <br />Provide global access to the object instance.<br />Example: Counter, Application Menu System, Application objects, Services, Clients<br />Not as easy as you think: What about threading?<br />
    17. 17. Factory Method<br />Defines an interface for creating an object but lets subclasses decide which class to instantiate.<br />Lets classes defer instantiation to subclasses<br />C<br />
    18. 18. Benefits of Factories<br />We can choose what to instantiate at runtime<br />We can code to an interface<br />Don’t have to know exactly what kind of object we’ll get back<br />We centralize code<br />In order to add new types of objects, we only have to modify one place, the factory<br />12<br />
    19. 19. Builder<br />C<br />Separate the construction of a complex object from its representation so that the same construction process can create different representations.<br />
    20. 20. Façade<br />S<br />Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.<br />(GoF)<br />
    21. 21. The DBFacade Example<br />Common JDBC uses:<br />Open a connection to DB<br />Close a connection<br />Get all tables from DB<br />Executes a SQL statement<br />Retrieve result from a SELECT statement<br />All That I Need<br />
    22. 22. DatabaseFacade Outlook<br />Client<br />uses<br />DatabaseFacade<br />JDBC API<br />Connection<br />ResultSet<br />ResultSetMetadata<br />Statement<br />DatabaseMetadata<br />Driver<br />…<br />
    23. 23. Decorator<br />S<br />BufferedReader in = new BufferedReader(new FileReader(“foo.in"));<br />
    24. 24. Strategy<br />B<br />Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it<br />
    25. 25. Observer<br />B<br />Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.<br />GoF<br />
    26. 26. MVC<br />What does it hire?<br />
    27. 27. Command<br />B<br />“Let’s hide (encapsulate) the way we call (invoke) methods.”<br />
    28. 28. Example: Menus<br />Consider an application menu system.<br />How do we de-couple our menu system from our document class?<br />Composite Pattern!<br />Menu contains many menu items.<br />ConcreteCommand<br />Client<br />invoker<br />We can do a lot of things (actions) with a document. Each interface might be different!<br />Command Interface<br />Receiver<br />
    29. 29. AllGoFpatterns<br />
    30. 30. J2EE patterns<br />Categorized by tiers:<br /><ul><li> Presentation Tier
    31. 31. Business Tier
    32. 32. Integration Tier</li></li></ul><li>Data Access Object<br />Alur et al.<br />
    33. 33. Business Objects<br />Alur et al.<br />
    34. 34. Session Façade<br />Alur et al.<br />
    35. 35. Application Service <br />Alur et al.<br />
    36. 36. References<br />E. Gamma, R. Helm, R. Johnson, & J. Vlissides. Design Patterns: Elements of Object-Oriented Software. Addison-Wesley, Boston, 1995.<br />D. Alur, J. Crupi and D. Malks, Core J2EE Patterns: Best Practices and Design Strategies, Second Edition, 2003<br />

    ×