本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0

package com.pdw.pattern;

interface Me{
 public void sayHello();
 public void sayBay();
}


class SampleFacotry implements Me{

 public void sayHello() {
  // TODO Auto-generated method stub
  System.out.println("sampleFacotry->SayHello....");
 }

 public void sayBay() {
  // TODO Auto-generated method stub
  System.out.println("sampleFacotry->Bay....");
 }
 
}
class PPFacotry implements Me{

 public void sayHello() {
  // TODO Auto-generated method stub
  System.out.println("PPFacotry-->"+"Say Hello");
 }

 public void sayBay() {
  // TODO Auto-generated method stub
  System.out.println("PPFacotry-->"+"Say Bay.");
 }
 
}
  /**
   * 以一般工厂方法构造类
   * @author Administrator
   *
   */
class CreateFacotry{
 public static Me createFacotry(String aa){
  if(aa.equalsIgnoreCase("Sample")){
   return new SampleFacotry();
  }else if(aa.equalsIgnoreCase("")){
   return new PPFacotry();
  }else{
   return null;
  }
 }
}
/**
 * 操象工厂的实现。
 * @author Administrator
 *
 */
abstract class CFacory{
 public abstract SampleFacotry mecreator();
 public abstract PPFacotry kkmeicreator();
}

class CFacoryImple extends CFacory{

 @Override
 public SampleFacotry mecreator() {
  // TODO Auto-generated method stub
  return new SampleFacotry();
 }

 @Override
 public PPFacotry kkmeicreator() {
  // TODO Auto-generated method stub
  return new PPFacotry();
 }
 
}

public class Facotry {
 public static void main(String[] args){
  Me a=CreateFacotry.createFacotry("Sample");
  a.sayHello();
  CFacoryImple cfi=new CFacoryImple();
  Me me=cfi.mecreator();
  Me ppme=cfi.kkmeicreator();
  me.sayBay();
  ppme.sayBay();
 }
}

以上这个例程,说明了一般工厂以及操象工厂的实现。
工厂这个构造模式在开发中会经常用到

posted on 2006-06-28 22:54 有猫相伴的日子 阅读(276) 评论(0)  编辑  收藏 所属分类: Patterns

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网