Patterns

Patterns

 

There are 23 GoF patterns, divided into Creational, Structural, and Behavioral patterns. There are 21 J2EE patterns, divided into Presentation, Business, and Integration tier patterns.

GoF Creational
Abstract Factory Create families of related objects.
Each concrete Factory can be a singleton. It contains no state. The factory is purely a constructor object.
Create graphical widgets for a windowing system.
Builder Encapsulate construction of a complex object.
Each Builder instance builds one object.
Build a graphic that may be rendered as either SVG or SWF or Postscript.
Factory Method Define an interface for creating objects, but let subclasses decide which class to instantiate.
The enclosing class has its own meaning; it is not purely a constructor object.
In JDBC, different Connection implementation create their own type of Statement and PreparedStatements.
Prototype Facilitate creation of a complex object by cloning an existing object. Set up a template mail message to be used when creating mail.
Singleton Ensure that a class only has a single instance. A system has a single print spooler, and we must be sure that there is never more than one.
GoF Structural
Adapter Convert the interface of one class so it can be used like another.
Instances of the adapter class each wrap an instance of the adaptee.
Provide an InputStream interface to a microphone. Wrap a property interface over a database table.
Bridge Decouple abstraction from implementation.
The abstraction has one inheritance tree and the implementation has a separate tree.
The abstract class contains an instance of the implementation.
Remote control class has a bridge to a TV hierarchy.
Composite Compose objects into tree structures to represent part/whole hierarchies Graphics objects, each of which has a draw() method. Composite type is also a Graphic and has a draw().
Runable class that may contain other runnables.
Scene graph.
Decorator Wrap an object, provide the same interface, but change or add functionality Input Streams
Swing Borders
Facade Provide a unified, simplified interface to a complex system. Home Theater facade hides the details of the tuner, DVD player, stereo, etc.
Flyweight Use sharing to support the use of large numbers of fine-grained objects efficiently.
Objects are usually non-modifiable.
Allow the possibility of non-shared objects.
Separate objects for each Unicode character. Reuse a character if it is used by more than one in a document.
JVM attempts to re-use String, Long, Integer, Char
Proxy Provide a surrogate or placeholder for another object to control access to it.
Subject object may be remote, or expensive to create, or in need of additional security.
Client access to a web service may be treated as a local object, even though it is a proxy to a remote service.
GoF Behavioral
Chain of Responsibility Create a list of receivers that may handle an event.
Handling the event may, or may not, stop execution.
Events in a window system
Exception handling
Cascading style system.
Command Encapsulate a request as an object. Actions attached to buttons or menu items.
Actions that are recorded to make macros.
Actions that support undo.
Interpreter Implement a language that influences behavior of the larger application.
Interpreted language may be easier or have more security or be more agile or just different than the parent language.
Macro system in an editor.
Scripting language within a game.
Scripted application server.
Expression evaluator or matching criteria.
Iterator Abstract access to members of a collection. Return members of a list.
Wrap an iterator around a database query.
Mediator Encapsulates how a set of objects interact. Promotes loose coupling by keeping objects from referring to each other.
Simplifies communication by managing distribution between objects.
GUI mediator that causes controls to be enabled/disabled based on program state.
Constraint propagation system.
Memento Capture an objects internal state so it can be restored later. Transaction system where we want to commit or roll back the total system state.
Save state in a game.
Observer When an object changes, notify all dependent objects.
Decouples observer from observable. Observable doesn’t know in advance what dependents will watch it.
MVC pattern where a document can have multiple views. Manipulating the model causes views to change.
State Object changes behavior when its internal state changes.
Behavior is delegated to an internal state object.
TCP connection is in Established, Listen, or Closed states.
Strategy Define a family of algorithms, encapsulate in classes, and make them interchangeable. Cipher algorithms
Compression algorithms
Line break algorithms
Template Method Define the skeleton of an algorithm, deferring some steps to subclasses Document object that subclasses reading and writing primitives.
Visitor Represent an operation to be performed on elements of an object structure.
May use overriding to put methods in for multiple types. Typically applies to a collection of objects.
Manipulate elements in a scene graph.
Process an XML document.
J2EE Presentation
Interceptor Filter Intercept and manipulate request and response.
Chain of filters can be applied to request and response.
One chain of filters can apply to multiple URIs or URI patterns.
Implement with Servlet filters or Axis interceptors.
Use for security, logging, performance monitoring.
Web templating or XSLT transforms.
Front Controller Need a central access point for request handling.
Factor out common pre and post processing.
Implement as Servlet or JSP.
Typically delegates control to Application Controller.
A web app may have a single controller, or multiple controllers.
Context Object Avoid using protocol specific information outside of context.
Translate request, response, session, and application contexts into POJOs or Maps or XML.
Decouples code from HTTP, SOAP, etc.
May abstract out form reading or writing.
May improve testability.
Typically uses a Factory to translate contexts.
Application Controller Centralize and modularize action and view management.
Implement and abstract framework for assigning requests to Commands and responses to Views.
MVC frameworks such as Struts, Stripes, or Spring MVC.
Configure through XML or annotation or convention or database.
View Helper Separate a view from its processing logic.
Views encapsulate formatting logic and Helpers encapsulate view processing logic.
Implement as a custom tag or tag file or JavaBean.
Reduces scriptlet code.
Most Helpers only provide presentation logic, but it is possible to connect to a Business Delegate.
Composite View Build a view from smaller parts. Manage content and layout independently.
Build a composite view from sub-views.
Allows multiple programmers and designers to work separately.
Implement with custom tags.
Leverages HTML’s tree-like structure.
Service to Worker Perform core request processing and invoke business logic before passing to the view.
Composed of the Front Controller, Application Controller, and View Helper.
MVC frameworks such as Struts, Stripes, or Spring MVC.
Controller-centric, with most business logic executing in the controller.
Dispatcher View The View handles request and generates response, while managing limited amounts of business processing.
Useful if views are static or generated from presentation model.
Minimal controller logic.
View-centric, with most processing occurring in the views.
Useful for simpler applications or read-only apps where users explore a data model.
J2EE Business
Business Delegate Hide clients from the complexity of remote communication with business service components. Implement as a remote proxy, or client side interface to EJB, or client code over a web service.
May wrap around a service locator.
Service Locator Transparently locate business components and services in a uniform manner. Hide details of resource finding, or web service registries, or JDBC data sources.
Typically singleton objects.
Cache handles to managed objects.
Manage EJBs or JMS queues.
Session Facade Expose business components and services to remote clients.
The Business Delegate is the client-side abstraction for a Session Facade.
The Session Facade is the server-side interface to Application services.
Implement as a Session Bean or web service.
Expose coarse grained interface to multiple session beans and entity beans.
The facade itself contains little business logic.
Application Service Centralize business logic across several business tier components and services.
The server-side business logic is coded into either Application Services or Business Objects.
Implement as POJOs or collections of EJBs or web services.
Encapsulates use-case logic.
Business Object Represent a conceptual domain model with business logic.
Domain model is a coded subset of the abstract business model.
Implement as Entity Bean or JDO or with a DAO.
Composite Entity Use entity beans to implement the conceptual domain model. Implement as Entity Beans or JDO or DAO.
Model associations between classes.
Provide an object view of the database schema.
Transfer Object Transfer data elements over a tier or between tiers.
Reduce network communication by translating live persistent entity data into non-persistent static objects.
Keeps Facades coarse-grained.
Implement as POJOs or XML.
Java classes should implement java.io.Serializable
Classes may be made immutable.
Transfer Object Assembler Provide access to application models that aggregates transfer objects from multiple business components.
Reduce network communication.
Construct object models from non-object sources.
Implement as a Session Facade (POJO or stateless session bean).
Assemble Transfer Objects based on DAOs or Business Objects, etc.
Decouples clients from underlying model.
View List Handler Allow a remote client to iterate over a large result set.
Results list is kept on the server side and is restricted to a page at a time.
Implement as a Session Facade (POJO or stateful session bean).
Cache results (or not)
Can be more flexible than EJB finder methods.
J2EE Integration
Data Access Object Encapsulate data access and manipulation into a separate class.
Decouples persistence implementation from the rest of the application.
Increases maintainability and portability.
Can abstract over different persistence types: RDBMS, OODB, LDAP, flat files, etc.c
May use Abstract Factory to create.
Service Activator Invoke services asynchronously.
Receive asynchronous requests and invoke business services.
Implement as a Message Driven Bean or a JMS message listener or an asynchronous web service or an ESB module.
Can integrate both publish / subscribe and point-to-point messaging.
Domain Store Separate persistence code from object model code.
Transparently persist object model.
This is persistence without Entity Beans.
Implement with JPA or JDO or Hibernate.
Lighter weight than Entity Beans, but more generalized than DAOs.
Web Service Broker Expose business components and services to remote clients using XML and web protocols.
Similar to Session Facade, but oriented to web services
Implement as a web service exposed Session Bean or an Axis servlet or a CXF servlet, etc.
Broker may insert extra logic for logging, monitoring, authentication, …
May be more coarse-grained than a session facade.

 

Other Pattern Collections

 

Antipatterns

An antipattern is a design pattern that typifies common bad practice. Sometimes antipatterns are posed for entertainment or to make straw-man arguments, but often they are serious studies. Like regular patterns, cataloging antipatterns give us a language to describe design practices and create a format in which to study them.

%d bloggers like this: