原型模式通俗易懂

首先也是遵从:是什么?干嘛用?怎么用三部曲。

相关详细概念网上铺天盖地,有些概念性的东西我就不复制了,只谈谈自己的理解和实现。

1、是什么?

根据字面意思都可以知道得差不多,原型模式,就是有一个原型实例,实现的话拷贝该原型创建新的实例,用的话就用新 的实例。他是创建型设计模式。
java类图
在这里插入图片描述

一共有三个角色

(1)客户(Client)角色
(2)抽象原型(Prototype)角色
(3)具体原型(Concrete Prototype)角色

分类
(1)浅拷贝:我们只拷贝对象中的基本数据类型(8种),对于数组、容器、引用对象等都不会拷贝

(2)深拷贝:不仅能拷贝基本数据类型,还能拷贝那些数组、容器、引用对象等

2、干嘛用?

1)当我们的类初始化需要消耗很多的资源时,就可以使用原型模式,因为我们的克隆不会执行构造方法,避免了初始化占有的时间和空间。

(2)一个对象被其她对象访问,并且能够修改时,访问权限都无效了,什么都能修改。

3、怎么用?

(1)jdk的clone方法
实现Cloneable 接口
如果有复杂对象重写clone方法,然后克隆复杂对象
实现具体原型方法
开始克隆

4、使用案例

上帝造人
首先创建一个抽象人,就是具体人的原型。
然后开始克隆

在这里插入图片描述

原型人

public class BaseHumans implements Cloneable {
    private Map<String, String> eyes;
    private String mouth;
    private String head;
    private Map<String, String> hands;
    private Map<String, String> foots;
    private long height;
    //....

    public Map<String, String> getEyes() {
        return eyes;
    }

    public void setEyes(Map<String, String> eyes) {
        this.eyes = eyes;
    }

    public String getMouth() {
        return mouth;
    }

    public void setMouth(String mouth) {
        this.mouth = mouth;
    }

    public String getHead() {
        return head;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public Map<String, String> getHands() {
        return hands;
    }

    public void setHands(Map<String, String> hands) {
        this.hands = hands;
    }

    public Map<String, String> getFoots() {
        return foots;
    }

    public void setFoots(Map<String, String> foots) {
        this.foots = foots;
    }

    public long getHeight() {
        return height;
    }

    public void setHeight(long height) {
        this.height = height;
    }

    public BaseHumans(Map<String, String> eyes, String mouth, String head, Map<String, String> hands, Map<String, String> foots, long height) {
        this.eyes = eyes;
        this.mouth = mouth;
        this.head = head;
        this.hands = hands;
        this.foots = foots;
        this.height = height;
    }

    @Override
    public String toString() {
        return "BaseHumans{" +
                "eyes=" + eyes +
                ", mouth='" + mouth + '\'' +
                ", head='" + head + '\'' +
                ", hands=" + hands +
                ", foots=" + foots +
                ", height=" + height +
                '}';
    }

    public BaseHumans clone() {
        BaseHumans baseHumans = null;

        try {
            baseHumans = (BaseHumans) super.clone();
            baseHumans.setEyes((Map<String, String>) ((HashMap) this.eyes).clone());
            if (this.hands != null) {
                baseHumans.setHands((Map<String, String>) ((HashMap) this.hands).clone());
            }
            if (this.foots != null) {
                baseHumans.setFoots((Map<String, String>) ((HashMap) this.foots).clone());
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return baseHumans;
    }

}

男人原型

import java.util.Map;

public class Men extends BaseHumans {
    //
    private String features = "有鸡鸡";

    public Men(Map<String, String> eyes, String mouth, String head, Map<String, String> hands, Map<String, String> foots, long height) {
        super(eyes, mouth, head, hands, foots, height);
        System.out.println("我是原型人构造方法");
    }

    public String getFeatures() {
        return features;
    }

    public void setFeatures(String features) {
        this.features = features;
    }

    public void show(String name) {
        System.out.println(name+"人的信息");
        System.out.println(this.getFeatures());
        System.out.println(this.getEyes());
    }
}

女人的原型

public class Women extends BaseHumans {
    private String features = "没鸡鸡";

    public Women(Map<String, String> eyes, String mouth, String head, Map<String, String> hands, Map<String, String> foots, long height) {
        super(eyes, mouth, head, hands, foots, height);
        System.out.println("我是原型人构造方法");
    }

    public String getFeatures() {
        return features;
    }

    public void setFeatures(String features) {
        this.features = features;
    }

    public void show(String name) {
        System.out.println(name+"人的信息");
        System.out.println(this.getFeatures());
        System.out.println(this.getEyes());
    }
}

上帝开始造人

import java.util.HashMap;
import java.util.Map;

public class CreateHuman {
    public static void main(String[] args) {
        //男人原型
        Map<String, String> eyes = new HashMap<>();
        eyes.put("1", "左眼");
        eyes.put("2", "右眼");
        String mouth = "大嘴巴";
        Men men = new Men(eyes, mouth, null, null, null, 170);
        for (int i = 0; i < 10; i++) {
            Men men1 = (Men) men.clone();
            men1.show(i + "号人");
        }

        Women women = new Women(eyes, mouth, null, null, null, 170);
        for (int i = 0; i < 10; i++) {
            Women women1 = (Women) women.clone();
            women1.show(i + "号人");
        }

    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值