• 抽象工厂


    github https://github.com/spring2go/core-spring-patterns.git

    问题域

    • 相关产品家族
      • 电器设备工厂
      • 电扇Fan
      • 日光灯TubeLight
      • 开关Switch
    • 不同风格产品家族
      • 中国电器设备厂(China)
      • 美国电器设备分厂(US)

    定义

    • 提供一个接口,用于制造一族相关或者相互依赖的产品,无需指定具体的实现类。
    • 创建工厂的工厂

    关系图

    产品 电扇接口

    public interface IFan {
    	
    	public void swithOn();
    	
    	public void switchOff();
    
    }
    

    电器 工厂接口

    1. 生产风扇
    2. 生产日光灯
    public interface IElectricalFactory {
    	
    	IFan createFan();
    	
    	ITubeLight createTubeLight();
    }
    

    中国工厂和产品实现

    // 中国制造的风扇
    public class ChineseFan implements IFan {
    
    	public void swithOn() {
    		System.out.println("The ChineseFan is swithed on ...");
    	}
    
    	public void switchOff() {
    		System.out.println("The ChineseFan is swithed off ...");
    	}
    
    }
    
    // 中国制造的台灯
    public class ChineseTubeLight implements ITubeLight {
    	public void swithOn() {
    		System.out.println("The ChineseTubeLight is swithed on ...");
    	}
    
    	public void switchOff() {
    		System.out.println("The ChineseTubeLight is swithed off ...");
    	}
    
    	public void tuneLight() {
    		System.out.println("The ChineseTubeLight is tuned ...");
    		
    	}
    }
    
    // 中国 电器工厂 实现
    public class ChineseElectricalFactory implements IElectricalFactory {
    
    	public IFan createFan() {
    		return new ChineseFan();
    	}
    
    	public ITubeLight createTubeLight() {
    		return new ChineseTubeLight();
    	}
    
    }
    

    美国工厂和产品 实现

    // 美国制造的风扇
    public class USFan implements IFan {
    
    	public void swithOn() {
    		System.out.println("The USFan is swithed on ...");
    	}
    
    	public void switchOff() {
    		System.out.println("The USFan is swithed off ...");
    	}
    
    }
    
    // 美国制造的 日光灯
    public class USTubeLight implements ITubeLight {
    	public void swithOn() {
    		System.out.println("The USTubeLight is swithed on ...");
    	}
    
    	public void switchOff() {
    		System.out.println("The USTubeLight is swithed off ...");
    	}
    
    	public void tuneLight() {
    		System.out.println("The USTubeLight is tuned ...");	
    	}
    }
    
    // 美国 电器工厂的 实现
    public class USElectricalFactory  implements IElectricalFactory {
    
    	public IFan createFan() {
    		return new USFan();
    	}
    
    	public ITubeLight createTubeLight() {
    		return new USTubeLight();
    	}
    
    }
    

    客户端调用

    public class AbstractFactoryMain {
    
    	public static void main(String[] args) {
    
    		// 中国 电器工厂 出来的产品都是中国风格的产品
    		IElectricalFactory electricalFactory = new ChineseElectricalFactory();
    		IFan fan = electricalFactory.createFan();
    		fan.swithOn();
    		
    		// 美国 电器工厂 出来的产品都是美国风格的产品
    		electricalFactory = new USElectricalFactory();
    		ITubeLight tubeLight = electricalFactory.createTubeLight();
    		tubeLight.swithOn();
    		tubeLight.tuneLight();
    	}
    }
    

    好处

    • 解耦
      • 客户代码和具体产品解耦
      • 产品家族之间解耦
    • 比工厂模式更高层的设计模式
    • 标准化产品构造流程
    • 易于替换产品家族

    Spring框架应用

    • FactoryBean 接口基于抽象工厂模式
      • ProxyFactoryBean
      • JndiFactoryBean
      • LocalSessionFactoryBean
      • LocalContainerEntityManagerFactoryBean
    • 构造具有很多依赖的复杂对象
    • 构造逻辑易变且依赖于配置
  • 相关阅读:
    onenote 使用手记0.3阶级
    尘埃落定:没有传说中k700i,官方只认可k700!
    【转】Ubuntu 9.10下安装Eclipse CDT 6.0
    四则运算
    文本内容统计
    《程序员修炼之道:从小工到专家》读后感(4)
    《程序员修炼之道:从小工到专家》读后感(5)
    多线程
    河北金力集团公文流转系统节选(2)
    动手动脑(6)文件和流
  • 原文地址:https://www.cnblogs.com/zhangjianbin/p/9147832.html
Copyright © 2020-2023  润新知