Contents






Affiliated sites






Other sites






         

Business Card




Bring focus to technology and not tools

Selection of people ofte focus only on tools. But tools are exchanged much more often than foundation technoglogies. This has its good reasons. Tools can atttach things - but this will only bring you part of the way - technology decides if the whole thing actually works.





Background knowledge, not one tool, makes the whole difference.


Business best practice - design patterns and OOD


GoF (Gang-of-Four) patterns originally appeared in the now legendary book "Design Patterns - Elements of Reusable Object-Oriented Software" by Eric Gamme, Richard Helm, Ralph Johnson, John Vlissides, 1994. Have now become defacto standard level for IT design discusions.

The contents of this site contains elements collected from the Internet. Credits are provided in the end.
Benefits of Design Patterns
  • Teaches good design
  • Proven best practice
  • Define set of concepts used in communication between designers, architects and developers
  • Systematicly captures reusable experience, solving re-occurring types of problem
GoF Business Design Patterns
  1. Creational
    1. Factory Method IMPORTANT
    2. Abstract Factory IMPORTANT
    3. Singleton IMPORTANT
    4. Builder IMPORTANT
    5. Prototype
  2. Structural
    1. Adapter IMPORTANT
    2. Bridge
    3. Composite
    4. Decorator
    5. Facade IMPORTANT
    6. Flyweight
    7. Proxy IMPORTANT
  3. Behavioral
    1. Chain of Responsibility
    2. Command
    3. Interpreter
    4. Iterator IMPORTANT
    5. Mediator IMPORTANT
    6. Momento
    7. Observer IMPORTANT
    8. State
    9. Strategy
    10. Template Method
    11. Visitor
  4. J2EE
1. Creational Patterns
Creational: Involved with the process of object creation.
1. 1. Factory Method IMPORTANT

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

 

Applicability:

  • A class can't anticipate which kind of class of objects it must create.
  • A class uses its subclasses to specify which objects it creates.
  • You want to localize the knowledge of which class gets created.

Consequences:

  • Gives subclasses a hook for providing an extended version of an object being constructed.

Use in J2EE:

  • The EJB Home interface, which creates new EJB objects
  • Connection factories are used to connect to EIS's using DAO's

1.2. Abstract Factory IMPORTANT

The Abstract Factory pattern is one level of abstraction higher than the factory pattern - it is a factory object that returns one of several factories.

It is used when different families of Objects have to be used, for example the different look and feels of a GUI.

Benefits:

  • It isolates concrete classes.
  • It makes exchanging product families easy.
  • It promotes consistency among products.
  • Supporting new kinds of products is difficult.

Use in J2EE:

  • The EJB Home interface, which creates new EJB objects
  • javax.servlet.jsp.JspFactory is an abstract class that defines factory methods available to JSP's at runtime.
1.3. Singleton IMPORTANT

The Singleton patterns controls the number of instances of a class that can be created to a predefined constant, typically but not always one. It provides a single global point of access to these instances.

Benefits:

  • It avoids polluting the name space with global variables that store sole instances.
  • Permits a variable number of instances.
  • More flexible than static methods.
1.4. Builder IMPORTANT

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Benefits:

  • Lets you vary a product’s internal representation.
  • Isolates code for construction and representation.
  • Gives you finer control over the construction process.

When to use:

  • The algorithm for creating a complex object should be independent of both the parts that make up the object and how those parts are assembled
  • The construction process must allow different representations of the constructed object

Use in J2EE:

  • Separates the push scheduler and the data formatter for HTTP GET requests.
1.5. Prototype

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. (e.g. Java Cloning)

Benefits:

  • Hides the concrete product classes from the client.
  • Allows adding and removing products at run-time.
  • Can specify new object by varying the values of an object’s variables.

Use in J2EE:

  • The Clonable interface provides an implementation of Prototype.
2. Structural Patterns

Structural: Deals with the composition of classes or objects.

2.1 Adapter IMPORTANT

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Benefits:

  • Allows two or more incompatible object to communicate and interact
  • Improves reusability of older functionality

When to use:

  • Permits you to use an existing class that has an interface that does not match the one you need.
  • You want to create a reusable class that cooperates with unrelated or unforeseen classes.
  • The Object Adapter pattern can be used when you need to use several existing subclasses but it is impractical to adapt their interface by subclassing every one.

Use in J2EE:

  • Implementation of a vendor-specific subclasses of an abstract Data Access Object
  • Decoupling tiers in a service.
2.2 Bridge

Decouple an abstraction from its implementation so that the two can vary independently. Two hierarchies of classes that vary independently. Rarely used, however often combined with Abstract factory.

When to use:

  • You want to avoid a permanent binding between an abstraction and its implementation.
  • Both the abstractions and the implementations should be extensible by subclassing.
  • Changes in the implementation should not impact clients.

Benefits:

  • Enabled separation of interface from implementation
  • Improves extensibility
  • Hides implementation detail from clients

Use in J2EE:

  • For most Abstractions Java interfaces are used.
  • DAO implements the Bridge Pattern to separate services.
2.3. Composite

Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

When to use:

  • You want to represent the whole hierarchy or a part of the hierarchy of objects
  • You want client to ignore the difference between composite and leaf objects
  • The structure can have any level of complexity and is dynamic

Benefits:

  • Pattern defines hierarchies consisting of primitive objects and composite objects.
  • Allows clients to treat composite structures and individual objects uniformly.
  • Makes it easier to add new kinds of components.

Disadvantage:

  • Can make a design overly general
  • Leaf has methods such as add() which are meaningless

Use in J2EE:

  • Aggregate Entity beans
2.4. Decorator

Attach additional responsibilities to an object dynamically. A flexible alternative to subclassing. Modifies the behavior of individual objects without having to create a derived class. May be nested recursively.

Benefits:

  • More flexibility than static inheritance
  • Avoids feature laden classes high up in hierarchy
  • Simplified coding as each feature is implemented in a separate class
  • Enhances each object's extensibility because changes can be made by adding new classes

When to use:

  • Responsibilities must be added to individual object dynamically without affecting other objects
  • An objects responsibilities might vary over time
  • Extension by static subclassing is impractical

Use in J2EE:

  • Servlet spec v2.3 includes a mechanism to build, add and remove pre/post-processing filters.
  • java.io streams library uses Decorator to achieve what Unix programmers do with pipes & filters.
  • The EJB object is a decorator for the bean because the bean’s functionality is expanded to include remote behavior.
2.5. Façade IMPORTANT

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Benefits:

  • Provides simple interface to a complex system without reducing options provided by the system
  • Shields clients from subsystem components
  • Promotes weak coupling between the subsystem and its clients
  • Reduces coupling between subsystems is each subsystem only used Façades to interact with other subsystems
  • Translates client requests to subsystem that can fulfill request

When to use:

  • When a way of hiding a complex system inside a simpler interface is required.
  • Lots of dependencies between clients and the implementation classes of an abstraction
  • You want to layer subsystems

Use in J2EE:

  • The Session Entity Façade pattern is a derivation of Façade that uses a Session bean as a façade for multiple Entity beans.
2.6. Flyweight

Use sharing to support large numbers of fine-grained objects efficiently. Each instance does not contain its own state, but stores it externally. A way of splitting objects into mutable and immutable data.

Benefits:

  • Reduction of number of Objects to handle
  • Reduction in memory and on storage devices, if objects are persisted

When to use:

  • The application uses a large number of objects
  • Storage costs are high because of the number of objects
  • The application doesn't depend on object identity
2.7. Proxy IMPORTANT

Provide a surrogate or placeholder for another object to control access to it.

Benefits:

  • A remote proxy can hide the fact that the object resides in a different address space
  • a virtual proxy can perform optimisations, such as creating an object on demand

When to use:

  • You need a more versatile or sophisticated reference to an object than a simple pointer

Use in J2EE:

  • The EJB’s remote interface acts as a proxy for the bean.
  • Proxy is used in RMI.
3. Behavioral

Behavioral: Characterize the ways in which classes or objects interact and distribute responsibility.

3.1. Chain of Responsibility


Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Set of handlers is dynamic.

Benefits:

  • Reduced coupling
  • Added flexibility in assigning responsibilities to objects
  • Allows a set of classes to behave as a whole, because events produced in one class can be set on to other handlers within the composite

When to use:

  • More than one object can handle a request, and the hander isn't known
  • You want to issue a request to one of several objects without specifying the receiver explicitly
  • The set of objects that can handle a request should be specified dynamically

Use in J2EE:

  • Commonly used for parsers and even compilers, F1 help systems.
3.2. Command

Encapsulate a request as an object, thereby letting you parameters clients with different requests, queue or log requests, and support undoable operations.

The Command pattern can be used to provide pluggable behavior which enforces client access to services.

Benefits:
  • Separates the object performing an operation from the one that knows how to perform it
  • Easy to add new commands - don't have to change existing classes

When to use:

  • You want to paramaterize object by an action to perform
  • You specify, queue and execute requests at different times
  • You must support logging, undo or transactions
3.3. Interpreter

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Benefits:

  • Easy to change and extend grammar
  • Implementing grammar is easy

When to use:

  • The grammar of the language is simple
  • Efficiency is not critical
3.4. Iterator IMPORTANT

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Benefits:

  • Allows a different collections to be traversed in the same way
  • Simplifies the interface of the collection

When to use:

  • Need to access a collections contents without exposing the internal representation
  • Support multiple traversals of objects in a collection
  • Provide uniform interface for traversing different structures in a collection
3.5. Mediator IMPORTANT

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

Benefits:

  • Decouples colleagues
  • Simplifies object protocols
  • Centralizes control

When to use:

  • A set of objects communicate in a well defined but complex way
  • You want to customize a behavior that is distributed between several objects without using subclasses
2.6. Memento

Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

Benefits:

  • Preserves encapsulation boundaries
  • Simplifies the originator

When to use:

  • A snapshot of an object's state must be saved so that it can be returned to that state later
  • Using a direct interface to obtain the state would expose implementation details and break the object's encapsulation
2.7. Observer IMPORTANT

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Benefits:

  • Abstract coupling between subject and observer
  • Support for broadcast communication

When to use:

  • A change to one object requires other, dynamically selected, objects to change
  • An object should be able to notify other objects without making assumptions about their identity
2.8. State

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Benefits:

  • Localizes state-specific behavior and partitions behavior for different states
  • Makes state transitions explicit

When to use:

  • An object's behavior depends on its state and it must change its behavior at run-time depending on that state
  • Operations have large, multipart conditional statements that depend on the objects state
2.9. Strategy

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

The Strategy pattern can be used to provide pluggable behavior which enforces client access to services.

Benefits:

  • An alternative to subclassing
  • Defines each behavior in its own class - eliminating conditional statements
  • Easier to extend a model to incorporate new behaviors without recoding

When to use:

  • Many related classes differ only in their behavior
  • You need different variants of an algorithm
  • An algorithm uses data unknown to clients
2.10. 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 changing the algorithm’s structure.

Benefits:

  • Fundamental technique for reusing code

When to use:

  • You want to implement invariant parts of an algorithm once and use subclasses to implement the behavior that can vary
  • When common behavior amongst subclasses should be factored and localized in a common class to avoid duplication
2.11. Visitor

Represent 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.

Benefits:

  • Makes adding new operations easy
  • Gathers related operations and separates unrelated ones

When to use:

  • An object structure contains many classes of objects with differing interfaces and you want to perform operations on these objects that depend on concrete classes
  • Classes defining the object structure rarely change but you often want to define new operations over the structure

J2EE Patterns

There are a number of patterns used in developing J2EE applications. They may be found on some of the pages referred to in the links section below.

Links

  1. Leo Crawford's SCEA ressource collection which has been source for inspiration during my SCEA studies
  2. Web Cornucopia is a very extensive source of design patterns and general information on component design. It Covers Creational, Structural and Behavioural Patterns.
  3. Design Patterns In Java A very nice presentation. It has the bullets for all the design patterns plus UML diagrams for all the patterns. This is a big plus for this resource. You do need some fundamental understanding of UML before you can use this ppt. A very helpful resource.
  4. University of Maryland course notes
  5. The Design Patterns Java Companion
  6. The serverside.com has a good collection of the design pattern usage in J2EE.
  7. Sun Microsystem released the first public release of 15 patterns used in J2EE.
  8. Ian's Patterns mock test