Books We Recommend:

Design Patterns

Creational Design Patterns:



Click to zoom Singleton - Ensure that only one instance of a class is created and Provide a global access point to the object.

and When to Use Common Usage Example: Lazy Singleton in Java Example: Early Singleton in Java Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code. A special care should be taken in multi-threading environments when multiple threads must access the same resources through the same singleton object. There are many common situations when singleton pattern is used:

- Logger Classes

- Configuration Classes

- Accesing resources in shared mode

- Other design patterns implemented as Singletons: Factories and Abstract Factories, Builder, Prototype





Click to zoom Factory(Simplified version of Factory Method) - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface. (Simplified version of Factory Method) -and When to Use Common Usage Factory pattern should be used when: - a framework delegate the creation of objects derived from a common superclass to the factory - we need flexibility in adding new types of objects that must be created by the class Along with singleton pattern the factory is one of the most used patterns. Almost any application has some factories. Here are a some examples in java:

- factories providing an xml parser: javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory

- java.net.URLConnection - allows users to decide which protocol to use





Click to zoom

Document Application Example Factory Method - Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface. and When to Use Common Usage Factory Method pattern should be used when:

- a framework delegate the creation of objects derived from a common superclass to the factory

- the base factory class does not know what concrete classes will be required to create - delegates to its subclasses the creation of concrete objects

- factory subclasses subclasses are aware of the concrete classes that must be instantiated



Factory method pattern, compared to Factory pattern replace the factory with an abstract class and a set of concrete factories subclasses. The subclasses are responsible for creating concrete product objects; for factory method is possible adding new product classes without changing the abstract factory. The same result can be achieved for simplified factory pattern if reflection is used. Along with singleton pattern the factories are the most used patterns. Almost any application has some factories. Here are a some examples:

- factories providing an xml parser: javax.xml.parsers.DocumentBuilderFactory or javax.xml.parsers.SAXParserFactory





Click to zoom

Look & Feel Example Abstract Factory - Offers the interface for creating a family of related objects, without explicitly specifying their classes. - Offers the interface for creating a family of related objects, without explicitly specifying their classes. When to Use Common Usage Example: Gui Look & Feel in Java A system should be configured with one of multiple families of products

A system should be independent of how its products are created, composed and represented

Products from the same family should be used all together, products from different families ahould not be used togheter and this constraint must be ensured.

Only the product interfaces are revealed, the implementations remains hidden to the clients. Abstract Factory should be used when: java.awt.Toolkit - the abstract superclass of all actual implementations of the Abstract Window Toolkit. Subclasses of Toolkit are used to bind the various components to particular native toolkit implementations(Java AWT).

javax.swing.LookAndFeel - an abstract swing factory to swithct between several look and feel for the components displayed(Java Swing).

java.sql.Connection - an abstract factory which create Statements, PreparedStatements, CallableStatements,... for each database flavor. Examples of abstract factories:



Click to zoom

Text Converter Example Builder - Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process. - Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process. Example: Text Converter in Java



Click to zoom Prototype - Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. - Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.



Click to zoom

Database Example Object Pool - reuses and shares objects that are expensive to create.. - reuses and shares objects that are expensive to create.. When to Use Common Usage Sourcecode: Database Connection Pool in Java Basically, we'll use an object pool whenever there are several clients who needs the same stateless resource which is expensive to create. The most common situations when object pool pattern is used:

- Database Connections

- Remote Objects



Behavioral Design Patterns:



Click to zoom Chain of Responsibiliy - It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.

- The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it. - It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.- The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it. Sourcecode:



Click to zoom

Restaurant Example Command - Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue. - Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue. Sourcecode: Buying/Selling stocks in Java



Click to zoom Interpreter - Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language / Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design - Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language / Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design Sourcecode: Romans Numerals Converter in Java



Click to zoom Iterator - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Sourcecode: Java Iterator



Click to zoom Mediator - Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. - Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Sourcecode:



Click to zoom

News Publisher Example Observer - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Sourcecode: News Publisher in Java



Click to zoom

Robot Example Strategy - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Sourcecode: Robot Application in Java



Click to zoom

Travel Example Template Method - Define the skeleton of an algorithm in an operation, deferring some steps to subclasses / Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure. - Define the skeleton of an algorithm in an operation, deferring some steps to subclasses / Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure. Sourcecode: Travel Agency Application in Java



Click to zoom

Customers Example Visitor - Represents an operation to be performed on the elements of an object structure / Visitor lets you define a new operation without changing the classes of the elements on which it operates. - Represents an operation to be performed on the elements of an object structure / Visitor lets you define a new operation without changing the classes of the elements on which it operates. Sourcecode: Customers Report Example



Click to zoom Null Object - Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators. - Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators. Sourcecode:

Structural Design Patterns:



Click to zoom Adapter - Convert the interface of a class into another interface clients expect. / Adapter lets classes work together, that could not otherwise because of incompatible interfaces. - Convert the interface of a class into another interface clients expect. / Adapter lets classes work together, that could not otherwise because of incompatible interfaces.



Click to zoom

Click to zoom Bridge - Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly. - Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly. Sourcecode: Object Persistence Api in Java



Click to zoom

Shapes Example Composite - Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly. - Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly. Sourcecode: Shapes Example in Java



Click to zoom

GUI Example Decorator - add additional responsibilities dynamically to an object. - add additional responsibilities dynamically to an object. Sourcecode: Gui Application Example



Click to zoom

Wargame Example Flyweight - use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary. - use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary. Sourcecode: Java Wargame Example



Click to zoom

Calculator Example Memento - capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed. - capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed. Source Code: Calculator Example in Java