实现了创建者和调用者的分离
详细分类
简单工厂模式
工厂方法模式
抽象工厂模式
开闭原则: 一个软件的实体应当对扩展开放, 对修改关闭
依赖倒转原则: 要针对接口编程, 不要针对实现编程
迪米特法则: 只与你直接的朋友通信, 而避免和陌生人通信
实例化对象不使用new, 用工厂方法代替
将选择实现类, 创建对象统一管理和控制. 从而将调用者跟我们的实现类解耦
简单工厂模式
用来生产统一等级结构中的任意产品(对于增加新的产品,需要覆盖已有代码)
工厂方法模式
用来生产同一等级结构中的固定产品(支持增加任意产品)
抽象工厂模式
围绕一个超级工厂创建其他工厂, 该超级工厂又称为其他工厂的工厂
package factory.simple;
public interface Car {
void name();
}
package factory.simple;
public class Tesla implements Car{
@Override
public void name() {
System.out.println("Tesla!");
}
}
package factory.simple;
public class WuLing implements Car {
@Override
public void name() {
System.out.println("WuLing!");
}
}
package factory.simple;
public class CarFactory {
public static Car getCar(String car){
if ("WuLing".equals(car)){
return new WuLing();
}else if ("Tesla".equals(car)){
return new Tesla();
}else{
return null;
}
}
}
package factory.simple;
public class Consumer {
public static void main(String[] args) {
// 传统方法
// Car car = new WuLing();
// Car car1 = new Tesla();
// car.name();
// car1.name();
// ========================================
Car car = CarFactory.getCar("Tesla");
Car car1 = CarFactory.getCar("WuLing");
car.name();
car1.name();
}
}
package factory.methodFactory;
public interface Car {
void name();
}
package factory.methodFactory;
public interface CarFactory {
Car getCar();
}
package factory.methodFactory;
public class DazhongFactory implements CarFactory {
@Override
public Car getCar() {
return new Dazhong();
}
}
package factory.methodFactory;
public class Dazhong implements Car {
@Override
public void name() {
System.out.println("Dazhong!");
}
}
package factory.methodFactory;
public class TeslaFactory implements CarFactory{
@Override
public Car getCar() {
return new Tesla();
}
}
package factory.methodFactory;
public class Tesla implements Car {
@Override
public void name() {
System.out.println("Tesla!");
}
}
package factory.methodFactory;
public class WuLingFactory implements CarFactory {
@Override
public Car getCar() {
return new WuLing();
}
}
package factory.methodFactory;
public class WuLing implements Car {
@Override
public void name() {
System.out.println("WuLing!");
}
}
相比于简单工厂模式(静态工厂模式),添加新的汽车类是不需要变化原有的代码,只需添加新的汽车工厂和汽车类。遵循7大原则,开闭原则。
package factory.abstract1;
//抽象产品工厂
public interface ProductFactory {
// 生产手机
PhoneProduct phoneProduct();
// 生产路由器
RouterProduct routerProduct();
}
package factory.abstract1;
//手机产品接口
public interface PhoneProduct {
void start();
void shutdown();
void call();
void sendMassage();
}
package factory.abstract1;
//路由器产品接口
public interface RouterProduct {
void start();
void shutdown();
void openWife();
void Setting();
}
package factory.abstract1;
//华为手机工厂
public class HuaWeiFactory implements ProductFactory{
@Override
public PhoneProduct phoneProduct() {
return new HuaWeiPhone();
}
@Override
public RouterProduct routerProduct() {
return new HuaWeiRouter();
}
}
package factory.abstract1;
//华为手机
public class HuaWeiPhone implements PhoneProduct{
@Override
public void start() {
System.out.println("开启华为手机");
}
@Override
public void shutdown() {
System.out.println("关闭华为手机");
}
@Override
public void call() {
System.out.println("华为手机打电话");
}
@Override
public void sendMassage() {
System.out.println("华为手机发短信");
}
}
package factory.abstract1;
public class HuaWeiRouter implements RouterProduct{
@Override
public void start() {
System.out.println("开启华为路由器");
}
@Override
public void shutdown() {
System.out.println("关闭华为路由器");
}
@Override
public void openWife() {
System.out.println("华为路由器开启wife");
}
@Override
public void Setting() {
System.out.println("设置华为路由器");
}
}
package factory.abstract1;
//小米手机工厂
public class XiaomiFactory implements ProductFactory {
@Override
public PhoneProduct phoneProduct() {
return new XIaomiphone();
}
@Override
public RouterProduct routerProduct() {
return new XiaomiRouter();
}
}
package factory.abstract1;
//小米手机
public class XIaomiphone implements PhoneProduct {
@Override
public void start() {
System.out.println("开启小米手机");
}
@Override
public void shutdown() {
System.out.println("关闭小米手机");
}
@Override
public void call() {
System.out.println("小米手机打电话");
}
@Override
public void sendMassage() {
System.out.println("小米手机发短信");
}
}
package factory.abstract1;
public class XiaomiRouter implements RouterProduct{
@Override
public void start() {
System.out.println("开启小米路由器");
}
@Override
public void shutdown() {
System.out.println("关闭小米路由器");
}
@Override
public void openWife() {
System.out.println("小米路由器开启wife");
}
@Override
public void Setting() {
System.out.println("设置小米路由器");
}
}
package factory.abstract1;
public class Client {
public static void main(String[] args) {
System.out.println("===========小米系列产品==============");
XiaomiFactory xiaomiFactory = new XiaomiFactory();
XIaomiphone xIaomiphone = (XIaomiphone) xiaomiFactory.phoneProduct();
xIaomiphone.start();
xIaomiphone.call();
xIaomiphone.sendMassage();
xIaomiphone.shutdown();
XiaomiRouter xiaomiRouter = (XiaomiRouter) xiaomiFactory.routerProduct();
xiaomiRouter.start();
xiaomiRouter.Setting();
xiaomiRouter.openWife();
xiaomiRouter.shutdown();
System.out.println("===========华为系列产品==============");
HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
HuaWeiPhone huaWeiPhone = (HuaWeiPhone) huaWeiFactory.phoneProduct();
huaWeiPhone.start();
huaWeiPhone.call();
huaWeiPhone.sendMassage();
huaWeiPhone.shutdown();
HuaWeiRouter huaWeiRouter = (HuaWeiRouter) huaWeiFactory.routerProduct();
huaWeiRouter.start();
huaWeiRouter.Setting();
huaWeiRouter.openWife();
huaWeiRouter.shutdown();
System.out.println("====================小米手机系列产品====================");
PhoneProduct phoneProduct = xiaomiFactory.phoneProduct();
phoneProduct.start();
phoneProduct.shutdown();
RouterProduct routerProduct = xiaomiFactory.routerProduct();
routerProduct.openWife();
routerProduct.shutdown();
System.out.println("====================华为手机系列产品====================");
HuaWeiFactory huaWeiFactory1 = new HuaWeiFactory();
phoneProduct = huaWeiFactory1.phoneProduct();
phoneProduct.start();
phoneProduct.shutdown();
routerProduct = huaWeiFactory1.routerProduct();
routerProduct.openWife();
routerProduct.shutdown();
}
}
简单工厂模式(静态工厂模式)
虽然某种程度上不符合设计原则, 但实际使用最多
工厂方法模式
不修改已有类的前提下, 通过增加新的工厂类实现扩展
抽象工厂模式
不可以增加产品, 可以增加产品族
应用场景
JDK中Calendar的getInstance方法
JDBC中的Connection对象的获取
Spring中IOC容器创建管理bean对象
反射z中Class对象的newstance方