提供一个创建一系列相关或者相互依赖对象的接口,而无须指定他们具体的类
优点:
良好的封装性:每个产品类与高层之间的关系不需要被client知道,client只需要知道工厂类能够创建出一个需要的对象即可
产品族内的约束关系为非公开的状态
缺点:
产品族扩展较难,产品等级扩展容易
代码
Main
public class Main {
public static void main(String[] args) {
DoorFactory ldf=new LeftDoorFactory();
DoorFactory rdf=new RightDoorFactory();
CarDoor audiLeftDoor=ldf.getAUDIDoor();
audiLeftDoor.getColor();
audiLeftDoor.getDir();
CarDoor bmwRightDoor=rdf.getBMWDoor();
bmwRightDoor.getColor();
bmwRightDoor.getDir();
DoorFactory bdf=new BackDoorFactory();
CarDoor bmwBackDoor=bdf.getBMWDoor();
bmwBackDoor.getColor();
bmwBackDoor.getDir();
}
}
interface:CarDoor
public interface CarDoor {
public void getColor();
public void getDir();
}
interface:DoorFactory
public interface DoorFactory {
public CarDoor getBMWDoor();
public CarDoor getAUDIDoor();
public CarDoor getTOYOTADoor();
}
imp:BackDoorFactory
public class BackDoorFactory implements DoorFactory {
@Override
public CarDoor getBMWDoor() {
return new BMWBackDoor();
}
@Override
public CarDoor getAUDIDoor() {
// TODO Auto-generated method stub
return null;
}
@Override
public CarDoor getTOYOTADoor() {
// TODO Auto-generated method stub
return null;
}
}
imp:LeftDoorFactory
public class LeftDoorFactory implements DoorFactory {
@Override
public CarDoor getBMWDoor() {
return new BMWLeftDoor();
}
@Override
public CarDoor getAUDIDoor() {
return new AUDILeftDoor();
}
@Override
public CarDoor getTOYOTADoor() {
return new TOYOTALeftDoor();
}
}
imp:RightDoorFactory
public class RightDoorFactory implements DoorFactory {
@Override
public CarDoor getBMWDoor() {
return new BMWRightDoor();
}
@Override
public CarDoor getAUDIDoor() {
return new AUDIRightDoor();
}
@Override
public CarDoor getTOYOTADoor() {
return new TOYOTARightDoor();
}
}
imp:AUDIDoor
public class AUDIDoor implements CarDoor {
@Override
public void getColor() {
System.out.println("奥迪一直都是黑色的");
}
@Override
public void getDir() {
// TODO Auto-generated method stub
}
}
imp:BMWDoor
public class BMWDoor implements CarDoor {
@Override
public void getColor() {
System.out.println("宝马的门向来都是红色的");
}
@Override
public void getDir() {
// TODO Auto-generated method stub
}
}
imp:TOYOTADoor
public class TOYOTADoor implements CarDoor {
@Override
public void getColor() {
System.out.println("丰田一直都是白色的");
}
@Override
public void getDir() {
// TODO Auto-generated method stub
}
}
AUDILeftDoor
public class AUDILeftDoor extends AUDIDoor {
@Override
public void getDir() {
System.out.println("这是左门");
}
}
AUDIRightDoor
public class AUDIRightDoor extends AUDIDoor {
@Override
public void getDir() {
System.out.println("这是右门");
}
}
BMWLeftDoor
public class BMWLeftDoor extends BMWDoor {
@Override
public void getDir() {
System.out.println("这是作门");
}
}
BMWRightDoor
public class BMWRightDoor extends BMWDoor {
@Override
public void getDir() {
System.out.println("这是右门");
}
}
BMWBackDoor
public class BMWBackDoor extends BMWDoor {
@Override
public void getDir() {
System.out.println("这是宝马的尾门");
}
}
TOYOTALeftDoor
public class TOYOTALeftDoor extends TOYOTADoor {
@Override
public void getDir() {
System.out.println("这是左门");
}
}
TOYOTARightDoor
public class TOYOTARightDoor extends TOYOTADoor {
@Override
public void getDir() {
System.out.println("这是右门");
}
}