Abstract factory pattern
Abstract Factory (Abstract Factory Pattern) is to create a super other factories around the plant. The plant is also known as super other factories factory. This type of design pattern belongs create schema, which provides the best way to create objects. In the abstract factory pattern, the interface is responsible for creating a related object factory, do not need to explicitly specify their class. Each generated in accordance with the object factory can provide factory model. The main problem of interface options.
Pros: When a product family of multiple objects are designed to work together when it guarantees clients always use only objects in the same product family.
Disadvantages: product family expansion is very difficult, to increase a product of a series, both in the abstract Creator Riga codes, but also add specific code inside.
achieve
We will create Shape and Color interfaces and implementations of these interfaces entity classes. The next step is to create an abstract factory class AbstractFactory . It then defines factory class ShapeFactory and ColorFactory , these two classes are the factory extends AbstractFactory . Then create a factory creator / builder class FactoryProducer .
AbstractFactoryPatternDemo , our demo class uses FactoryProducer to get AbstractFactory object. It to AbstractFactory transmitting shape information of the Shape ( CIRCLE / the RECTANGLE / SQUARE ), it needs to obtain the type of the object. It also to AbstractFactory transmitting color information Color ( RED / GREEN / BLUE ), it needs to obtain the type of the object.
step 1
Create an interface for the shape.
1 public interface Shape { 2 void draw(); 3 }
Step 2
Creating an entity class that implements the interface.
1 public class Rectangle implements Shape { 2 3 @Override 4 public void draw() { 5 System.out.println("Inside Rectangle::draw() method."); 6 } 7 }
1 public class Square implements Shape { 2 3 @Override 4 public void draw() { 5 System.out.println("Inside Square::draw() method."); 6 } 7 }
1 public class Circle implements Shape { 2 3 @Override 4 public void draw() { 5 System.out.println("Inside Circle::draw() method."); 6 } 7 }
Step 3
Creating an interface for color
1 public interface Color { 2 void fill(); 3 }
Step 4
Creating an entity class that implements the interface.
1 public class Red implements Color { 2 3 @Override 4 public void fill() { 5 System.out.println("Inside Red::fill() method."); 6 } 7 }
1 public class Green implements Color { 2 3 @Override 4 public void fill() { 5 System.out.println("Inside Green::fill() method."); 6 } 7 }
1 public class Blue implements Color { 2 3 @Override 4 public void fill() { 5 System.out.println("Inside Blue::fill() method."); 6 } 7 }
Step 5
Create an abstract class for Color and the Shape object to get the factory.
1 public abstract class AbstractFactory { 2 public abstract Color getColor(String color); 3 public abstract Shape getShape(String shape) ; 4 }
Step 6
Creating extended AbstractFactory factory class, object entity classes generated based on the given information.
1 public class ShapeFactory extends AbstractFactory { 2 3 @Override 4 public Shape getShape(String shapeType){ 5 if(shapeType == null){ 6 return null; 7 } 8 if(shapeType.equalsIgnoreCase("CIRCLE")){ 9 return new Circle(); 10 } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ 11 return new Rectangle(); 12 } else if(shapeType.equalsIgnoreCase("SQUARE")){ 13 return new Square(); 14 } 15 return null; 16 } 17 18 @Override 19 public Color getColor(String color) { 20 return null; 21 } 22 }
1 public class ColorFactory extends AbstractFactory { 2 3 @Override 4 public Shape getShape(String shapeType){ 5 return null; 6 } 7 8 @Override 9 public Color getColor(String color) { 10 if(color == null){ 11 return null; 12 } 13 if(color.equalsIgnoreCase("RED")){ 14 return new Red(); 15 } else if(color.equalsIgnoreCase("GREEN")){ 16 return new Green(); 17 } else if(color.equalsIgnoreCase("BLUE")){ 18 return new Blue(); 19 } 20 return null; 21 } 22 }
Step 7
Create a factory creator / builder class, passing through the plant to obtain the shape or color information.
1 public class FactoryProducer { 2 public static AbstractFactory getFactory(String choice){ 3 if(choice.equalsIgnoreCase("SHAPE")){ 4 return new ShapeFactory(); 5 } else if(choice.equalsIgnoreCase("COLOR")){ 6 return new ColorFactory(); 7 } 8 return null; 9 } 10 }
Step 8
Use FactoryProducer to obtain AbstractFactory, acquires the object class by passing the entity type information.
. 1 public class AbstractFactoryPatternDemo { 2 public static void main (String [] args) { . 3 . 4 // Get plant shape . 5 AbstractFactory shapeFactory = FactoryProducer.getFactory ( "the SHAPE" ); . 6 . 7 // Get Circle object shape . 8 the Shape Shape1 shapeFactory.getShape = ( "CIRCLE" ); . 9 10 // calls the draw method of Circle . 11 shape1.draw (); 12 is 13 is // Get shape Rectangle object 14 the shape Shape2 = shapeFactory.getShape ( "the RECTANGLE" ); 15 16 // call the draw method of Rectangle . 17 shape2.draw (); 18 is . 19 // Get object shape Square 20 is the Shape shape3 = shapeFactory.getShape ( "SQUARE" ); 21 is 22 is // call the draw method Square 23 is shape3 .draw (); 24 25 // Get color factory 26 is AbstractFactory colorFactory = FactoryProducer.getFactory ( "cOLOR" ); 27 28 // Get object color Red 29 color Color1 = colorFactory.getColor ( "RED" ); 30 31 is // call the fill method Red 32 color1.fill (); 33 is 34 is // Get object color Green 35 Color color2 for colorFactory.getColor = ( "Green" ); 36 37 [ // Fill the method call Green 38 is color2.fill (); 39 40 // Get Blue color of the object 41 is color color3 = colorFactory.getColor ( "BLUE" ); 42 is 43 is // Fill the method call Blue 44 is color3.fill (); 45 } 46 is }
Step 9
The implementation of the program, output:
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.
(Listed above knowledge and real self runoob.com description link: https: //www.runoob.com/design-pattern/abstract-factory-pattern.html)