JavaDemystifying Design Pattern in Java

Demystifying Design Pattern in Java

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The knowledge of design pattern is crucial for designing any software, especially when dealing with object oriented design. Any programmer be it Java, or any other languages, unwittingly use one or more design patterns in their day to day programming. It’s intriguing; if we look closely we can see a pattern in every design aspect from the blueprint of a building to the soles used in our shoes. Perhaps the decimal number system got the pattern from the ten fingers we have. Any particular pattern is appraised after a lot of reuse and becomes a standard. Recognizing patterns while designing helps us to map our previous experience to the present condition. Design patterns are some ready-made, problem-solved templates that we can use and reuse every time we find a semblance in our problem from the history of patterns. It can give a clue to our software design especially when we are fumbling for a solution.

What is Design Pattern?

I cannot find a better one liner than to quote Christoper Alexander, “Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem in such a way that you can use this solution a million times over, without ever doing it the same way twice”[1]. While designing, we always encounter some similar problems that we have dealt with in the past. If we can create a template and reuse that pattern of solution to our existing design we can not only minimize our effort but also adhere to the best software engineering practices of reuse. Extra parameters may be added to the solution pattern to make it robust and more reliable. Software maintenance is always an issue and this approach can reduce it to a good extent.

Can We Create Our Own Custom Design Pattern?

Yes we can. However, creating a design pattern is not a cakewalk; it requires many years of experience in designing software. One needs to be very sure about where to apply the pattern, how to apply and where the pattern fails. Then gradual maturity takes the form of a standard that can be reused hundred times whenever a similar problem crops up. The core API architecture of Java uses several standard patterns such as Abstract Factory, Adapter, Builder, Composite, Observer, etc. In fact, it would not be an overstatement to say that the major robustness of Java API architecture rests on the implementation of many a standard design patterns.

Elements of Design Patterns

These are the essential elements of any standard design pattern:

  • Nomenclature: Every pattern has some relevant name associated with it. Isn’t it obvious? How would I call you if you didn’t have a name? But the name should be such that it gives an idea of what this particular pattern intends to establish.
  • Problem definition: Problem definition gives the idea and context where the pattern can be applied.
  • Solution approached: Solution provides the template, the real structure of the pattern that can be applied in our code.
  • Outcome: Outcome is the cost-benefit analysis of the design pattern. From a high level overview, design patterns are nothing but templates; they are not a solution per se but can be a path or give an idea of how to structure a solution. This establishes the fact that every standard design pattern makes certain tradeoffs. It is up to the designer’s discretion whether to tweak the existing pattern or use it in its entirety.

Design Pattern Catalog

According to the purpose and scope, a design pattern can be classified into the following groups [2]. Further they are sub categorized into classes and objects according to static and dynamic implementation respectively. These nomenclatures are not providence. They intend to give a hierarchical suit or meta pattern of names for better understanding.

  • Creational Pattern: Concerns about class/object creation process
    • Class: Factory Method
    • Object: Abstract Factory, Builder, Prototype, Singleton
  • Structural Pattern: Concerns about structure of class/object composition
    • Class: Adapter (can have both implementation – static/dynamic)
    • Object: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
  • Behavioral Pattern: Concerns about the delegation of responsibility and class/object interaction
    • Class: Interpreter, Template Method
    • Object: Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor.

Implementation in Java API Architecture

Java API architecture is strewn with all three categories of design pattern. One does not need to show where these patterns are applied. Just go through the design patterns as discussed above and have a look at the Java API library, you’ll not be surprised. If you are still not convinced, stay tuned we’ll be more specific in the next few articles…

Conclusion

These are the primary design patterns implemented in most modern programming language API libraries. However, standard design patterns have evolved radically from their inception, so they are not limited to these alone. They are everywhere in software design if not in any design architecture. Design patterns actually stops us from reinventing the same wheel over and over again. It’s like a milestone we achieve while creating a solution template of our historical problem but evolution goes on.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories