In: Computer Science
[Design Pattern] Think of a scenario that can be solved using all of these 3 patterns - Strategy, Factory and Abstract Factory patterns.
1. Write the Scenario
2. Write the code for the 3 patterns
3. Create the UML diagram
Strategy pattern means changing the algorithm at run-time, according to need(based on user input). Factory pattern works on a single super class and it's multiple sub-classes. In Abstract Factory pattern, an interface or abstract class is designed which then is implemented according to the need.
1. One of the required scenerio may be:
printing the area of a 2-d or 3-d shape.
First, create an interface named Shape, which is implemented by different classes of shapes according to the requirement.
2. Here, I'm not provoding you with the whole code, try this on your own, i'm just providing some key points:
Step1: create interface Shape,
public interface Shape{
void draw();
}
Step2: create some separate classes for 2-d or 3-d shapes, which will implement interface "Shape" like this:
public class Rectangle implements Shape{
public void draw(){
System.out.println("Rectangle");
}
}
public class Cuboid implements Shape{
public void draw(){
System.out.println("Cuboid");
}
}
These classes are called concrete classes.
Step3: create an abstract class to get factories:
public abstract class AbstractShapeFactory{
abstract Shape getShape(String type_of_shape);
}
Step4: create two different factory classes for 2-d an 3-d shapes, which will extend the abstract class defined above.
public class Shape2dFactory extends AbstractShapeFactory{
public Shape getShape(String type_of_shape){
if(type_of_shape.equalsIgnoreCase("rectangle")){ return new Rectangle();}
//create more conditions using else if, for more shapes defined by you in step2.
}
}
public class Shape3dFactory extends AbstractShapeFactory{
public Shape getShape(String type_of_shape){
if(type_of_shape.equalsIgnoreCase("cuboid")){ return new Cuboid();}
//create more conditions using else if, for more shapes defined by you in step2.
}
}
These classes are creating object for concrete classes.
Step5: create factory producer class
public class Factoryproducer{
public static AbstractShapeFactory getFactory(boolean twoDimensional) //check whether shape is 2-d or 3-d
{
if(twoDimensional){
return new Shape2dFactory();
}
else{
return new Shape3dFactory();
}
}
}
Step6: create Main class to get AbstractShapeFactory
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
boolean istwoDimensional;
String type_of_shape;
System.out.print("Is your shape 2D? true/false:"); //implementing strategy pattern
istwoDimensional= sc.nextBoolean();
AbstractShapeFactory factory=FactoryProducer.getFactory(istwoDimensional);
System.out.print("Enter the shape:"); //strategy pattern
type_of_shape=sc.nextLine();
Shape shape=factory.getShape(type_of_shape);
shape.draw();
String user;
System.out.print("Do you want to enter more shapes? Yes/No:");
user=sc.nextLine();
while(user.equalsIgnoreCase("Yes)){
System.out.print("Is your shape 2D? true/false:"); //implementing strategy pattern
istwoDimensional= sc.nextBoolean();
AbstractShapeFactory factory=FactoryProducer.getFactory(istwoDimensional);
System.out.print("Enter the shape:"); //strategy pattern
type_of_shape=sc.nextLine();
Shape shape=factory.getShape(type_of_shape);
shape.draw();
System.out.print("Do you want to enter more shapes? Yes/No:");
user=sc.nextLine();
}
}
}
Feel free to ask in the comment box if you are not able to understand any point.