前面提到抽象工厂的实现,这里说说抽象工厂的原型实现,与工厂方法的实现不同,原型实现有他自己的优点和缺点

原型的优点:

1. 效率:clone是native方法,比new的效率高,当使用复杂循环嵌套对象是尤为明显

2. 不需要为每一个新增的product创造一个具体factory

缺点:

1. 如果需要深复制,则需要手写所有包含循环引用对象的类的clone方法,因为需要手动在clone方法里对引用对象进行clone,否则是浅复制

2. 当需要改变一个克隆实例内部的值时,由于clone方法是无参方法,只能在clone完以后手动调用改变值得方法,且如果这个值是一个类的引用,则克隆应该建立在深复制的前提下,否则会污染原型数据

3. 原型工厂有可能被初始化成不兼容组件的组合,例如 CatHead和DogBody的组合,实际上这也是原型的一个灵活性的体现之一

4. 就Java而言,它的clone方法是protected方法,我们需要手动实现Clonable接口以后重写clone()方法将其改为public方法才能调用

下面是一个例子

工厂类,不在需要具体的工厂子类,而是通过构造方法设置原型来产生不同的工厂

package factory;

import product.Body;
import product.Eye;
import product.Head; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:33
* To change this template use File | Settings | File Templates.
*/
public class ModuleFactory {
private static ModuleFactory factory = new ModuleFactory();
private static Head _head; // 原型组件
private static Body _body;
private static Object lockObj = new Object(); // 设置原型
public static ModuleFactory createFactory(Head newHead, Body newBody) {
if (newHead == null || newBody == null)
throw new RuntimeException("Param newHead and newBody cannot be null"); // 为简单起见,不考虑多线程问题,不使用延迟加载
_head = newHead;
_body = newBody; return factory;
} public Head createHead() throws CloneNotSupportedException {
Head head = (Head) _head.clone();
return head;
} // 重载方法
public Head createHead(Eye eye) throws CloneNotSupportedException {
Head head = (Head) _head.clone();
// 必须在clone之后手动调用修改成员的方法
head.setEye(eye);
return head;
} public Body createBody() throws CloneNotSupportedException {
return (Body) _body.clone();
}
}

产品类

package product;

/**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:24
* To change this template use File | Settings | File Templates.
*/
public abstract class Head implements Cloneable {
private Eye eye = new Eye("defaultEye"); public Eye getEye() {
return eye;
} public void setEye(Eye eye) {
this.eye = eye;
} @Override
public Object clone() throws CloneNotSupportedException {
Head head = (Head) super.clone();
// 对内部对象进行深复制
head.setEye((Eye) eye.clone());
return head;
} public abstract void eat();
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:29
* To change this template use File | Settings | File Templates.
*/
public class DogHead extends Head {
@Override
public void eat() {
System.out.println("A dooog's head is eating with its eye " + getEye().getName());
}
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:29
* To change this template use File | Settings | File Templates.
*/
public class CatHead extends Head {
@Override
public void eat() {
System.out.println("A caaat's head is eating fast");
}
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:48
* To change this template use File | Settings | File Templates.
*/
public class Eye implements Cloneable{
private String name; @Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
} public Eye(String name) {
this.name = name;
} public void setName(String name) {
this.name = name;
} public String getName() {
return name;
}
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:24
* To change this template use File | Settings | File Templates.
*/
public abstract class Body implements Cloneable {
public abstract void dance(); // 此处必须将clone方法重新声明为public方法
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:27
* To change this template use File | Settings | File Templates.
*/
public class CatBody extends Body {
@Override
public void dance() {
System.out.println("A caaat's body is dancing crazily!!");
}
} package product; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:27
* To change this template use File | Settings | File Templates.
*/
public class DogBody extends Body {
@Override
public void dance() {
System.out.println("A dooog's body is dancing!");
}
}

测试类

import factory.ModuleFactory;
import product.*; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-8-1
* Time: 上午12:35
* To change this template use File | Settings | File Templates.
*/
public class Test { // 通过原型工厂克隆得到新的实例
// 与具体实现无关的解耦代码
private static void act(ModuleFactory factory) throws CloneNotSupportedException {
Head head = factory.createHead();
Body body = factory.createBody();
head.eat();
body.dance();
} public static void main(String[] args) throws CloneNotSupportedException {
Head head = new DogHead();
head.getEye().setName("customEye");
ModuleFactory factory = ModuleFactory.createFactory(head, new DogBody());
factory.createHead().eat(); // 测试深浅复制
Head head2 = (Head) head.clone();
head2.getEye().setName("smallEye");
head2.eat();
head.eat(); // 这里的输出发现eye并没有变成smallEye,所以可以确定eye是深复制 System.out.println();
factory = ModuleFactory.createFactory(new CatHead(), new CatBody());
act(factory);
}
}

输出

A dooog's head is eating with its eye customEye
A dooog's head is eating with its eye smallEye
A dooog's head is eating with its eye customEye A caaat's head is eating fast
A caaat's body is dancing crazily!!

从原型模式(Prototype Pattern)到 Clone的更多相关文章

  1. Net设计模式实例之原型模式( Prototype Pattern)

    一.原型模式简介(Brief Introduction) 原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. Specify the kin ...

  2. 二十四种设计模式:原型模式(Prototype Pattern)

    原型模式(Prototype Pattern) 介绍用原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象.示例有一个Message实体类,现在要克隆它. MessageModel usin ...

  3. 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)

    原文:乐在其中设计模式(C#) - 原型模式(Prototype Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 原型模式(Prototype Pattern) 作者:weba ...

  4. 设计模式系列之原型模式(Prototype Pattern)——对象的克隆

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  5. 六个创建模式之原型模式(Prototype Pattern)

    定义: 使用原型实例指定创建对象的种类,并通过拷贝这个原型的属性创建新的对象. 结构图: Prototype:抽象原型类,声明克隆方法的接口,并是所有原型类的公共父类.在Java中,Object类为该 ...

  6. 原型模式(Prototype Pattern)

    用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象. 它通过复制一个已经存在的实例来返回新的实例,而不是新建实例.被复制的实例就是我们所称的原型,这个原型是可定制的. 原型模式多用于创建复杂 ...

  7. C#设计模式——原型模式(Prototype Pattern)

    一.概述 在软件开发中,经常会碰上某些对象,其创建的过程比较复杂,而且随着需求的变化,其创建过程也会发生剧烈的变化,但他们的接口却能比较稳定.对这类对象的创建,我们应该遵循依赖倒置原则,即抽象不应该依 ...

  8. 2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)

    案例: 某即时战略游戏,你训练出来各种很强的战士. 为了增加游戏的可玩性,增加了一种复制魔法.实施该魔法,可以复制任意的战士. 你会怎样考虑这个设计? 在继续阅读之前,请先认真思考并写出你的设计,这样 ...

  9. 原型模式(Prototype Pattern)--对象的克隆

    定义:使用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象; 原型类的核心在于如何实现克隆方法: 能够实现克隆的Java类必须实现一个标识接口Cloneable,表示这个类支持被复制; 通 ...

随机推荐

  1. USACO 6.4 Electric Fences

    Electric FencesKolstad & Schrijvers Farmer John has decided to construct electric fences. He has ...

  2. loadrunner 脚本中文乱码

    loadrunner 脚本中文乱码 1.新建脚本--->选择协议(Http)-->选项-->高级-->选择“支持字符集”并点选“UTF-8”: 2.在回放脚本之前:Vuser- ...

  3. caffe for python

    导言 本教程中,我们将会利用Caffe官方提供的深度模型——CaffeNet(该模型是基于Krizhevsky等人的模型的)来演示图像识别与分类.我们将分别用CPU和GPU来进行演示,并对比其性能.然 ...

  4. Logstash 本地安装plugin

    plugin的gems仓库 下载地址:仓库地址 1.安装ruby环境 yum install ruby yum install rubygems 2.下载插件包 下载插件的方式有多种 2.1 wget ...

  5. DRUID控制

    @Configuration public class DruidConfiguration { @Bean public ServletRegistrationBean statViewServle ...

  6. wpf企业应用之SelectButton(用于列表页之类的选择)

    在企业级应用中,通常我们会遇到这样的需求,需要点击一个按钮选择列表中的一项或者多项,然后将结果显示到按钮中.这里我给自己的控件命名为SelectButton,具体效果见 wpf企业级开发中的几种常见业 ...

  7. [BZOJ4811][YNOI2017]由乃的OJ(树链剖分+线段树)

    起床困难综合症那题,只要从高往低贪心,每次暴力跑一边看这一位输入0和1分别得到什么结果即可. 放到序列上且带修改,只要对每位维护一个线段树,每个节点分别记录0和1从左往右和从右往左走完这段区间后变成的 ...

  8. Template(Updating)

    1.Splay (Tyvj1728) #include <bits/stdc++.h> using namespace std; ,INF=<<; ],sz[MAXN],cnt ...

  9. vijos p1768 数学

    链接:点我 预处理:b[i][j]表示a[1] ... a[j]中比a[i]小的数的数量. 设 int get_lower_count(int b[], int l, int r) { return ...

  10. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...