当前位置:首页 > 设计模式 > 正文内容

原型模式

canca19年前 (2007-03-26)设计模式500

//原型模式
//CopyRight(C) CAnca Software Office.2006
//Created by CAnca.

public class PrototypeMode{
 public static void main(String[] args){
  AbstractSpoon spoon1 = new SoupSpoon();
  AbstractSpoon spoon = new SaladSpoon();
  print(spoon1.getName());
  print(spoon.getName());
  
  AbstractSpoon s1 = (AbstractSpoon)spoon1.clone();
  print(s1.getName());
 }
 
 public static void print(String str){
  System.out.println(str);
 }
}

abstract class AbstractSpoon implements Cloneable{
 String spoonName;
 public void setName(String strName){
  this.spoonName = strName;
 }
 
 public String getName(){
  return spoonName;
 }
 
 public Object clone(){
  Object object = null;
  try{
   object = super.clone();
  }catch (CloneNotSupportedException exception) {
   System.err.println("AbstractSpoon is not Cloneable");
  }
  return object;
 }
}

class SoupSpoon extends AbstractSpoon
{
 public SoupSpoon()
 {
  setName("Soup Spoon");
 }
}

class SaladSpoon extends AbstractSpoon
{
 public SaladSpoon()
 {
  setName("Salad Spoon");
 }
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/665.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:建造者模式

下一篇:代理模式

“原型模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

建造者模式

//建造者模式//CopyRight(C)CAnca Software Office.//Created by CAnca. import java.util.*; public class BuilderMode{ public static void main(String[] arg...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

适配器模式

//适配器模式//CopyRight(C)CAnca Software Office.//Created by CAnca. public class AdapterMode{ public static void main(String[] args){  Adapt...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。