工厂模式

简单工厂模式,说白了就是专门弄一个工厂类,根据传入的参数决定创建哪一种产品类。因为很多时候,在编码时不能预见需要创建哪一种类实例。

// 产品接口
public interface Product {    
    public void getName();    
}    

// 具体产品A
public class ProductA implements Product {    
    public void getName() {    
        System.out.println("  I am ProductA  ");    
    }
}

// 具体产品B
public class ProductB implements Product {    
    public void getName() {    
        System.out.println("  I am ProductB  ");    
    }
}    

// 工厂类(关键所在)    
public class ProductCreator {    
    public Product createProduct(String type) {    
        if ("A".equals(type)) {    
            return new ProductA();    
        }    
        if ("B".equals(type)) {    
            return new ProductB();    
        } else   
            return null;    
    }

    public static void main(String[] args) {    
        ProductCreator creator = new ProductCreator();    
        creator.createProduct("A").getName();    
        creator.createProduct("B").getName();    
    }    
}   

当每个抽象产品都有多个具体子类的时候,工厂角色怎么知道实例化哪一个子类呢?

抽象工厂模式,可以说是简单工厂模式的扩展,主要的区别在于需要创建对象的复杂程度上。

有一个抽象的Factory类(可以是抽象类和接口),这个类只制定一些规范,具体的产品生产工作由其子类去完成。在这个模式中,工厂类和产品类往往可以依次对应。即一个抽象工厂对应一个抽象产品,一个具体工厂对应一个具体产品,这个具体的工厂就负责生产对应的产品。

//  产品 Plant接口          
public interface Plant {    
}    

// 具体产品PlantA,PlantB    
public class PlantA implements Plant {    
    public PlantA() {    
        System.out.println(" create PlantA ! ");    
    }    
   
    public void doSomething() {    
        System.out.println("  PlantA do something  ");    
    }    
}    
   
public class PlantB implements Plant {    
    public PlantB() {    
        System.out.println(" create PlantB ! ");    
    }    
   
    public void doSomething() {    
        System.out.println("  PlantB do something  ");    
    }    
}    
   
// 产品 Fruit接口    
public interface Fruit {    
}    
   
// 具体产品FruitA,FruitB    
public class FruitA implements Fruit {    
    public FruitA() {    
        System.out.println(" create FruitA ! ");    
    }    
   
    public void doSomething() {    
        System.out.println("  FruitA do something  ");    
    }    
}    
   
public class FruitB implements Fruit {    
    public FruitB() {    
        System.out.println(" create FruitB ! ");    
    }    
   
    public void doSomething() {    
        System.out.println("  FruitB do something  ");    
    }    
}    
   
// 抽象工厂方法(关键所在)    
public interface AbstractFactory {    
    public Plant createPlant();    
   
    public Fruit createFruit();    
}    
   
// 具体工厂方法  
public class FactoryA implements AbstractFactory {    
    public Plant createPlant() {    
        return new PlantA();    
    }    
   
    public Fruit createFruit() {    
        return new FruitA();    
    }    
}    
   
public class FactoryB implements AbstractFactory {    
    public Plant createPlant() {    
        return new PlantB();    
    }    
   
    public Fruit createFruit() {    
        return new FruitB();    
    }    
}  

(2014-05-14 晚)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值