积少成多

垃圾堆

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks
工厂模式的产品角色和简单工厂并没有区别。变化就是在工厂角色那。将具体创建产品的工作放到子工厂进行。这样就便于进行扩展了。
public
 interface IProduct {
    
public void desc();
}

public class ProductA implements IProduct {
    
public void desc() {
        
//excute;
    }
}

public class ProductB implements IProduct {
    
public void desc() {
        
//excute;
    }
}

public interface IFactory {
    
public IProduct factory();
}

public class FactoryA implements IFactory {
    
public IProduct factory() {
        
return new ProductA();
    }
}

public class FactoryB implements IFactory {
    
public IProduct factory() {
        
return new ProductB();
    }
}

个人觉得如果工厂的factory方法如果是静态的也可以阿。为什么不呢?但是接口是无法申明static的方法的。只能用类或抽象类来做。不知道还有没有更好的方法。
public abstract class IFactory {
    
public static IProduct factory(){return null;};
}

public class FactoryA extends IFactory {
    
public static IProduct factory() {
        
return new ProductA();
    }
}

public class FactoryB extends IFactory {
    
public static IProduct factory() {
        
return new ProductB();
    }
}


posted on 2011-05-27 10:53 思无 阅读(184) 评论(0)  编辑  收藏 所属分类: 模式

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


网站导航: