本质:实例化对象不用new,用工厂代替,实现了创建者和调用者分离
满足:
开闭原则:对拓展开放,对修改关闭
依赖倒置原则:要针对接口编程
迪米特原则:最少了解原则,只与自己直接相关的类有关系
简单工厂模式
也被称为静态工厂
public interface Car {void name();
}
public class BWM implements Car{
@Overridepublic void name() {System.out.println("宝马");}
}
public class DaZhong implements Car{
@Overridepublic void name() {System.out.println("大众");}
}
public class CarFactory{public static Car getCar(String name){if(name.equals("大众")){return new DaZhong();}else if (name.equals("宝马")){return new BWM();}else{return null;}}
}
public class consumer {public static void main(String[] args) {
Car car = CarFactory.getCar("大众");car.name();Car car2 = CarFactory.getCar("宝马");car2.name();}
}
总结
将创建对象的任务交给工厂去完成
缺点
不满足开闭原则,如果我们新创建一个车,就需要修改CarFactory的源代码
工厂方法模式
多个工厂对应多个实现类
public interface CarFactory {
Car getCar();
}
public class BMWFactory implements CarFactory{@Overridepublic Car getCar() {return new BWM();}
}
public class DaZhongFactory implements CarFactory{@Overridepublic Car getCar() {return new DaZhong();}
}
如果我们想要创建新的车对象,只要创建对应的车工厂即可,无需修改CarFactory的代码
public class Aodi implements Car {@Overridepublic void name() {System.out.println("奥迪");}
}public class AodiFactory implements CarFactory{@Overridepublic Car getCar() {return new Aodi();}
}public class consumer {public static void main(String[] args) {Car car = new DaZhongFactory().getCar();car.name();Car car1 = new BMWFactory().getCar();car1.name();Car car2 = new AodiFactory().getCar();car2.name();}
}
抽象工厂模式
围绕一个超级工厂生产工厂,该工厂又称为其他工厂的工厂 (抽象的抽象)
public interface IProductFactory {IPhoneProduct iphoneproduct();IRouterProduct irouterproduct();
}
具体的产品工厂
public class XiaomiFactory implements IProductFactory{@Overridepublic IPhoneProduct iphoneproduct() {return new XiaomiPhone();}@Overridepublic IRouterProduct irouterproduct() {return new XiaomiRouter();}
}
public class HuaweiFactory implements IProductFactory{@Overridepublic IPhoneProduct iphoneproduct() {return new HuaweiPhone();}@Overridepublic IRouterProduct irouterproduct() {return new HuaweiRouter();}
}
产品功能
public interface IPhoneProduct {void open();void close();
}
public interface IRouterProduct {void open();void close();}
具体实现
public class XiaomiPhone implements IPhoneProduct{@Overridepublic void open() {System.out.println("小米手机开机");}@Overridepublic void close() {System.out.println("小米手机关机");}
}
public class HuaweiPhone implements IPhoneProduct{@Overridepublic void open() {System.out.println("华为手机开机");}@Overridepublic void close() {System.out.println("华为手机关机");}
}
public class XiaomiRouter implements IRouterProduct{@Overridepublic void open() {System.out.println("小米路由器开机");}@Overridepublic void close() {System.out.println("小米路由器关机");}
}
public class HuaweiRouter implements IRouterProduct{@Overridepublic void open() {System.out.println("华为路由器开机");}@Overridepublic void close() {System.out.println("华为路由器关机");}
}
测试
public class consumer {public static void main(String[] args) {//先创建工厂System.out.println("==========小米==========");IPhoneProduct product = new XiaomiFactory().iphoneproduct();product.open();product.close();IRouterProduct irouterproduct = new XiaomiFactory().irouterproduct();irouterproduct.open();irouterproduct.close();System.out.println("===========华为==========");IPhoneProduct iphoneproduct = new HuaweiFactory().iphoneproduct();iphoneproduct.open();iphoneproduct.close();IRouterProduct irouterproduct1 = new HuaweiFactory().irouterproduct();irouterproduct1.open();irouterproduct1.close();}
}