Design pattern (implementation)
Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.
Following are the 23 Design Patterns Creational(6), Behavioural(11), Structural(7):
- Behavioural – abbr – (CIMS OTV)C-2 I-2 M-2 S-2 O TV)
- Chain of Responsibility.
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template method
- Visitor
- Creational – abbr – (Creational has singleton so –Single .Building. Ab. Factory Prototype. hai )
- Singleton
- Builder
- Factory Method
- Abstract Factory
- Prototype
- Object pool pattern
- Structural– These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities
- Adaptor
- Bridge
- Composite
- Decorator
- Facade
- Flyweigh
- Proxy
- Abbrev (structure is made of ABCD i.e basics)- A.B.C.D. Fac.Fly. proxy i,e A.B.C.D. does Fake Fly using Proxy
- J2EE Patterns These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center.
- Architectural Design pattern
References
- Elements of reusable Object Oriented Software Design
- JavaTpoint
Behavioural Design Pattern
These design patterns are specifically concerned with communication between objects.
Patterns Covered –
- Strategy
- Chain of Responsibility.
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Template method
- Visitor
abbr – (CIMS OTV)C-2 I-2 M-2 S-2 O TV
Strategy Design Patterm
Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.
i.e a class behavior or its algorithm can be changed at run time.
Implementation
We are going to create a Strategy interface defining an action and concrete strategy classes implementing the Strategy interface. Context is a class which uses a Strategy.
StrategyPatternDemo, our demo class, will use Context and strategy objects to demonstrate change in Context behaviour based on strategy it deploys or uses.

Step 1
Create an interface.
Strategy.java
public interface Strategy {
public int doOperation(int num1, int num2);
}
Step 2
Create concrete classes implementing the same interface.
OperationAdd.java
public class OperationAdd implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
OperationSubstract.java
public class OperationSubstract implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}
OperationMultiply.java
public class OperationMultiply implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}
Step 3
Create Context Class.
Context.java
public class Context {
private Strategy strategy;
public Context(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}
Step 4
Use the Context to see change in behaviour when it changes its Strategy.
StrategyPatternDemo.java
public class StrategyPatternDemo {
public static void main(String[] args) {
Context context = new Context(new OperationAdd());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));
context = new Context(new OperationSubstract());
System.out.println("10 - 5 = " + context.executeStrategy(10, 5));
context = new Context(new OperationMultiply());
System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
}
}