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
版权声明:本文为博主原创文章,未经博主允许不得转载。

Creational Patterns Part 1/5: Factory Pattern

Creational Patterns Part 1/5: Factory Pattern目录 Creational Patterns Part 15 Factory Pattern 目录 Defin...

Design Pattern - Creational Patterns - Factory Pattern

Section 1, Chapter 1 Factory Pattern Intr: Factories use a base class type to create a class ...

Design Pattern - Creational Patterns - Singleton Pattern

2007 Section 1, Chapter 1 Singleton Pattern Concept The Singleton pattern is a way t...

Design Pattern - Creational Patterns - Abstract Factory Pattern

2007 Section 2, Chapter 2 Abstract Factory Pattern Concept Since...

Design Pattern - Creational Patterns - Builder Pattern

2007 Section 3, Chapter 2 Builder Pattern Concept Builders usually deal with constru...

23种设计模式(5)_创建型_原型模式(Prototype Pattern)

用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。使用原型模式创建对象比直接new一个对象在性能上要好的多,因为Object类的clone方法是一个本地方法,它直接操作内存中的二进制流,特...

设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 原型模式(Prototype) - Java实现

设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 原型模式(Prototype) 用原型实例指定创建对象的种类,并且通过复制这些原型,创建新的对象...
  • achi010
  • achi010
  • 2017年07月02日 22:41
  • 82

Behavioral Patterns Part 2/11: Command Pattern

Behavioral Patterns Part 2/11: Command Pattern目录 Behavioral Patterns Part 211 Command Pattern 目录 Def...

Structural Patterns Part 6/7: Flyweight Pattern

Structural Patterns Part 6/7: Flyweight Pattern目录 Structural Patterns Part 67 Flyweight Pattern 目录 D...

Behavioral Patterns Part 3/11: Interpreter Pattern

Behavioral Patterns Part 3/11: Interpreter Pattern目录 Behavioral Patterns Part 311 Interpreter Patter...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Creational Patterns Part 5/5: Prototype Pattern
举报原因:
原因补充:

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