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());
		}
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM