Core Java Design Pattern : Introduction
- What is Design Pattern?
- What is Coupling?
- Type of Design Pattern
- When and How to Apply Patterns
- Design Pattern Notation
|What is Design Pattern?|
“Pattern” as the name suggests, means series of events occurring in a definite order. The patterns can be found in Java and J2ee technologies also.
Many times, we find that there is a particular way of tackling a problem. This way is easy and has been used many times successfully by a number of people earlier also. This method becomes a pattern.
Patterns Defined: The patterns can be defined in many ways. You can find the definitions of patterns in many good books.
“Design patterns are recurring solutions to design problems.”
Gang of Four (GoF) Patterns: Description
“A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for
creating a reusable object-oriented design. The design pattern identifies the participating classes and instances, their roles
and collaborations, and the distribution of responsibilities.
Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether
it can be applied in view of other design constraints, and the consequences and trade-offs of its use.”
(Gamma, Helm, Johnson, and Vlissides, Design Patterns)
Origin of the Pattern Concept
Building architects first developed the pattern concept. They stated that:
“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
By: (Christopher Alexander, A Pattern Language: Towns, Buildings, Construction)
|What is Coupling?|
Design Pattern helps to achieve following Non-functional attributes
- Development efficiency
Coupling is a measure of how dependant classes are on other classes
To reduce coupling:
- Hide the implementation of the classes
- Couple to the abstract interface of a class
- Reduce the number of methods in the interface of the class
- Consider the coupling of the entire system rather than just between individual classes
|Type of Design Pattern|
Patterns: According to commonly known practices, there are 23 design patterns in Java.
Gang of Four (GoF) Patterns: The GoF patterns are organized into three groups:
- Creational Patterns:- Provide more robust ways to create objects
- Structural Patterns:- Discuss how objects are wired together
- Behavioral Patterns:- Describe how objects interact and distribute responsibility.
All the creational patterns define the best possible way in which an object can be instantiated.
These describes the best way to CREATE object instances.
Now everyone knows the object instance in Java can be created using a new operator.
Book book = new Book ();
So, what’s the great stuff? Well, that’s true.
The new Operator creates the instance of an object, but this is hard-coding.
As I have said earlier, creating good software is difficult and so, hard coding is the last thing one should do. Also, at times the very nature of the object which is created can change according to the nature of the program.
In such scenarios, we can make use of patterns to give this a more general and flexible approach.
There are five types of Creational Patterns.
1. Factory Pattern
2. Abstract Factory Pattern
3. Builder Pattern
4. Prototype Pattern
5. Singleton Pattern
Structural Patterns describe how objects and classes can be combined to form larger structures. The difference between class patterns and object patterns is that class patterns describe abstraction with the help of inheritance and how it can be used to provide more useful program interface. Object patterns, on other hand, describe how objects can be associated and composed to form larger, more complex structures.
There are seven structural patterns described. They are as follows:
1. Adapter Pattern
2. Bridge Pattern
3. Composite Pattern
4. Decorator Pattern
5. Facade Pattern
6. Flyweight Pattern
7. Proxy Pattern
1. Chain of Responsibility Pattern
2. Command Pattern
3. Interpreter Pattern
4. Iterator Pattern
5. Mediator Pattern
The chain of responsibility pattern is based on the same principle as written above. It decouples the sender of the request to the receiver.
The only link between sender and the receiver is the request which is sent. Based on the request data sent, the receiver is picked.
This is called “data-driven”. In most of the behavioral patterns, the data-driven concepts are used to have a loose coupling.
|When and How to Apply Patterns|
- Some literature suggests that patterns be proactively applied to designs
- Some literature suggests using fewer patterns and only refactoring them into a design
- Either way, you should look for a design balance of flexibility and simplicity
- After understading design pattern tutorial, you might be inclined to try and solve every problem with a design pattern
- Design patterns are not a golden hammer to solve every problem
Design Pattern Selection
There is no one best pattern, rather there are specific patterns that apply to particular problems:
Pattern selection should be based on similarities between the context, the problem, and the forces of the
pattern and a design problem
- Multiple patterns might be used to solve a particular problem
|Design Pattern Notation|
Design Pattern Notation
Core java Design Pattern at glance