School of Computing and Informatics Department of Information Technology
School of Computing and Informatics Department of Information Technology
Group member
Design pattern:-
Deal with one of the most commonly performed tasks in an OO application, the creation of objects.
Flyweight pattern is primarily used to reduce the number of objects created and to decrease memory
footprint and increase performance.
Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new
object when no matching object is found.
This question number two is assigned to group four and we will something about this as much as possible. In
Strategy pattern, a class behavior or its algorithm can be changed at run time. This type of design pattern comes
under behavior pattern. In Strategy pattern, we create objects which represent various strategies and a context object
whose behavior varies as per its strategy object.The strategy object changes the executing algorithms of the context
object.
Implementation
We are going to create the strategy interface defining an action and concrete strategy classesimplements 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 behavior based on strategy it deploys or uses.
/********************************************************************/
Step 1
Create an interface.
Strategy.java
Step 2
Create concrete classes implementing the same interface.
OperationAdd.java
Publicclassoperation AddimplementsStrategy {
@Override publicintdo Operation (int num1, int num2) {return num1 + num2;
}
} OperationSubstract.java
PublicclassOperationSubstractimplementsStrategy {
@Override
PublicintdoOperation (int num1, int num2) {return num1 - num2;
}
}
OperationMultiply.java
PublicclassOperationMultiplyimplementsStrategy {
@Override
PublicintdoOperation (int num1, int num2) {return num1 * num2;
}
}
Step 3
Create Context Class.
Context.java
publicclassContext{ private Strategy
strategy;
Step 4
Use the Context to see change in behavior when it changes its Strategy.
StrategyPatternDemo.java
Step 5
Verify the output.
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
Summary
• Algorithms may be related via an inheritance hierarchy or unrelated [must implement the same interface].
• Strategies don’t hide everything -- client code is typically aware that there are a number of strategies and
has some criteria to choose among them -- shifts the algorithm decision to the client.