pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/VictorSSH/Java/tree/master/java-Design-pattern/prototype

70be7debc79a8eff.css" /> Java/java-Design-pattern/prototype at master · VictorSSH/Java · GitHub
Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

原型模式 ( Prototype )

用途

用原型实例指定创建对象的种类, 并且通过拷贝这些原型创建新的对象

实例

首先要联想到克隆,最经典的一个例子就是克隆羊多利了。原型模式的核心就是在已经提供的对象的基础上来构造一个新的对象

模式分析

在 Java 中,克隆操作可以通过实现 Cloneable 接口并且重写定义于 Object 类中的 clone() 方法

public Sheep implements Cloneable {
    private String name;
    public Sheep(String name){
        this.name = name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    @Override
      public Sheep clone() throws CloneNotSupportedException {
        return new Sheep(this.name);
    }
}

克隆一个羊对象可以这样操作

...
Sheep origen = new Sheep("团团");
System.out.println(origen.getName());   //团团

origen.setName("圆圆");
Sheep clone = origen.clone();
System.out.println(clone.getName());    //圆圆
...

适用场景

当一个系统应该独立于它的产品创建、 构成和表示时, 要使用Prototype模式; 以及

  • 当要实例化的类是在运行时刻指定时, 例如, 通过动态装载
  • 为了避免创建一个与产品类层次平行的工厂类层次时
  • 当一个类的实例只能有几个不同状态组合中的一种时,建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy