// We find that the algorithm to make coffee and tea are similar and they are
// not supposed to be controlled within the Tea or Coffee classes instead
// the algorithm should be moved out
abstract class Beverage {
final public void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
abstract public void brew();
abstract public void addCondiments();
//some of the methods could be final - making that step in the algorithm compulsory
public final void boilWater() {
println("Boil water");
}
public void pourInCup() {
println("Pour in Cup");
}
}
class Coffee extends Beverage {
public void brew() {
println("Brew Coffee");
}
public void addCondiments() {
println("Add Sugar and Milk");
}
}
class Tea extends Beverage {
public void brew() {
println("Steep Tea");
}
public void addCondiments() {
println("Add Lemon");
}
}

It is used to achieve lose coupling in software design where a request from client is passed to a chain of objects to process them. Then the object in the chain will decide themselves who will be processing the request and whether the request is required to be sent to the next object in the chain or not.

It’s used to implement lose coupling in a request-response model. In command pattern, the request is send to the invoker and invoker pass it to the encapsulated command object. Command object passes the request to the appropriate method of Receiver to perform the specific action.

If we have to change the behavior of an object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state. State pattern is used to provide a systematic and lose-coupled way to achieve this through Context and State implementations.
Context is the class that has a State reference to one of the concrete implementations of the State and forwards the request to the state object for processing.

/**
* in the above case the states themselves aren't directly related the control
* to change the state is given to the external program
*
* but if the state is to be maintained internally then we use an abstract class
* instead of interface and create final state objects.
*
* the abstract class then will have a protected constructor.
**/
abstract class State {
static State initialState;
static final State state1 = new State1();
static final State state2 = new State2();
protected State() {
if (initialState == null)
initialState = this;
}
abstract void stateExit(StateContext owner);
abstract void stateEnter(StateContext owner);
}
class State1 extends State {
void stateExit(StateContext owner) {
}
void stateEnter(StateContext owner) {
}
}
class State2 extends State {
void stateExit(StateContext owner) {
}
void stateEnter(StateContext owner) {
}
}
class StateContext {
private State stateVar;
// Create the object and initialize its state
public StateContext() {
this.stateVar = State.state1;
stateVar.stateEnter(this);
}
// Set the new state
public void setState(State newState) {
stateVar.stateExit(this);
this.stateVar = newState;
stateVar.stateEnter(this);
}
}

Visitor pattern

is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.
For example, think of a Shopping cart where we can add different type of items (Elements), when we click on checkout button, it calculates the total amount to be paid.