设计模式与架构03 -- 原型模式,建造者模式

2021-12-02  本文已影响0人  YanZi_33

原型模式

image.png
import androidx.annotation.NonNull;

public class Realizetype implements Cloneable{

    public Realizetype() {
        System.out.println("具体原型对象查创建成功");
    }

    @NonNull
    @Override
    public Realizetype clone() throws CloneNotSupportedException {
        System.out.println("具体原型对象复制成功");
        return (Realizetype) super.clone();
    }
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.sign.prototype.Realizetype;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Realizetype realizetype = new Realizetype();
        try {
            Realizetype clone = realizetype.clone();
            System.out.println("原型与克隆是否是同一个对象" + (realizetype == clone));
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
image.png
import androidx.annotation.NonNull;

public class Citation implements Cloneable{
    private String name;

    public String getName() {
        return name;
    }

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

    public void show() {
        System.out.println(name + "同学: 在2021年12月1日 获取三号学生,特发此状");
    }

    @NonNull
    @Override
    public Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.sign.prototype.Citation;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Citation citation = new Citation();
        try {
            Citation c1 = citation.clone();
            c1.setName("张三");
            citation.setName("李四");
            citation.show();
            c1.show();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
扩展(深克隆)
public class Student {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import androidx.annotation.NonNull;

public class Citation implements Cloneable{
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void show() {
        System.out.println(student.getName() + "同学: 在2021年12月1日 获取三号学生,特发此状");
    }

    @NonNull
    @Override
    public Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.sign.prototype.Citation;
import com.example.sign.prototype.Student;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Citation citation = new Citation();
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);

        try {
            Citation c1 = citation.clone();
            c1.getStudent().setName("李四");
            citation.show(); //李四
            c1.show(); //李四
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
import java.io.Serializable;

public class Student implements Serializable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
import androidx.annotation.NonNull;
import java.io.Serializable;

public class Citation implements Cloneable, Serializable {
    private Student student;

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public void show() {
        System.out.println(student.getName() + "同学: 在2021年12月1日 获取三号学生,特发此状");
    }

    @NonNull
    @Override
    public Citation clone() throws CloneNotSupportedException {
        return (Citation) super.clone();
    }
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.sign.prototype.Citation;
import com.example.sign.prototype.Student;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Citation citation = new Citation();
        Student student = new Student();
        student.setName("张三");
        citation.setStudent(student);

        //创建输出流
        try {
            //将对象序列化到磁盘文件
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("User/liyanyan/desktop/a.txt"));
            //写对象
            oos.writeObject(citation);
            oos.close();
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("User/liyanyan/desktop/a.txt"));
            Citation citation1 = (Citation) ois.readObject();
            ois.close();
            Student student1 = citation1.getStudent();
            student1.setName("李四");
            citation.show(); //张三
            citation1.show(); //李四
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

建造者模式

image.png
public class Bike {
    private String frame;
    private String seat;

    public String getFrame() {
        return frame;
    }

    public void setFrame(String frame) {
        this.frame = frame;
    }

    public String getSeat() {
        return seat;
    }

    public void setSeat(String seat) {
        this.seat = seat;
    }
}
//抽象建造者
public abstract class Builder {
    protected Bike bike = new Bike();

    public abstract void buildFrame();
    public abstract void buildSeat();

    public abstract Bike createBike();
}
public class MobleBuilder extends Builder{
    @Override
    public void buildFrame() {
        bike.setFrame("碳纤维车架");
    }

    @Override
    public void buildSeat() {
        bike.setSeat("真皮车座");
    }

    @Override
    public Bike createBike() {
        return bike;
    }
}
public class OfoBuilder extends Builder{
    @Override
    public void buildFrame() {
        bike.setFrame("铝合金车架");
    }

    @Override
    public void buildSeat() {
        bike.setSeat("橡胶车座");
    }

    @Override
    public Bike createBike() {
        return bike;
    }
}
//指挥者类 装配 
public class Director {
    private Builder builder;

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

    public Bike construct() {
        builder.buildFrame();
        builder.buildSeat();
        return builder.bike;
    }
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.sign.build.Bike;
import com.example.sign.build.Director;
import com.example.sign.build.MobleBuilder;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Director director = new Director(new MobleBuilder());
        Bike bike = director.construct();
        System.out.println(bike.getFrame() + bike.getSeat());
    }
}
//抽象建造者
public abstract class Builder {
    protected Bike bike = new Bike();

    public abstract void buildFrame();
    public abstract void buildSeat();

    public abstract Bike createBike();
    
    public Bike construct() {
        this.buildFrame();
        this.buildSeat();
        return this.createBike();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读