【转】静态工厂模式static factory method(简单工厂模式 simple facotry)

一、静态工厂方法模式(简单工厂模式)

为水果类声明了一个接口,表现在代码上:

 1 public   interface  Fruit {
 2      // 生长
 3       void  grow();
 4       // 收获
 5       void  harvest();
 6       // 种植
 7       void  plant();
 8 }

 9
10

 

package  fac;

public   class  Apple  implements  Fruit { // 通过implements实现接口Fruit
     private   int  treeAge;
    
    
public   void  grow() {
        log(
" Apple is growing " );
    }

    
    
public   void  harvest() {
        log(
" Apple has been harvested " );
    }

    
    
public   void  plant() {
        log(
" Apple ha been planted " );
    }

    
    
public   static   void  log(String msg) {
        System.out.println(msg);
    }

    
    
public   int  getTreeAge() {
        
return  treeAge;
    }

    
    
public   void  setTreeAge( int  treeAge) {
        
this .treeAge = treeAge;
    }

}

package  fac;

public   class  Grape  implements  Fruit {
    
private boolean seedless;
    
public void grow(){
        log(
"Grape is growing.");
    }

    
    
public void harvest(){
        log(
"Grape has been harvested");
    }

    
    
public void plant(){
        log(
"Grape ha been planted");
    }

    
    
public static void log(String msg){
        System.out.println(msg);
    }


    
public boolean isSeedless() {
        
return seedless;
    }


    
public void setSeedless(boolean seedless) {
        
this.seedless = seedless;
    }

    

}
package  fac;

public   class  Strawberry  implements  Fruit {
    
public void grow(){
        log(
"Strawberry is growing");
    }

    
    
public void harvest(){
        log(
"Strawberry has been harvested");
    }

    
    
public void plant(){
        log(
"Strawberry has been planted");
    }

    
    
public static void log(String msg){
        System.out.println(msg);
    }

}
package  fac;

public   class  FruitGardener {
    
public static Fruit factory(String which)throws Exception{
        
if(which.equalsIgnoreCase("apple")){
            
return new Apple();
        }
else if(which.equalsIgnoreCase("strawberry")){
            
return new Strawberry();
        }
else if (which.equalsIgnoreCase("grape")){
            
return new Grape();
        }
else{
            
throw new Exception("Bad fruit request");
        }

    }

}
package  fac;

public   class  People  {

    
public static void main(String[] args) throws Exception {
        FruitGardener fg
=new FruitGardener();
        Fruit ap
=fg.factory("Apple");
        ap.grow();
        Fruit gp
=fg.factory("Grape");
        gp.plant();
        
        Fruit dd
=fg.factory("ddd");//抛出Bad fruit request异常
    }
    

}


(注:以上代码在JDK5.0,Myeclise3.2下编译通过)


理解了以下两个例子,再来看第三个例子:
注意对比以下三个实例的不同
实例1:

package  org.jzkangta.factorydemo01;
// 定义接口
interface  Car {
    
public void run();
    
public void stop();
}

// 具体实现类
class  Benz  implements  Car {
    
public void run(){
        System.out.println(
"Benz开始启动了。。。。。");
    }

    
public void stop(){
        System.out.println(
"Benz停车了。。。。。");
    }

}

// 具体实现类
class  Ford  implements  Car {
    
public void run(){
        System.out.println(
"Ford开始启动了。。。");
    }

    
public void stop(){
        System.out.println(
"Ford停车了。。。。");
    }

}

// 工厂
class  Factory {
    
public static Car getCarInstance(){
        
return new Ford();
    }

}

public   class  FactoryDemo01  {

    
public static void main(String[] args) {
        Car c
=Factory.getCarInstance();
        c.run();
        c.stop();

    }


}


实例二:

package  fac;


// 定义接口
interface  Car {
    
public void run();
    
public void stop();
}

// 具体实现类
class  Benz  implements  Car {
    
public void run(){
        System.out.println(
"Benz开始启动了。。。。。");
    }

    
public void stop(){
        System.out.println(
"Benz停车了。。。。。");
    }

}


class  Ford  implements  Car {
    
public void run(){
        System.out.println(
"Ford开始启动了。。。");
    }

    
public void stop(){
        System.out.println(
"Ford停车了。。。。");
    }

}

// 工厂
class  Factory {
    
public static Car getCarInstance(String type){
        Car c
=null;
        
if("Benz".equals(type)){
            c
=new Benz();
        }

        
if("Ford".equals(type)){
            c
=new Ford();
        }

        
return c;
    }

}



public   class  FactoryDemo02  {

    
public static void main(String[] args) {
        Car c
=Factory.getCarInstance("Benz");
        
if(c!=null){
            c.run();
            c.stop();
        }
else{
            System.out.println(
"造不了这种汽车。。。");
        }

        

    }


}



实例三:

interface  Car {
    
public void run();
    
public void stop();
}


class  Benz  implements  Car {
    
public void run(){
        System.out.println(
"Benz开始启动了。。。。。");
    }

    
public void stop(){
        System.out.println(
"Benz停车了。。。。。");
    }

}


class  Ford  implements  Car {
    
public void run(){
        System.out.println(
"Ford开始启动了。。。");
    }

    
public void stop(){
        System.out.println(
"Ford停车了。。。。");
    }

}


class  Toyota  implements  Car {
    
public void run(){
        System.out.println(
"Toyota开始启动了。。。");
    }

    
public void stop(){
        System.out.println(
"Toyota停车了。。。。");
    }

}


class  Factory {
    
public static Car getCarInstance(String type){
        Car c
=null;
        
try {
            c
=(Car)Class.forName("org.jzkangta.factorydemo03."+type).newInstance();//利用反射得到汽车类型 
        }
 catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
 catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }

    
        
return c;
    }

}

public   class  FactoryDemo03  {

    
public static void main(String[] args) {
        Car c
=Factory.getCarInstance("Toyota");
        
if(c!=null){
            c.run();
            c.stop();
        }
else{
            System.out.println(
"造不了这种汽车。。。");
        }

        

    }


}


对比三个实例:
实 例一,虽然实现了简单工厂,但每次只能得到一种汽车,如果我们想换一种,就得修改工厂,太不方便,而实例二则改变了这种情况,便得我们可以按照我们的需要 更换汽车,但我们所更换的汽车必须是实现类中有的,如果我们想要增加一种汽车的时候,我们还是得更改工厂,通过改进,实例三利用反射机制,得到汽车类型, 这样当我们需要增加一种新的汽车时,就无需要再修改工厂,而只需要增加要实现的类即可。也就是说要增加什么样的汽车直接增加这个汽车的类即可,而无需改变 工厂。从而达到了工厂分离的效果。

猜你喜欢

转载自bigdragon.iteye.com/blog/2251205