Question

In: Computer Science

[Design Pattern] Think of a scenario that can be solved using all of these 3 patterns...

[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

Solutions

Expert Solution

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.

  


Related Solutions

A scenario that can be solved using Strategy, Factory, and Abstract Factory patterns. Write the Scenario...
A scenario that can be solved using Strategy, Factory, and Abstract Factory patterns. Write the Scenario Write the code Create the UML diagram
or each of the design patterns listed below, briefly discuss the purpose of that pattern and...
or each of the design patterns listed below, briefly discuss the purpose of that pattern and explain how its use contributed to the code you developed for your Maze Game in Assignment 2. Your explanation should provide specific examples from your game that illustrate the function performed by the implementation of each pattern. a)      Command Pattern b)      State Pattern c)      Singleton Pattern
Can you give a java code example of using a singleton pattern and factory pattern design...
Can you give a java code example of using a singleton pattern and factory pattern design together?
Develop a scenario with a series of logical statements that can be "solved" using k-maps. Once...
Develop a scenario with a series of logical statements that can be "solved" using k-maps. Once you have the scenario and logical statements, translate them into a truth table and use a k-map to build a simplified Boolean equation.
Patterns Which do you think is more difficult to establish, a pattern of property crime or...
Patterns Which do you think is more difficult to establish, a pattern of property crime or a pattern of persons crime? What are potential police responses to these patterns?
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And...
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And why
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2...
Print the following two patterns using nested loops. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ####12 ###123 ##1234 #12345
Briefly describe an example scenario where a strategy design pattern is suitable to use.
Briefly describe an example scenario where a strategy design pattern is suitable to use.
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579...
Print the following two patterns using nested loops. Using java. Pattern 1 13579 13579 13579 13579 Pattern 2 #####1 ### #12 ###123 ##1234 #12345
Using the Boyer-Moore algorithm, find the Bad Match table for the below patterns. Pattern1: AABCACCCA Pattern...
Using the Boyer-Moore algorithm, find the Bad Match table for the below patterns. Pattern1: AABCACCCA Pattern 2: CCCAAABBD Pattern3: ABCABCBAD Pattern4: BSDGSBAA
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT