Design Patterns [creational] 03-- Abstract Factory (Abstract Factory)

I. Introduction

Abstract Factory (Abstract Factory Pattern): create a series of related or dependent objects interface, without specifying their concrete classes. Abstract Factory pattern, also known as Kit, belonging schema object creation.

Note: Here and factory method difference is: a series of (multiple), while the factory is only one way.

Second, the class structure

Abstract factory model is structured as follows:

Character category Brief
AbstractFactory Abstract Factory Usually an interface is an abstract class or
ConcreteFactory Concrete factory Used to create a series of specific products
AbstractProduct Abstract product An abstract class or an interface
Product Specific products To create specific products

Three, UML diagram

For example, we need to design a drawing tool:

2021264-80ea75b8d53429de.png

Fourth, code analysis

1, abstract products

Graphics class interface:

interface Shape{
    public function draw();
}

Color class interface:

interface Color {
   public function fill();
}

2, specific products

Specific graphics classes:

//矩形类
class Rectangle implements Shape {
    public function draw() {
        echo '插入一个矩形 \n\r';
    }
}
//正方形类
class Square implements Shape {
    public function draw() {
        echo '插入一个正方形 \n\r';
    }
}
//圆形类
class Circle implements Shape {
    public function draw() {
        echo '插入一个圆形 \n\r';
    }
}

Specific color categories:

//红色类
class Red implements Color {
    public function fill() {
        echo '填充红色 \n\r';
    }
}
//绿色类
class Green implements Color {
    public function fill() {
        echo '填充绿色 \n\r';
    }
}
//蓝色类
class Blue implements Color {
    public function fill() {
        echo '填充蓝色 \n\r';
    }
}

3, Abstract Factory

Create an abstract class for Color and the Shape object to get the factory.

class interface AbstractFactory {
   public function getColor($color);
   public function getShape($shape) ;
}

4, concrete factory

//形状工厂类
class ShapeFactory extends AbstractFactory {

    public function getShape($shapeType){
        if($shapeType == false){
            return false;
        }        
        if($shapeType=="circle"){
            return new Circle();
        } else if($shapeType=="rectangle"){
            return new Rectangle();
        } else if($shapeType=="square"){
            return new Square();
        }
        return false;
    }
    
    public function getColor($color) {
        return false;
    }
}

//颜色工厂类
class ColorFactory extends AbstractFactory {

    public function getShape($shapeType){
        return false;
    }
   
    public function getColor($color) {
        if($color == false){
            return false;
        }
        if($color=="red"){
            return new Red();
        } else if($color=="green"){
            return new Green();
        } else if($color=="blue"){
            return new Blue();
        }
        return false;
    }
}

5, factory creator

Create a factory creator / builder class, passing through the plant to obtain the shape or color information.

class FactoryProducer {
    public static function getFactory($choice){
        if($choice=="shape")){
            return new ShapeFactory();
        } else if($choice=="color"){
            return new ColorFactory();
        }
        return null;
    }
}

6, using examples

public class Demo {
   public static function test() {
 
      //获取形状工厂
      $shapeFactory = FactoryProducer::getFactory("SHAPE");
      
      //为形状插入Circle对象
      $shape1 = $shapeFactory->getShape("circle");
      //调用 Circle 的 draw 方法
      $shape1->draw();
 
      //为形状插入Rectangle对象
      $shape2 = $shapeFactory->getShape("rectangle");
      //调用 Rectangle 的 draw 方法
      $shape2->draw();
      
      //获取颜色工厂
      $colorFactory = FactoryProducer::getFactory("color");
 
      //获取颜色为 Red 的对象
      $color1 = $colorFactory->getColor("red");
      //调用 Red 的 fill 方法
      $color1->fill();
   }
}

Demo::test();

Test Results:

插入一个圆形
插入一个矩形
填充红色

V. Features

1, the advantages

  • Abstract factory pattern isolated generate specific classes so customers do not need to know what is being created. Because of this isolation, replacement of a particular plant becomes relatively easy. All concrete plants have achieved those public interface defined in the abstract factory, so just change the specific instance of the factory, you can change the behavior of the entire software system to some extent. Further, the application may be implemented abstract factory design pattern object with high cohesion and low coupling, so abstract factory model has been widely used.
  • When a product family of multiple objects are designed to work together, it can ensure that clients always use only objects in the same product family. This need for some to determine its behavior according to the current environment of software systems, is a very practical design mode.
  • Adding new concrete factory and product family easily, without modifying the existing system, in line with "the principle of opening and closing."

2, shortcomings

  • When you add a new product object, difficult to extend the abstract factory to produce new types of products, this is because the provision of all possible sets of products have been created in the abstract factory role, to support new types of products it means that interface be extended, which would involve modifications to the abstract factory role and all its subclasses will obviously bring greater inconvenience.
  • The principle of opening and closing of the tilt of the (increase in new factories and easy product family, adding new product hierarchy trouble). .

3, application scenarios

Consider using an abstract factory pattern in the following cases:

  • A system should not depend on how the product class instance is created, a combination of details and expression, which is important for all types of factory pattern.
  • System has more than one product family, wherein each time only one product family.
  • Products belong to the same product family will be used together, the constraints must be reflected in the design of the system.
  • The system provides a product class library, all of the products appear in the same interface, so that the client does not depend on the specific implementation.

Reproduced in: https: //www.jianshu.com/p/cbd46bcebcc0

Guess you like

Origin blog.csdn.net/weixin_34255793/article/details/91093465