Prototype design pattern (prototype) - rapid production of cars

Rapidly build cars - prototype mode

What to do if you are tired of producing a car?

Last time, we introduced the factory mode (Factory), that is, the user can produce and return by the factory by providing specific model information.

Later, it was discovered that there were fewer and fewer customers, the factory was unable to make ends meet, and even received complaints. The reputation was extremely poor, and it was about to smash the door and close down. Why is this? Ao, it turns out that the factory is too slow to produce cars. The big boss wants 100 Rolls Royces all of a sudden. Our workers continue to start from scratch, buying parts, assembling tires, assembling bodies, assembling engines...

Senior engineer to help

The assembly was too slow. At this time, the senior engineer thought, if we have a Rolls-Royce at this time, it would be great to be able to copy new Rolls-Royces continuously , but the copy needs to be new , and then continue to design and add Attributes, assembly , this is the magic weapon Cloneable interface of the senior engineer , as long as it is a class that implements this interface, it can copy the same new Rolls-Royce car ! ! Soon, the factory stood up again, and the senior engineer was also promoted to senior engineer~~~

Hahaha, this is the concrete abstraction of the prototype pattern in the design pattern! ! !

The magic weapon of high engineers (implementing prototype mode)

Graphical Magic (Prototype Mode)

insert image description here

At this point, the problem becomes clear, we need to save the factory like this:

  1. Let's design a model. In addition to all the production processes for designing this model, we need to implement the Cloneable interface and override the clone() method for this model class.
  2. When producing a new car, you only need to call the clone() method of the created model to build a new car! ! !

High engineer's initial concrete implementation (shallow copy)

Rolls Royce models

package com.design_patterns.prototype.instance;


import com.design_patterns.factory.instance.Car;

public class RollsRoyce implements Car, Cloneable {
    
    
    private String tirName = null;                  //轮胎型号
    private String bodyName = null;             //车身型号
    private String engineName = null;               //发动机型号
    private Information information = null;         //劳斯莱斯车的信息类对象

    public String getTirName() {
    
    
        return tirName;
    }

    public void setTirName(String tirName) {
    
    
        this.tirName = tirName;
    }

    public String getBodyName() {
    
    
        return bodyName;
    }

    public void setBodyName(String bodyName) {
    
    
        this.bodyName = bodyName;
    }

    public String getEngineName() {
    
    
        return engineName;
    }

    public void setEngineName(String engineName) {
    
    
        this.engineName = engineName;
    }

    public Information getInformation() {
    
    
        return information;
    }

    public void setInformation(Information information) {
    
    
        this.information = information;
    }

    @Override
    public void produce() {
    
    
        System.out.println("劳斯莱斯车生产成功!");
    }

    /**
     * 覆写劳斯莱斯车的拷贝方法,默认为父类的clone方法
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        return super.clone();
    }

    @Override
    public String toString() {
    
    
        return "RollsRoyce{" +
                "tirName='" + tirName + '\'' +
                ", bodyName='" + bodyName + '\'' +
                ", engineName='" + engineName + '\'' +
                ", information=" + information +
                '}';
    }
}

Rolls-Royce production information class Information (indicates the number of cars)

package com.design_patterns.prototype.instance;

public class Information {
    
    
    private static int amount = 0;              //劳斯莱斯车的生产数量
    public Information(){
    
    
        amount++;
    }

    public static int getAmount() {
    
    
        return amount;
    }

    public static void setAmount(int amount) {
    
    
        Information.amount = amount;
    }

    @Override
    public String toString() {
    
    
        return "劳斯莱斯@num" + amount;
    }
}

Production operation test class

package com.design_patterns.prototype.instance;

public class Client {
    
    
    public static void main(String[] args) throws Exception{
    
    
        RollsRoyce rollsRoyce = new RollsRoyce();
        rollsRoyce.setTirName("劳斯莱斯轮胎");                //制造轮胎
        rollsRoyce.setBodyName("劳斯莱斯车身");                //制造车身
        rollsRoyce.setEngineName("劳斯莱斯发动机");                  //制造发动机
        rollsRoyce.produce();                           //制造汽车,此为汽车的制作全部过程
        rollsRoyce.setInformation(new Information());

        System.out.println("样本车产生---->" + rollsRoyce + "\n");

        int num = 50;
        RollsRoyce rollsRoyce1 = null;
        while (num > 0){
    
                                                        //生产50辆劳斯莱斯车
            rollsRoyce1 = (RollsRoyce) rollsRoyce.clone();
            System.out.println(rollsRoyce1);
            System.out.println("生产编号: " + rollsRoyce1.hashCode());
            num--;
        }
    }
}

operation result

劳斯莱斯车生产成功!
样本车产生---->RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}

RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}
生产编号: 1163157884
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}
生产编号: 1956725890
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}
生产编号: 356573597
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}
生产编号: 1735600054
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}
生产编号: 21685669
......
由于生产50台,数据过多暂列举一部分,生产编号均不同

Wow, at this time we found that the exact same 50 too Rolls-Royce was produced almost instantly, and there is no need to work hard on the production of sets, sets, and sets, and the production efficiency has been greatly improved, just as the boss When the customer is very happy.

The senior engineer told the boss that these 50 Rolls-Royces cannot be sold. For the positive development of the factory in the future, they must be destroyed. Why? At this time, I noticed that the glass windows of the Rolls-Royce are uniformly printed with a line of small characters: information: Rolls-Royce@num1, why is the model number 1? It is clearly determined that acount will be accumulated when the information class is instantiated.

At this time, the senior engineer said that, in fact, we only have one instance of Information. We use a shallow copy when copying a Rolls-Royce car , and the Information class is a reference data type . The shallow copy can only point the value of the new reference data type to the address of the original reference data, and cannot be reproduced (new ) refers to the data type. If you want to implement (new) reference data type Information, you must improve the overriding clone() method , and the senior engineer gave a solution...

new solution

The senior engineer gave the guidance plan, which will (RollsRoyce class):

@Override
protected Object clone() throws CloneNotSupportedException {
     
     
    return super.clone();
}

improved to

@Override
protected Object clone() throws CloneNotSupportedException {
     
     
    RollsRoyce rollsRoyce = (RollsRoyce) super.clone();
    rollsRoyce.information = new Information();            //实例化一个新的Information类对象
    return rollsRoyce;                                  //返回深拷贝后的劳斯莱斯车对象
}

This produces the class Information Rolls-Royce information, and again runs the production Rolls-Royce operation...

operation result

劳斯莱斯车生产成功!
样本车产生---->RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num1}

RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num2}
生产编号: 1163157884
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num3}
生产编号: 1956725890
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num4}
生产编号: 356573597
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num5}
生产编号: 1735600054
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num6}
生产编号: 21685669
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num7}
生产编号: 2133927002
RollsRoyce{
    
    tirName='劳斯莱斯轮胎', bodyName='劳斯莱斯车身', engineName='劳斯莱斯发动机', information=劳斯莱斯@num8}
生产编号: 1836019240
......
由于生产50台,数据过多暂列举一部分,生产编号均不同,information属性也不同

Summarize

Just a whim, hee hee, this is the first time I use this example to write notes. I don't know if the effect is good or not. In fact, this chapter also briefly introduces the basic usage of prototype mode, as well as shallow copy, deep copy , and deep copy suggestions . Implemented using object serialization ( this article has a method for serializing deep copies ).

If your friends feel that the writing is not bad, please feel free to give a like, hehe, if you feel that the writing is not good, please private message me, I can correct it, come on!

A senior engineer has been promoted to a senior engineer, how long will he still have to go?

Guess you like

Origin blog.csdn.net/weixin_43479947/article/details/107468497