Creational Patterns Part 5/5: Prototype Pattern

原创 2016年05月31日 01:55:34

Creational Patterns Part 5/5: Prototype Pattern

目录


Definition

Prototype Pattern会根据指定的Prototype(原型)去制造(Make)而不是创建(New)一个新的对象。
要知道每次new对象是很耗时的操作。Prototype Pattern会在克隆时间点制造出一个一模一样的对象(也就是两者在那一瞬间的state一样)出来,除此之外,对象在clone出来之后与prototype并无联系。

何时使用?希望避免new对象带来的损耗时或者是创建的对象都很相似时(比如一堆围棋)。

使用频率:Frequncey of use Medium


UML Class Diagram

Prototype Pattern


Implementation

Prototype Pattern相对来说还是比较简单的,下面以拷贝一个Student类为例。
注意到student和student2仅在clone时状态完全一样,但是此后两者便是独立没有联系的(前提是深拷贝)。

// PrototypeDemo.java
package designpatterns.creationalpatterns.prototype;

public class PrototypeDemo {
    public static void main(String[] args) {
        Cellphone cp = new Cellphone("NOKIA");
        Person student = new Student("Stephen", cp);
        Person student2 = student.Clone();
        System.out.println(student);
        System.out.println(student2);
        System.out.println("========================");
        student.setCellphone(new Cellphone("iPhone"));
        System.out.println(student);
        System.out.println(student2);
    }
}

abstract class Person implements Cloneable {
    protected String name;
    protected Cellphone cellphone;

    public Person(String name, Cellphone cp) {
        this.name = name;
        this.cellphone = cp;
    }

    public String getName() {
        return name;
    }

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

    public Cellphone getCellphone() {
        return cellphone;
    }

    public void setCellphone(Cellphone cellphone) {
        this.cellphone = cellphone;
    }

    public abstract Person Clone();

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + ", " + name + ", " + cellphone;
    }
}

class Student extends Person {

    public Student(String name, Cellphone cp) {
        super(name, cp);
    }

    @Override
    public Person Clone() {
        Person person = null;       
        try {
            person = (Person) super.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }       
        return person;
    }


}

class Cellphone {
    private String brand;

    public Cellphone() {}

    public Cellphone(String brand) {
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    @Override
    public String toString() {
        return brand;
    }
}

// output
Student, Stephen, NOKIA
Student, Stephen, NOKIA
========================
Student, Stephen, iPhone
Student, Stephen, NOKIA
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/iorichang/article/details/51542519

Spring 5 Design Patterns epub

  • 2017年10月16日 09:57
  • 2.87MB
  • 下载

Creational Patterns Part 4/5: Builder Pattern

Creational Patterns Part 4/5: Builder Pattern目录 Creational Patterns Part 45 Builder Pattern 目录 Defin...
  • iorichang
  • iorichang
  • 2016-05-30 23:17:59
  • 239

Creational Patterns Part 3/5: Singleton Pattern

Creational Patterns Part 3/5: Singleton Pattern目录 Creational Patterns Part 35 Singleton Pattern 目录 D...
  • iorichang
  • iorichang
  • 2016-05-30 22:13:30
  • 195

Creational Patterns Part 1/5: Factory Pattern

Creational Patterns Part 1/5: Factory Pattern目录 Creational Patterns Part 15 Factory Pattern 目录 Defin...
  • iorichang
  • iorichang
  • 2016-05-30 16:09:43
  • 339

spring中的相关design pattern

待续。。。。
  • xuqianghit
  • xuqianghit
  • 2010-10-16 19:27:00
  • 531

Design Pattern: Prototype 模式

学习是分享和合作式的! 转载请注明出处:http://blog.csdn.net/wdzxl198/article/details/9223275; 文章摘自: http://www.riabook...
  • wdzxl198
  • wdzxl198
  • 2013-07-08 14:42:49
  • 1223

java设计模式大全 Design pattern samples in Java(最经典最全的资料)

Design pattern samples in Java.Build status:IntroductionDesign patterns are formalized best practice...
  • changemyself
  • changemyself
  • 2015-06-19 13:10:58
  • 11019

Design Pattern----Creational Patterns

设想有这么一个问题,在一个迷宫游戏中
  • ccsu_001
  • ccsu_001
  • 2014-06-20 17:14:12
  • 952

Design Pattern - Creational Patterns - Prototype Pattern

2007 Section 4, Chapter 2 Prototype Pattern Concept The Prototype pattern gives us a...
  • zlxadhkust
  • zlxadhkust
  • 2016-05-07 18:20:37
  • 350
收藏助手
不良信息举报
您举报文章:Creational Patterns Part 5/5: Prototype Pattern
举报原因:
原因补充:

(最多只允许输入30个字)