设计模式之原型模式(prototype)

来源:互联网 发布:雅思口语网课推荐知乎 编辑:程序博客网 时间:2024/06/04 18:37
意图:
用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象.

适用性:
1.当要实例化的类是在运行时指定时
2.为了避免创建一个与产品类层次平行的工厂类层次时
3.当一个类的实例只能有几个不同状态组合中的一种时.

效果:
1.运行时刻增加和删除产品.
2.改变值以指定新对象
3.改变结构以指定新对象
4.减少子类的构造
5.用类动态配置应用



代码实现:

Java中原型模式有两种实现及浅拷贝和深拷贝,Object中默认实现了浅拷贝.


1.浅拷贝实现


package com.git.books.b_design_patterns.d_prototype.one;/** *  * @Description: 浅拷贝父类 * @author: songqinghu * @date: 2017年2月27日 下午7:23:32 * Version:1.0 */public interface PrototypeCar extends Cloneable {    public abstract void run();}


package com.git.books.b_design_patterns.d_prototype.one;/** *  * @Description: bmwcar 实体类 * @author: songqinghu * @date: 2017年2月27日 下午7:25:33 * Version:1.0 */public class PrototypeBMWCar implements PrototypeCar {    private String brand ;    private BMWCarEngine engine ;    public PrototypeBMWCar() {        brand = "bmw";        engine = new BMWCarEngine();        engine.setSound("wuwuwu~~");    }    @Override    public void run() {        System.out.println("the brand is : " + brand + " and the engine sound is : " + engine.getSound());    }    public String getBrand() {        return brand;    }    public void setBrand(String brand) {        this.brand = brand;    }    public BMWCarEngine getEngine() {        return engine;    }    public void setEngine(BMWCarEngine engine) {        this.engine = engine;    }    @Override    protected Object clone() {        Object car = null;        try {            car =  (Object) super.clone();        } catch (CloneNotSupportedException e) {           System.out.println("copy occor error!");        }        return car;    }}


package com.git.books.b_design_patterns.d_prototype.one;/** *  * @Description: 宝马车引擎 * @author: songqinghu * @date: 2017年2月27日 下午7:29:39 * Version:1.0 */public class BMWCarEngine {    private String sound;    public String getSound() {        return sound;    }    public void setSound(String sound) {        this.sound = sound;    }}


package com.git.books.b_design_patterns.d_prototype.one;/** *  * @Description: 浅复制原型模式调用类 * @author: songqinghu * @date: 2017年2月27日 下午7:36:33 * Version:1.0 */public class SimplePrototypeTest {    public static void main(String[] args) {        PrototypeBMWCar oneCar = new PrototypeBMWCar();        PrototypeBMWCar twoCar = (PrototypeBMWCar) oneCar.clone();        oneCar.run();        twoCar.run();        System.out.println(oneCar.getBrand() == twoCar.getBrand());        System.out.println(oneCar == twoCar);        System.out.println(oneCar.getEngine() == twoCar.getEngine());    }}

运行结果:

the brand is : bmw and the engine sound is : wuwuwu~~the brand is : bmw and the engine sound is : wuwuwu~~truefalsetrue


2.深拷贝实现


package com.git.books.b_design_patterns.d_prototype.one;import java.io.Serializable;/** *  * @Description: 浅拷贝父类 * @author: songqinghu * @date: 2017年2月27日 下午7:23:32 * Version:1.0 */public interface PrototypeCar extends Cloneable,Serializable {    public abstract void run();}


package com.git.books.b_design_patterns.d_prototype.one;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;/** *  * @Description: bmwcar 实体类 * @author: songqinghu * @date: 2017年2月27日 下午7:25:33 * Version:1.0 */public class PrototypeBMWCar implements PrototypeCar {    /**     *      */    private static final long serialVersionUID = -3622417086664448880L;    private String brand ;    private BMWCarEngine engine ;    public PrototypeBMWCar() {        brand = "bmw";        engine = new BMWCarEngine();        engine.setSound("wuwuwu~~");    }    @Override    public void run() {        System.out.println("the brand is : " + brand + " and the engine sound is : " + engine.getSound());    }    public String getBrand() {        return brand;    }    public void setBrand(String brand) {        this.brand = brand;    }    public BMWCarEngine getEngine() {        return engine;    }    public void setEngine(BMWCarEngine engine) {        this.engine = engine;    }    @Override    protected Object clone() {        Object car = null;        try {            car =  (Object) super.clone();        } catch (CloneNotSupportedException e) {           System.out.println("copy occor error!");        }        return car;    }    /**     *      * @描述:流之深度拷贝     * @return Object     * @exception     * @createTime:2017年2月27日     * @author: songqinghu     */    protected Object deepClone(){        Object car =null;        ByteArrayOutputStream bo = new ByteArrayOutputStream();        ObjectOutputStream out;        try {            out = new ObjectOutputStream(bo);            out.writeObject(this);            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());            ObjectInputStream oi = new ObjectInputStream(bi);            car  = oi.readObject();        } catch (IOException e) {            e.printStackTrace();        }finally{            return car;        }    }}


package com.git.books.b_design_patterns.d_prototype.one;import java.io.Serializable;/** *  * @Description: 宝马车引擎 * @author: songqinghu * @date: 2017年2月27日 下午7:29:39 * Version:1.0 */public class BMWCarEngine implements Serializable{    /**     *      */    private static final long serialVersionUID = -8855588455186407728L;    private String sound;    public String getSound() {        return sound;    }    public void setSound(String sound) {        this.sound = sound;    }}

package com.git.books.b_design_patterns.d_prototype.one;/** *  * @Description: 浅复制原型模式调用类 * @author: songqinghu * @date: 2017年2月27日 下午7:36:33 * Version:1.0 */public class SimplePrototypeTest {    public static void main(String[] args) {        PrototypeBMWCar oneCar = new PrototypeBMWCar();        PrototypeBMWCar twoCar = (PrototypeBMWCar) oneCar.deepClone();        oneCar.run();        twoCar.run();        System.out.println(oneCar.getBrand() == twoCar.getBrand());        System.out.println(oneCar == twoCar);        System.out.println(oneCar.getEngine() == twoCar.getEngine());    }}

运行结果:

the brand is : bmw and the engine sound is : wuwuwu~~the brand is : bmw and the engine sound is : wuwuwu~~falsefalsefalse


结构图:




重点:

区分什么什么实用使用浅拷贝和深拷贝及实现条件的区别.


参考:

<<设计模式>>
<<Java与模式>>















0 0
原创粉丝点击