深入浅出工厂模式
http://blog.csdn.net/ai92/archive/2004/12/08/209198.aspx
?
有一个例子没有给出,补上:
?
?package abstractFactory;
//抽象产品角色
public interface IBusinessCar
{
?public void drive();
}
package abstractFactory;
//抽象产品角色
public interface ISportCar
{
?public void drive();
}
package abstractFactory;
//具体产品角色
public class BusinessBenz implements IBusinessCar
{
?@Override
?public void drive() {
??System.out.println("开车");
?}
?
}
package abstractFactory;
//具体产品角色
public class BusinessBMW implements IBusinessCar
{
?@Override
?public void drive()
?{
??System.out.println("开车");
?}
?
}
package abstractFactory;
//具体产品角色
public class SportBenz implements ISportCar
{
?@Override
?public void drive() {
??System.out.println("开车");
?}
?
}
package abstractFactory;
//具体产品角色
public class SportBMW implements ISportCar
{
?@Override
?public void drive()
?{
??System.out.println("开车");
?}
?
}
package abstractFactory;
//抽象工厂角色
public interface Driver {
?IBusinessCar BusinessCarDriver();
?ISportCar SportCarDriver();
}
package abstractFactory;
//具体工厂角色
public class BenzDriver implements Driver{
?public IBusinessCar BusinessCarDriver()
?{
?return new BusinessBenz();
?}
?public ISportCar SportCarDriver()
?{
?return new SportBenz();
?}
}
package abstractFactory;
//具体工厂角色
public class BmwDriver implements Driver{
?public IBusinessCar BusinessCarDriver()
?{
?return new BusinessBMW();
?}
?public ISportCar SportCarDriver()
?{
?return new SportBMW();
?}
}
package abstractFactory;
/*
?* 工厂方法模式:每个抽象产品派生多个具体产品类,每个抽象工厂类派生多个具体工厂类,每个具体工厂类负责一个具体产品的实例创建;
?* 抽象工厂模式:每个抽象产品派生多个具体产品类,每个抽象工厂类派生多个具体工厂类,每个具体工厂类负责多个(一系列)具体产品的实例创建。
?*/
public class Magnate {
?public static void main(String args[]) {
??Driver myDriver = new BenzDriver();
??ISportCar myCar = myDriver.SportCarDriver();
??myCar.drive();
?}
}
?