延续『蚂蚁学模式』之实例化简单工厂、工厂方法模式中工厂方法模式的实例。
而这次,汽车厂(MotorFactory)有新的需求,不只要生产国产车(NativeMotor),还需要生产进口车(ForeignMotor)。这两种车都包括公交(Bus)和小轿车(Car)。
1. com.cleversoft.designpatterns.abstractfactory.MotorFactory.java

package  com.cleversoft.designpatterns.abstractfactory;

public   interface  MotorFactory  {

}

2. com.cleversoft.designpatterns.abstractfactory.NativeMotor.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  NativeMotor  implements  MotorFactory  {
    
public Car produceCar() {
        
return new NativeCar();
    }


    
public Bus produceBus() {
        
return new NativeBus();
    }

}

3. com.cleversoft.designpatterns.abstractfactory.ForeignMotor.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  ForeignMotor  implements  MotorFactory  {
    
public Car produceCar() {
        
return new ForeignCar();
    }


    
public Bus produceBus() {
        
return new ForeignBus();
    }

}

4. com.cleversoft.designpatterns.abstractfactory.Bus.java
package  com.cleversoft.designpatterns.abstractfactory;

public   interface  Bus  {
    
public void run();
}

5. com.cleversoft.designpatterns.abstractfactory.Car.java
package  com.cleversoft.designpatterns.abstractfactory;

public   interface  Car  {
    
public void run();
}

6. com.cleversoft.designpatterns.abstractfactory.ForeignBus.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  ForeignBus  implements  Bus  {

    
public ForeignBus() {

    }


    
public void run() {
        System.out.println(
"This is a foreign bus!");
    }

}

7. com.cleversoft.designpatterns.abstractfactory.ForeignCar.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  ForeignCar  implements  Car  {

    
public ForeignCar() {

    }


    
public void run() {
        System.out.println(
"This is a foreign car!");
    }

}

8. com.cleversoft.designpatterns.abstractfactory.NativeBus.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  NativeBus  implements  Bus  {

    
public NativeBus() {

    }


    
public void run() {
        System.out.println(
"This is a native bus!");
    }

}

9. com.cleversoft.designpatterns.abstractfactory.NativeCar.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  NativeCar  implements  Car  {
    
public NativeCar() {

    }


    
public void run() {
        System.out.println(
"This is a native car!");
    }

}

10. com.cleversoft.designpatterns.abstractfactory.Main.java
package  com.cleversoft.designpatterns.abstractfactory;

public   class  Main  {

    
/** *//**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        Bus b = new ForeignMotor().produceBus();
        b.run();
        Car c 
= new NativeMotor().produceCar();
        c.run();
    }


}


终于开始想通一些问题,。也再次印证这样的道理:没有笨的读者,只有笨的作者。
从分析的角度,你要做的无非是些抽象的活儿;从普通编程人员的角度,你要实现的是类似NativeBus.java的具体实现。这是差距,巨大的差距!
会写实例以外的一些东西。

参考资料:
1. http://terrylee.cnblogs.com/archive/2005/12/13/295965.html
2. 《Java与模式》



欢迎大家访问我的个人网站 萌萌的IT人