23 design patterns (three) - An object created

  • Factory Method Factory Method

    Schema definition:
    define an interface for creating an object, but let subclasses decide which class to instantiate technology. Factory Method retarder such that a class instance (object: decoupling means: virtual function) to subclasses.

Components:
Abstract Factory (AbstractFactory) : provides an interface to create a product, the caller access to the plant by its specific method newProduct factory () to create the product.
Concrete Factory (ConcreteFactory) : mainly the implementation of the abstract method in the abstract factory, complete the creation of specific products.
Abstract product (Product) : defines the product specification, describes the main features and functionality of the product.
Specific products (ConcreteProduct) : implements the interface defined roles abstract product, created by the concrete factory, which correspond with between concrete factory.

FIG class .png

package com.lillcol.designmodel;

/**
 * @author lillcol
 * 2019/6/16-21:36
 */
public class FactoryMethod {
    public static void main(String[] args) {
        ConcreteFactory1 concreteFactory1 = new ConcreteFactory1();
        Product product = concreteFactory1.newProduct();
        product.show();
        System.out.println("-------------------------------------------");
        ConcreteFactory2 concreteFactory2 = new ConcreteFactory2();
        Product product2 = concreteFactory2.newProduct();
        product2.show();
    }
}

abstract class Product {//定义了产品的规范,描述了产品的主要特性和功能

    abstract void show();
}

class ConcreteProductA extends Product { //具体产品ProductA

    @Override
    void show() {
        System.out.println("ConcreteProductA");
    }
}

class ConcreteProductB extends Product { //具体产品ProductB

    @Override
    void show() {
        System.out.println("ConcreteProductB");
    }
}

abstract class AbstractFactory{ //提供了创建产品的抽象
    abstract Product newProduct();
}

class ConcreteFactory1 extends AbstractFactory{ //实现抽象工厂中的抽象方法,完成具体产品的创建。

    @Override
    Product newProduct() {
        return  new ConcreteProductA();
    }
}

class ConcreteFactory2 extends AbstractFactory{//实现抽象工厂中的抽象方法,完成具体产品的创建。

    @Override
    Product newProduct() {
        return  new ConcreteProductB();
    }
}
//输出结果:
ConcreteProductA
-------------------------------------------
ConcreteProductB

The advantages of factory method pattern: the
user only needs to know the name of the specific plant you can get the product you want, without having to know the specific product creation process;
just add specific product categories and the corresponding concrete factory class in the system to add new products without any modification to the original factory to meet the principles of opening and closing;

The disadvantage of factory methods:
for each additional product to be increased and a specific product class corresponding to a class concrete factory, which increases the complexity of the system.

  • AbstractFactory Abstract Factory

    Schema definition:
    define an interface, so that excuse is responsible for creating a series of "related or interdependent objects", without specifying their concrete classes.

Abstract factory pattern is an upgraded version of the factory method pattern, factory method pattern produced only one level of product, and abstract factory pattern can produce more grades of products.

Abstract factory conditions:
there are multiple product family, with each family create concrete factory product but of a different hierarchical structure.
The system only possible where a family of consumer products that use the same family of products together.

Components:
Abstract Factory (AbstractFactory ): provides an interface to create a product that contains multiple methods newProduct create products (), you can create several different grades of products.
Concrete Factory (ConcreteFactory) : The main is to achieve more abstract methods in the abstract factory, complete the creation of specific products.
Abstract the product (Product) : product specification defines, describes the main characteristics and functionality, there are a plurality of abstract factory model abstract products.
Specific products (ConcreteProduct) : implements the interface defined roles abstract product, created by the specific plant, it is the same relationship between the many-to-concrete factory.

FIG class .png

package com.lillcol.designmodel;

/**
 * @author lillcol
 * 2019/6/16-22:38
 */
public class AbstractFactory {
    public static void main(String[] args) {
        ConcreteFactory1 concreteFactory1 = new ConcreteFactory1();
        concreteFactory1.newProduct1().show();
        concreteFactory1.newProduct2().show();
        System.out.println("-----------------------------------------");
        ConcreteFactory2 concreteFactory2 = new ConcreteFactory2();
        concreteFactory2.newProduct1().show();
        concreteFactory2.newProduct2().show();
    }
}
//中国 飞机:J20  航母:辽宁舰
//美国 飞机:F22  航母:福特级航母

abstract class Product1 {  //飞机
    abstract void show();
}

class ConcreteProduct11 extends Product1 {//中国飞机

    @Override
    void show() {
        System.out.println("中国 飞机:J20");
    }
}

class ConcreteProduct12 extends Product1 {//美国飞机:F22

    @Override
    void show() {
        System.out.println("美国 飞机:F22 ");
    }
}

abstract class Product2 {  //航母
    abstract void show();
}

class ConcreteProduct21 extends Product2 {//中国 航母:辽宁舰

    @Override
    void show() {
        System.out.println("中国 航母:辽宁舰");
    }
}

class ConcreteProduct22 extends Product2 {//美国 航母:福特级航母

    @Override
    void show() {
        System.out.println("美国 航母:福特级航母 ");
    }
}

abstract class Factory {
    abstract Product1 newProduct1();

    abstract Product2 newProduct2();
}
class ConcreteFactory1 extends Factory {//中国
    @Override
    Product1 newProduct1() {
        return new ConcreteProduct11();
    }

    @Override
    Product2 newProduct2() {
        return new ConcreteProduct21();
    }

}

class ConcreteFactory2 extends Factory {//美国
    @Override
    Product1 newProduct1() {
        return new ConcreteProduct12();
    }

    @Override
    Product2 newProduct2() {
        return new ConcreteProduct22();
    }

}
//输出结果:
中国 飞机:J20
中国 航母:辽宁舰
-----------------------------------------
美国 飞机:F22 
美国 航母:福特级航母 

Abstract factory pattern addition to the advantages of having a factory method model, other major advantages are as follows:
can multilevel Product family associated together within the class management, without the introduction of multiple new class specifically to manage.
When adding a new product family without modifying the original code, satisfy the principle of opening and closing.

Abstract factory pattern drawback:
When the product needs to add a new family of products, all of the factory class needs to be modified.

  • Prototype prototype model
    schema definition:
    a prototype to create an object instance of the specified type, and then create new objects by copying the prototype

Prototype model components:
abstract prototype class: specifies the interface must implement specific prototype object.
DETAILED class prototype: Prototype abstract class implement the clone () method, which is copied object.
Access classes: using clone () method of class specific prototype to copy the new object.

FIG class .png

package com.lillcol.designmodel;

/**
 * @author lillcol
 * 2019/6/17-21:48
 */
public class Prototype {
    public static void main(String[] args) throws CloneNotSupportedException {
        Realizetype realizetype = new Realizetype();
        Realizetype clone = (Realizetype)realizetype.clonePrototype();
        System.out.println("realizetype==clone?  "+ (realizetype==clone));
    }
}


abstract class AbstractPrototype implements Cloneable{
    abstract Object clonePrototype() throws CloneNotSupportedException;
}
class Realizetype  extends   AbstractPrototype{
    Realizetype(){
        System.out.println("Real  King of Monkey");
    }
    Object clonePrototype() throws CloneNotSupportedException {
        System.out.println("fake King of Monkey");
        return (Realizetype)super.clone();
    }
}

//输出结果:
Real  King of Monkey
fake King of Monkey
realizetype==clone?  false

With an already created instance as a prototype to create a prototype of the same or similar new objects by copying this prototype object.
Here, a prototypical instance specifies the kind of object to be created. Objects created in this way is very efficient, there is no need to know the details of object creation.

  • Builder Builder

Schema definition:
to build a complex object representation its phase separation, so that the same construction process (stabilizer) may create different representations (variation).

Builder (Builder) mode components:
product role (Product): It is a complex object that contains multiple components, and off to create its various components by a particular builder.
Abstract builder (Builder): It is an abstract method includes creating various subcomponents of the product interface, typically also contain a complex product process returns getResult ().
Specific builder (ConcreteBuilder): Interface Builder achieve, accomplish specific method of creating the various components of complex products.
Director (Director): it calls part construction and assembly methods builder object of creating complex objects, that did not involve specific products in command who.

FIG class .png

package com.lillcol.designmodel;

/**
 * @author lillcol
 * 2019/6/17-22:32
 */
//组装电脑
public abstract class Builder {
    Product product = new Product();

    abstract void buyKey();

    abstract void buyMouse();

    abstract Product returnComputer();
}


class ConcreteBuilder1 extends Builder {

    @Override
    void buyKey() {
        product.setKey("ConcreteBuilder1 key :双飞燕无线鼠标");
    }

    @Override
    void buyMouse() {
        product.setMouse("ConcreteBuilder1 mouse :双飞燕机械键盘");
    }

    @Override
    Product returnComputer() {
        return product;
    }
}

class ConcreteBuilder2 extends Builder {

    @Override
    void buyKey() {
        product.setKey("ConcreteBuilder1 key :无线鼠标");
    }

    @Override
    void buyMouse() {
        product.setMouse("ConcreteBuilder1 mouse :雷柏机械键盘");
    }

    @Override
    Product returnComputer() {
        return product;
    }
}

class Product {
    String key;

    String mouse;

    public void setKey(String key) {
        this.key = key;
    }

    public void setMouse(String mouse) {
        this.mouse = mouse;
    }

    public void show() {
        System.out.println(key + "    " + mouse);
    }
}

class Director {
    Builder builder;

    public Director(Builder builder) {
        this.builder = builder;
    }

    public Product construct() {
        builder.buyKey();
        builder.buyMouse();
        return builder.returnComputer();
    }

}

class BuilderTest {
    public static void main(String[] args) {
        ConcreteBuilder1 concreteBuilder1 = new ConcreteBuilder1();
        Director dirctor = new Director(concreteBuilder1);
        Product product = dirctor.construct();
        product.show();
        System.out.println("-----------------");
        ConcreteBuilder2 concreteBuilder2 = new ConcreteBuilder2();
        Director dirctor2 = new Director(concreteBuilder2);
        Product product2 = dirctor2.construct();
        product2.show();

    }
}

//输出结果:
ConcreteBuilder1 key :双飞燕无线鼠标    ConcreteBuilder1 mouse :双飞燕机械键盘
-----------------
ConcreteBuilder1 key :无线鼠标    ConcreteBuilder1 mouse :雷柏机械键盘

advantage:

  1. Each specific builders are independent, it is conducive to expansion of the system.
  2. The client does not have to know the details of the internal composition of the product, easy to control the details of risk.

Disadvantages:

  1. Part of the product must be the same, which limits its use.
  2. If the internal changes in product complexity, the model will increase a lot the builder category.

Reference document:
http://c.biancheng.net/view/1364.html
Li Jianzhong 23 design patterns

Guess you like

Origin www.cnblogs.com/lillcol/p/11129940.html