factory模式的学习

1.Simple Factory模式:

1、 在这里,我们先定义水果(Fruit)接口:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   interface  Fruit
{
    
void  plant();

    
void  enableEat();
}

2、 苹果(Apple)是对水果(Fruit)接口的实现:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   class  Apple  implements  Fruit
{

    
public   void  enableEat()
    {
        System.out.println(
" 苹果能吃啦 " );
    }

    
public   void  plant()
    {
        System.out.println(
" 种植苹果 " );
    }
    
}

3、 葡萄(Grape)是对水果(Fruit)接口的实现:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   class  Grape  implements  Fruit
{

    
public   void  enableEat()
    {
        System.out.println(
" 葡萄能吃啦 " );
    }

    
public   void  plant()
    {
        System.out.println(
" 种植葡萄 " );
    }

}

4、 鸭梨(Pear)是对水果(Fruit)接口的实现:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   class  Pear  implements  Fruit
{

    
public   void  enableEat()
    {
        System.out.println(
" 鸭梨能吃啦! " );
    }

    
public   void  plant()
    {
        System.out.println(
" 种植鸭梨 " );
    }

}

5、定义买水果工厂(FruitFactory)这一过程类:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   class  FruitFactory
{
    
/**
     * 种植水果
     * 
     * 
@param  str
     * 
@return
     
*/
    
public   static  Fruit plantFruit(String str)
    {
        str 
=  str.toLowerCase();
        
if  (str.equals( " apple " ))
            
return   new  Apple();
        
if  (str.equals( " grape " ))
            
return   new  Grape();
        
if  (str.equals( " pear " ))
            
return   new  Pear();
        
return   null ;
    }

    
/**
     * 吃水果
     * 
     * 
@param  str
     * 
@return
     
*/
    
public   static  Fruit eatFruit(String str)
    {
        str 
=  str.toLowerCase();
        
if  (str.equals( " apple " ))
            
return   new  Apple();
        
if  (str.equals( " grape " ))
            
return   new  Grape();
        
if  (str.equals( " pear " ))
            
return   new  Pear();
        
return   null ;
    }
}

6、 编写测试类:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.simplefactory;

public   class  Client
{
    
public   static   void  main(String[] args)
    {
        Apple apple 
=  (Apple) FruitFactory.plantFruit( " apple " );
        apple.plant();
        
        Apple appleEat 
=  (Apple) FruitFactory.eatFruit( " apple " );
        appleEat.enableEat();
        
        Grape grape 
=  (Grape) FruitFactory.plantFruit( " grape " );
        grape.plant();
        
        Grape grapeEat 
=  (Grape) FruitFactory.eatFruit( " grape " );
        grapeEat.enableEat();
        
        
        Pear pear 
=  (Pear) FruitFactory.plantFruit( " pear " );
        pear.plant();
        
        Pear pearEat 
=  (Pear) FruitFactory.eatFruit( " pear " );
        pearEat.enableEat();
    }
}

7、 说明:

A:我要购买苹果,只需向工厂角色(BuyFruit)请求即可。而工厂角色在接到请求后,会自行判断创建和提供哪一个产品。

B:但是对于工厂角色(FruitFactory)来说,增加新的产品(比如说增加草莓)就是一个痛苦的过程。工厂角色必须知道每一种产品,如何创建它们,以及何时向客户端提供它们。换言之,接纳新的产品意味着修改这个工厂。

C:因此Simple Factory模式的开放性比较差。


 

有什么办法可以解决这个问题吗?那就需要Factory Method模式来为我们服务了。

二、Factory Method模式:

1、同样,我们先定义水果(Fruit)接口:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   interface  Fruit
{
    
void  plant();
}


 

2、苹果(Apple)是对水果(Fruit)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   class  Apple  implements  Fruit
{

    
public   void  plant()
    {
        System.out.println(
" plant apple " );
    }
    
}


 

3、葡萄(Grape)是对水果(Fruit)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   class  Grape  implements  Fruit
{

    
public   void  plant()
    {
        System.out.println(
" plant grape " );
    }

}


 

4、鸭梨(Pear)是对水果(Fruit)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   class  Pear  implements  Fruit
{

    
public   void  plant()
    {
        System.out.println(
" plant pear " );
    }

}


 

5、在这里我们将买水果(FruitFactory)定义为接口类:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   interface  FruitFactory
{
    Fruit plantFruit();
}


 

6、种植苹果是(PlantApple)对水果工厂(FruitFactory)这个接口的实现

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

import  com.pattern.factorymethod.FruitFactory;

public   class  PlantApple  implements  FruitFactory
{

    
public  Fruit plantFruit()
    {
        
return   new  Apple();
    }

}


 

7、种植鸭梨是(PlantBear)对水果工厂(FruitFactory)这个接口的实现

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   class  PlantPear  implements  FruitFactory
{
    
public  Fruit plantFruit()
    {
        
return   new  Pear();
    }

}


 

8、种植葡萄是(PlantGrape)对水果工厂(FruitFactory)这个接口的实现

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;

public   class  PlantGrape  implements  FruitFactory
{

    
public  Fruit plantFruit()
    {
        
return   new  Grape();
    }

}


 

9、编写测试类:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.factorymethod;
/**
 * 测试类
 * 
@author  supercrsky
 *
 
*/
public   class  Client
{
    
public   static   void  main(String[] args)
    {
        FruitFactory factory 
=   new  PlantApple();
        factory.plantFruit().plant();
        
        factory 
=   new  PlantGrape();
        factory.plantFruit().plant();
        
        factory 
=   new  PlantPear();
        factory.plantFruit().plant();
    }
}

 

10、说明:

A:工厂方法模式和简单工厂模式在结构上的不同是很明显的。工厂方法模式的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。工厂方法模式可以允许很多具体工厂类从抽象工厂类中将创建行为继承下来,从而可以成为多个简单工厂模式的综合,进而推广了简单工厂模式。

B:工厂方法模式退化后可以变得很像简单工厂模式。设想如果非常确定一个系统只需要一个具体工厂类,那么就不妨把抽象工厂类合并到具体的工厂类中去。由于反正只有一个具体工厂类,所以不妨将工厂方法改成为静态方法,这时候就得到了简单工厂模式。

C:如果需要加入一个新的水果,那么只需要加入一个新的水果类以及它所对应的工厂类。没有必要修改客户端,也没有必要修改抽象工厂角色或者其他已有的具体工厂角色。对于增加新的水果类而言,这个系统完全支持“开-闭”原则。

D:对Factory Method模式而言,它只是针对一种类别(如本例中的水果类Fruit),但如果我们还想买肉,那就不行了,这是就必须要Abstract Factory Method模式帮忙了。



 

三、Abstract Factory模式

1、同样,我们先定义水果(Fruit)接口:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   interface  Fruit
{
    
void  plant();
}

 


 

2、苹果(Apple)是对水果(Fruit)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   class  Apple  implements  Fruit
{
    
public   void  plant()
    {
        System.out.println(
" 种植苹果 " );
    }
}

 


 

2、 定义肉(Meat)接口:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   interface  Meat
{
    
void  feed();
}

 


 

3、 猪肉(PigMeat)是对肉(Meat)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   class  PigMeat  implements  Meat
{

    
public   void  feed()
    {
        System.out.println(
" 猪肉 " );
    }

}

 


 

4、 牛肉(CowMeat)是对肉(Meat)接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   class  CowMeat  implements  Meat
{

    
public   void  feed()
    {
        System.out.println(
" 牛肉 " );
    }

}

 


 

5、 我们可以定义工厂接口:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   interface  Factory
{
    
//  种水果
    Fruit plantFruit(Fruit fruit);

    
//  养肉
    Meat feedMeat(Meat meat);
}

 


 

6、 我(MyFactory)是对Factory接口的实现:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   class  MyFactory  implements  Factory
{
    
//  养肉的工厂方法
     public  Meat feedMeat(Meat meat)
    {
        
return  meat;
    }

    
//  种水果的工厂方法
     public  Fruit plantFruit(Fruit fruit)
    {
        
return  fruit;
    }
}

 

 

7、编写测试类:


<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> package  com.pattern.abstractfactory;

public   class  Client
{
    
public   static   void  main(String[] args)
    {
        Fruit apple 
=   new  Apple();
        Meat pigMeat 
=   new  PigMeat();
        Factory factory 
=   new  MyFactory();
        
//  种植苹果
        factory.plantFruit(apple);
        
//  养殖猪肉
        factory.feedMeat(pigMeat);
    }
}

 

 

8、说明:

A:抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,创建多个产品族中的产品对象。这就是抽象工厂模式的用意。

B:抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。

posted on 2008-05-23 16:03 々上善若水々 阅读(60) 评论(0)  编辑  收藏 所属分类:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值