代码改变世界

Android Ap 开发 设计模式第六篇:原型模式

2011-07-14 21:26  Terry_龙  阅读(2559)  评论(2编辑  收藏  举报

  Prototype Pattern

名称由来

  不是利用类来产生实例对象,而是从一个对象实例产生出另一个新的对象实例 ,根据被视为原型的对象实例 ,建立起的另一个新的对象实例就称为原型模式(Ptototype Pattern)。

 

需求场景

 

  • 种类过多无法整合成类时
    第一种情形:若须处理的对象种类太多,如果要一个个设成不同的类,须产生大量的源程序文件
  • 不容易利用类产生对象实例时
    第二种情形:该对象的实例产生过程太复杂,很难利用类来建立。
  • 希望把框架和所产生的对象实例分开时
    第三种情形:希望让产生对象实例时的框架不会只存在于某些特殊类时。

功能演示

 

  测试程序类结构图如下:

  模式UML图中,共有三个类:

  • 接口,Product 己声明抽象方法use 和 createClone的接口
  • 管理类,Manager 利用createClone 复制对象实例的类
  • 普通类,MessageBox 把字符串框起来use 的类,实现Product的createClone 和use 方法。

  Product 接口

 

public interface Product extends Cloneable{

    
public abstract String use(String s);
    
    
public abstract Product createClone();
}

  有了Product 接口 ,才可能进行复制。这个接口继承java.lang.Cloneable 接口 ,利用clone 方法可自动对实现此接口 的类进行复制。

  use 方法是“使用”的部分,“使用”代表何种意义则需看子类的实现。

  createClone方法负责复制对象实例的部分。

  
  Manager类

 

public class Manager {

    
private Hashtable<String, Product> showCase = new Hashtable<String, Product>();

    
public void register(String name, Product proto) {
        showCase.put(name, proto);
    }

    
public Product create(String protoName) {
        Product p 
= showCase.get(protoName);
        
return p.createClone();
    }
}

  唯一跟其他类沟通的桥梁,负责添加和负责对象的实例。

 

  MessageBox类

 

public class MessageBox implements Product {

    
private char decochar;

    
public MessageBox(char dechar) {
        
this.decochar = dechar;
    }

    @Override
    
public Product createClone() {
        
// TODO Auto-generated method stub
        Product p = null;
        
try {
            p 
= (Product) clone();
        } 
catch (CloneNotSupportedException e) {
            
// TODO: handle exception
            e.printStackTrace();
        }

        
return p;
    }

    @Override
    
public String use(String s) {
        
// TODO Auto-generated method stub
        int length = s.getBytes().length;
        StringBuffer sb 
= new StringBuffer();
        
for (int i = 0; i < length + 4; i++) {
            sb.append(decochar);
        }
 
        sb.append(decochar 
+ "\n\"" + s + "\"\n" + decochar);

        
for (int i = 0; i < length + 4; i++) {
            sb.append(decochar);
        }
        sb.append(
"");
        
return sb.toString();

    }

}

 

  createClone 方法是复制本身的方法,这里所调用的clone 方法规定在java 语言规格内,它会产生另一个分身(本身的复制品)。在产生复制品时,若该字段包含对象实例,则其值也会跟着复制到新的对象实例。能利用clone 方法进行复制的只限于实现java.lang.Cloneable 接口 的类。如无实现则会报异常。但MessageBox 本身实现了Product ,而Product 是己经继承了Cloneable接口。

  Java 语言的clone 方法只能从本身的类(含子类)调用,所以如果是其他类的要求而进行复制时,则须另以其他方法(如createClone)把clone 抓起来。

 

  android 界面入口测试该原型 Prototype 的PrototypeActivity

 

public class PrototypeActivity extends Activity {
    
/** Called when the activity is first created. */
    @Override
    
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button) findViewById(R.id.Button01))
                .setOnClickListener(
new OnClickListener() {

                    @Override
                    
public void onClick(View v) {
                        
// TODO Auto-generated method stub

                        Manager manager 
= new Manager();

                        MessageBox messageBox 
= new MessageBox('*');
                        manager.register(
"prototype", messageBox);

                        Product product 
= manager.create("prototype");
                        ((EditText) findViewById(R.id.EditText01))
                                .setText(product.use(
"android ap Pattern"));
                    }
                });
    }
}

 

运行效果如下:

 

源码下载:

原型模式

 

 再发个求职信息:

  求职android(可胜任Framework及 以上工作。一年半经验) 、Windows Phone 7(软件、XNA) 开发工作。