Prototype 原型模式

package com.lonton.designpatterns;

interface Prototype
{
	void setSize(int x);
	void printSize();	
}

class A implements Prototype, Cloneable
{
	private int size;
	
	public A(int x)
	{
		size = x;
	}
	
	@Override
	public void setSize(int x)
	{
		// TODO Auto-generated method stub
		this.size = x;
	}

	@Override
	public void printSize()
	{
		// TODO Auto-generated method stub
		System.out.println("Size: " + size);
	}
	
	@Override
	public A clone() throws CloneNotSupportedException
	{
		return (A)super.clone();
	}
	
}

public class PrototypeTest
{
	public static void main(String[] args) throws CloneNotSupportedException
	{
		A a = new A(1);
		
		for (int i = 2; i < 10; i++)
		{
			Prototype tem = a.clone();
			tem.setSize(i);
			tem.printSize();
		}
	}	
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章