首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

原始模型方式

2012-09-02 
原始模型模式通过一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型

原始模型模式

通过一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的办法创建出更多同类型的对象。

?

public interface Prototype extends Cloneable {    public Object clone();}

?

public class ConcreatePrototype implements Prototype {    public Object clone() {        try {            return super.clone();        } catch (CloneNotSupportedException e) {            return null;        }    }}

?

?

public class Client {    public static void main(String[] args) {        Prototype prototype = new ConcreatePrototype();        System.out.println(prototype);        Prototype prototype2 = (Prototype) prototype.clone();        System.out.println(prototype2);        /*克隆对象与原对象不是同一个对象*/        System.out.println(prototype != prototype2); //true        /*克隆对象与原对象的类型一样*/        System.out.println(prototype.getClass() == prototype2.getClass()); //true    }}

?

热点排行