1. Design Patterns Don’t reinvent the wheel Hanoi - April, 2009 Duong Trong Tan, (tandt@fpt.edu.vn)
2. What are design patterns? They are common ways of structuring programs typically deal with a small number of classes, but can be of any size provide a communication framework in which ideas can be discussed at a high level can specify how objects are created, how they interact, or how they are structured
15. Singleton public static Counter getInstance() { if(uInstance == null) { this.uInstance = new Counter(); return this.uInstance; } else { return this.uInstance; } } C
16. The purpose of Singleton Makes sure only one instance of an object exists within an application. A better solution compared to global variables Provide global access to the object instance. Example: Counter, Application Menu System, Application objects, Services, Clients Not as easy as you think: What about threading?
17. Factory Method Defines an interface for creating an object but lets subclasses decide which class to instantiate. Lets classes defer instantiation to subclasses C
18. Benefits of Factories We can choose what to instantiate at runtime We can code to an interface Don’t have to know exactly what kind of object we’ll get back We centralize code In order to add new types of objects, we only have to modify one place, the factory 12
19. Builder C Separate the construction of a complex object from its representation so that the same construction process can create different representations.
20. Façade S 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. (GoF)
21. The DBFacade Example Common JDBC uses: Open a connection to DB Close a connection Get all tables from DB Executes a SQL statement Retrieve result from a SELECT statement All That I Need
22. DatabaseFacade Outlook Client uses DatabaseFacade JDBC API Connection ResultSet ResultSetMetadata Statement DatabaseMetadata Driver …
24. Strategy B Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it
25. Observer B Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. GoF
27. Command B “Let’s hide (encapsulate) the way we call (invoke) methods.”
28. Example: Menus Consider an application menu system. How do we de-couple our menu system from our document class? Composite Pattern! Menu contains many menu items. ConcreteCommand Client invoker We can do a lot of things (actions) with a document. Each interface might be different! Command Interface Receiver
36. References E. Gamma, R. Helm, R. Johnson, & J. Vlissides. Design Patterns: Elements of Object-Oriented Software. Addison-Wesley, Boston, 1995. D. Alur, J. Crupi and D. Malks, Core J2EE Patterns: Best Practices and Design Strategies, Second Edition, 2003
Notas do Editor
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).