⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 设计模式描述设计模式描述设计模式描述设计模式描述
💻 JAVA
字号:
/*
 * Created on 2005-5-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.test.pattern.create.prototype;

/**
 * @author Administrator
 * 
 * 意图 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。 
 * 适用性 当要实例化的类是在运行时刻指定时,例如,通过动态装载;
 * 或者为了避免创建一个与产品类层次平行的工厂类层次时;
 * 或者当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。
 *  
 */
abstract class AbstractPrototype {
	abstract public AbstractPrototype CloneYourself();
}

// This is a sample object

class MyPrototype extends AbstractPrototype {
	public AbstractPrototype CloneYourself() {
		AbstractPrototype x=null;
		try{
			x=(AbstractPrototype)this.clone();
		}catch(Exception e){
			
		}
		return x;
	}
	// lots of other functions go here!
}

// This is the client piece of code which instantiate objects
// based on a prototype.

class Demo {
	private AbstractPrototype internalPrototype;

	public void SetPrototype(AbstractPrototype thePrototype) {
		internalPrototype = thePrototype;
	}

	public void SomeImportantOperation() {
		// During Some important operation, imagine we need
		// to instantiate an object - but we do not know which. We use
		// the predefined prototype object, and ask it to clone itself.

		AbstractPrototype x;
		x = internalPrototype.CloneYourself();
		// now we have two instances of the class which as as a prototype
	}
}

/// <summary>
/// Summary description for Client.
/// </summary>

public class Client {
	public static void main(String[] args) {
		Demo demo = new Demo();
		AbstractPrototype clientPrototype = new MyPrototype();
		demo.SetPrototype(clientPrototype);
		demo.SomeImportantOperation();

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -