Design Patterns In Java

Design-Patterns-In-Java


aimOne of my most interesting topic is “Design Patterns In Java”. Along with Liferay articles I will publish articles on various Design Patterns In Java. In this article I will explain only the overview of Java Design Patterns. In simple design patterns, or patterns, are solutions to recurring problems in a given context. Design patterns are beneficial because they describe a problem that occurs repeatedly, and then they explain the solution to the problem in a way that can be used many times over. 

Design pattern definition by Gang of Four (GoF): 

The Gang of Four (GoF)  described patterns as “a solution to a problem in a context.”


Who is Gang of Four(GoF)

Design Patterns: Elements of Reusable Object-Oriented Software is a software engineering book describing recurring solutions to common problems in software design. The book’s authors are Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides with a foreword by Grady Booch.

Book-by-gof

It has been highly influential to the field of software engineering and is regarded as an important source for object-oriented design theory and practice. More than 500,000 copies have been sold in English and in 13 other languages. The authors are often referred to as the Gang of Four (GoF).


[focus]

The three elements—problemsolution, and context—are the essence of a pattern.

[/focus]

Elements and sections for the GoF Design Patterns format shown in below table:-

Element/Section Description
Name Used to help convey the essence of the pattern.
Classification Categories are…
Creational Patterns concerned with creation
Structural Patterns concerned with composition
Behavioral Patterns concerned with interaction and responsibility.
Intent What problem does the pattern address? What does it do?
Also Known As Other common names for the pattern.
Motivation Scenario that illustrates the problem.
Applicability Situations in which the pattern can be used.
Structure Diagram representing the structure of classes and objects in the pattern. The GoF uses Object Modeling Technique (OMT) or Booch notation. Today, Unified Modeling Language (UML), a unification of OMT, Booch, and others, is commonly used.
Participants Classes and/or objects participating in the design pattern along with their responsibilities.
Collaborations How the participants work together to carry out their responsibilities.
Consequences What objectives does the pattern achieve? What are the trade-offs and results?
Implementation Implementation details (pitfalls, hints, or techniques) to consider. Are there language specific issues?
Sample Code Sample code.
Known Uses Examples from the real world.
Related Patterns Comparison and discussion of related patterns; scenarios where this pattern can be used in conjunction with another.

There are 23 patterns as described by GoF which are listed in below:-

Sequence Design Pattern Comment
1 Factory Method Frequently used and also well utilized by other patterns.
2 Strategy Frequently used, so early familiarity helps.
3 Decorator Considered the “skin” to the “guts” of Strategy.
4 Composite Often used along with Chain of Responsibility, Interpreter,Iterator, and Visitor.
5 Iterator Looping through anything is widespread in computing, so why not through objects, too?
6 Template Method Helps to reinforce your understanding of Strategy and Factory Method.
7 Abstract Factory Create more than one type of a group of objects.
8  Builder  Another way to create, similar to Factory Method and Abstract Factory.
9  Singleton  You want only one copy of something.
10  Proxy  Controlled access to a service is needed.
11  Adapter  Gain access to a service with an incompatible interface.
12  Bridge  Decouples the function from the implementation.
13  Mediator  Yet another middleman.
14  Facade  Single interface simplifying multiple interfaces in a subsystem.
15  Observer  A form of the publish/subscribe model.
16  Chain of Responsibility  Passes the message along until it’s dealt with.
17  Memento  Backs up and restores an object’s state.
18  Command  Separates invoker from performer.
19  Prototype  Similar to cloning.
20  State  Object appears to change class and alter its behavior.
21  Visitor  Object that represents an operation that operates on elements of an object structure.
22  Flyweight  Allows you to utilize sharing to support large numbers of objects efficiently.
23  Interpreter  Defines a grammar and an interpreter that uses the grammar to interpret sentences.

The 23 patterns defined by GoF are further classified into three broad categories. These 3 categories are

GoF Creational Design Patterns:
Creational design patterns are the design patterns which discuss about how to create an object.  These patterns are used when a decision must be made at the time a class is instantiated. Typically, the details of the concrete class that is to be instantiated are hidden from (and unknown to) the calling class by an abstract class that knows only about the abstract class or the interface it implements. The following creational patterns are described by the GoF (Total 5):

  • Abstract Factory
  • Builder
  • Factory Method
  • Prototype
  • Singleton

GoF Structural Design Patterns

Structural patterns are concerned with composition or the organization of classes and objects, how classes inherit from each other, and how they are composed from other classes.The following Structural patterns are described by GoF (Total 7):

  •  Adapter
  •  Bridge
  •  Composite
  •  Decorator
  •  Facade
  •  Flyweight
  •  Proxy

GoF Behavioral Design Patterns:
Behavioral patterns are concerned with the interaction and responsibility of objects. They help make complex behavior manageable by specifying the responsibilities of objects and the ways they communicate with each other.The following Behavioral patterns are described by GoF (Total 10):

  •  Chain of Responsibility
  •  Command
  •  Interpreter
  •  Iterator
  •  Mediator
  •  Memento
  •  Observer
  •  State
  •  Strategy
  •  Template Method

Summery:

i) A Design pattern is basically solution to a problem. 

ii) There are 23 Design Pattern as described by GoF. 

iii) The 3 elementsProblem, Solution and Context are the essence of a Design Pattern

iv) All the 23 design patterns are categorized into broad category – Creational Design Pattern, Structural Design Pattern and Behavioral Design Pattern.  

About The Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top