Home >Java >javaTutorial >Detailed explanation of Java factory design pattern course
Factory pattern is one of the most commonly used design patterns in Java. This type of design pattern is a creation pattern because this pattern provides one of the best ways to create objects.
In the factory pattern, we do not expose the creation logic to the client to create objects and use a common interface to reference the newly created objects. (Recommended learning: java course)
Implementation method
We will create a Shape interface and a specific class that implements the Shape interface. A factory class ShapeFactory will be defined in the next step.
FactoryPatternDemo This is a demonstration class that will use ShapeFactory to obtain a Shape object. It will pass the information (CIRCLE/RECTANGLE/SQUARE) to the ShapeFactory to get the required object type.
The structure of implementing the factory pattern is shown in the figure below-
Step 1
Create an interface-
Shape.java public interface Shape { void draw(); }
Step 2
Create a concrete class that implements the same interface. Several classes are shown below -
Rectangle.java public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } Square.java public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } } Circle.java public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); } }
Step 3
Create a factory to generate objects of specific classes based on the given information.
ShapeFactory.java public class ShapeFactory { //use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }
Step 4
Use the factory to obtain the object of the specific class by passing information such as type.
FactoryPatternDemo.java public class FactoryPatternDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Circle shape1.draw(); //get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Rectangle shape2.draw(); //get an object of Square and call its draw method. Shape shape3 = shapeFactory.getShape("SQUARE"); //call draw method of circle shape3.draw(); } }
Step 5
The verification output is as follows-
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.
The above is the detailed content of Detailed explanation of Java factory design pattern course. For more information, please follow other related articles on the PHP Chinese website!