zoukankan      html  css  js  c++  java
  • Java程序员的日常 —— 注册工厂的妙用

    注册工厂是一种很常用的框架书写方法,它适合于快速创建相同类型的对象。

    举个栗子

    比如一个家具工厂,有沙发、椅子、茶几等等,正常的编程模式是这样的:

    //创建
    class 沙发{}
    class 椅子{}
    class 茶几{}
    
    //使用
    new 沙发();
    new 椅子();
    new 椅子();
    new 茶几();
    

    如果想要扩展,就需要继续定义class,然后new对象。

    但是其实沙发的制作与使用时解耦的,使用者并不需要知道沙发、茶几是怎么制作出来的,只是想使用它而已。

    使用注册工厂,相当于沙发、茶几、椅子都统一了一套创建方法,用户只需要去使用就行了。

    参考下面的伪码:

    //定义创建工厂
    interface Factory<T>{ 
    	T create();
    }
    
    //对象继承这个工厂
    class 沙发 extends 家具{
    	public static class Factory implements a.b.c.Factory<沙发>{
    	public 沙发 create(){ return new 沙发()}
    	}
    }
    class 茶几 extends 家具{
    	public static class Factory implements a.b.c.Factory<茶几>{
    	public 茶几 create(){ return new 茶几()}
    	}
    }
    class 椅子 extends 家具{
    	public static class Factory implements a.b.c.Factory<椅子>{
    	public 椅子 create(){ return new 椅子()}
    	}
    }
    
    //注册到工厂Map中
    Map<String,Factory<? extends 家具>> map = new HashMap<>();
    map.put("沙发",new 沙发.Factory());
    map.put("椅子",new 椅子.Factory());
    map.put("茶几",new 茶几.Factory());
    
    //这样在使用的时候,就可以直接用它创建对象了
    map.get("沙发").create()
    

    详细代码

    Factory.class

    package xing.test.thinking.chap14;
    
    public interface Factory<T> {
    	T create();
    }
    

    RegisteredFactories.class

    package xing.test.thinking.chap14;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    class Part {
    	public String toString(){
    		return getClass().getSimpleName();
    	}
    	static List<Factory<? extends Part>> partFactories = new ArrayList<Factory<? extends Part>>();//存放所有的对象工厂
    	//在静态块中添加对象工厂
    	static{
    		partFactories.add(new FuelFilter.Factory());
    		partFactories.add(new AirFilter.Factory());
    		partFactories.add(new CabinAirFilter.Factory());
    		partFactories.add(new OilFilter.Factory());
    		partFactories.add(new FanBelt.Factory());
    		partFactories.add(new PowerSteeringBelt.Factory());
    		partFactories.add(new GeneratorBelt.Factory());
    	}
    	private static Random rand = new Random(47);
    	public static Part createRandom(){
    		int n = rand.nextInt(partFactories.size());
    		return partFactories.get(n).create();
    	}
    }
    class Filter extends Part{}
    class FuelFilter extends Filter {
    	public static class Factory implements xing.test.thinking.chap14.Factory<FuelFilter> {
    		public FuelFilter create(){
    			return new FuelFilter();
    		}
    	}
    }
    class AirFilter extends Filter {
    	public static class Factory implements xing.test.thinking.chap14.Factory<AirFilter> {
    		public AirFilter create(){
    			return new AirFilter();
    		}
    	}
    }
    class CabinAirFilter extends Filter {
    	public static class Factory implements xing.test.thinking.chap14.Factory<CabinAirFilter> {
    		public CabinAirFilter create(){
    			return new CabinAirFilter();
    		}
    	}
    }
    class OilFilter extends Filter {
    	public static class Factory implements xing.test.thinking.chap14.Factory<OilFilter> {
    		public OilFilter create(){
    			return new OilFilter();
    		}
    	}
    }
    class Belt extends Part{};
    class FanBelt extends Belt {
    	public static class Factory implements xing.test.thinking.chap14.Factory<FanBelt> {
    		public FanBelt create(){
    			return new FanBelt();
    		}
    	}
    }
    class GeneratorBelt extends Belt {
    	public static class Factory implements xing.test.thinking.chap14.Factory<GeneratorBelt> {
    		public GeneratorBelt create(){
    			return new GeneratorBelt();
    		}
    	}
    }
    class PowerSteeringBelt extends Belt {
    	public static class Factory implements xing.test.thinking.chap14.Factory<PowerSteeringBelt> {
    		public PowerSteeringBelt create(){
    			return new PowerSteeringBelt();
    		}
    	}
    }
    public class RegisteredFactories {
    	public static void main(String[] args) {
    		for(int i=0 ; i<10 ; i++){
    			System.out.println(Part.createRandom());
    		}
    	}
    }
    
  • 相关阅读:
    Internet Explorer 11:不要再叫我IE
    C#汉字转拼音
    winfrom设置当前画面始终显示在最前面
    解决 winform打开网页 和WebBrowser打开链接360误报拦截的问题
    dataGridView使用指南系列一、回车换行或换列完美解决方案
    C#--WinForm项目主窗体设计
    C#后台解析 json 动态解析 通用(Dictionary)
    在windows下安装git中文版客户端并连接gitlab
    检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法(非简单设置为【经典】模式)
    关闭窗体后,进程仍然在运行的问题重现与解决
  • 原文地址:https://www.cnblogs.com/xing901022/p/5585532.html
Copyright © 2011-2022 走看看