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
Design Patterns<br />Don’t reinvent the wheel<br />Hanoi - April, 2009<br />Duong Trong Tan, (firstname.lastname@example.org)<br />
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 />
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 />
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 />
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 />
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 />
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 />
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 />
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 />
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?<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 />
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 />