java设计模式之原型模式和建造者模式的写法(二)

背景

原型模式和建造者模式属于创建型模式,在开发中经常会用到,那么这两种设计模式是怎么写的呢,下面进行代码演示。

原型模式

原型模式的核心就是复制

package com.example.prototype.prototype;

import java.io.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class PrototypePattern implements Cloneable,Serializable {
    
    
    private int age;
    private String name;
    private List<String> list;

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public List<String> getList() {
    
    
        return list;
    }

    public void setList(List<String> list) {
    
    
        this.list = list;
    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
    
    
        //浅克隆
        return super.clone();
    }

    protected Object clone2() throws CloneNotSupportedException {
    
    
        PrototypePattern prototypePattern= (PrototypePattern) super.clone();
        prototypePattern.setList((List<String>) ((ArrayList<String>)list).clone());
        //也可以变成深克隆,不过对象内的应用类型要一个一个进行clone操作
        return prototypePattern;
    }

    @Override
    public String toString() {
    
    
        return "PrototypePattern{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", list=" + list +
                '}';
    }

    //深克隆
    protected Object deepClone() throws CloneNotSupportedException {
    
    
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        try {
    
    
            ObjectOutputStream oos=new ObjectOutputStream(byteArrayOutputStream);
            oos.writeObject(this);

            ByteArrayInputStream bis=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream ois=new ObjectInputStream(bis);
            return ois.readObject();
        } catch (Exception e) {
    
    
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws CloneNotSupportedException {
    
    
        PrototypePattern  prototypePattern=new PrototypePattern();
        prototypePattern.setAge(1);
        prototypePattern.setName("小明");
        List<String>  list1=new ArrayList<>();
        list1.add("数学");
        list1.add("英语");
        list1.add("语文");
        prototypePattern.setList(list1);
        System.out.println(prototypePattern.toString());

        PrototypePattern clone1= (PrototypePattern) prototypePattern.clone();
        PrototypePattern clone2= (PrototypePattern) prototypePattern.clone2();
        PrototypePattern clone3= (PrototypePattern) prototypePattern.deepClone();

        prototypePattern.getList().remove(2);
        

        System.out.println(clone1.toString());

        System.out.println(clone2.toString());

        System.out.println(clone3.toString());


    }
}

在这里插入图片描述

建造者模式标准写法

复杂的对象一般通过建造者模式来创建,根据设置参数不同可以构建出不同的对象出来

package com.example.prototype.build;

public class BuildPattern {
    
    
    private int max;
    private int min;
    private Long ts;

    private String operate;

    public int getMax() {
    
    
        return max;
    }

    public void setMax(int max) {
    
    
        this.max = max;
    }

    public int getMin() {
    
    
        return min;
    }

    public void setMin(int min) {
    
    
        this.min = min;
    }

    public Long getTs() {
    
    
        return ts;
    }

    public void setTs(Long ts) {
    
    
        this.ts = ts;
    }

    public String getOperate() {
    
    
        return operate;
    }

    public void setOperate(String operate) {
    
    
        this.operate = operate;
    }
}

package com.example.prototype.build;

public class BuildPatternBuilder {
    
    
    BuildPattern buildPattern=new BuildPattern();

    public BuildPattern setMax(int max){
    
    
        buildPattern.setMax(max);
        return buildPattern;
    }

    public BuildPattern setMin(int min){
    
    
        buildPattern.setMin(min);
        return buildPattern;
    }
    public BuildPattern setTs(Long ts){
    
    
        buildPattern.setTs(ts);
        return buildPattern;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_40351360/article/details/128424770