基本设计模式05-一般工厂模式
作者:互联网
一般工厂模式用的比较广,在Java中尤为常见,因为相对比较简单,所以使用场景比较广泛;
springboot在整合RabbitMQ,创建EXCHANGE的代码,QUEUE的代码等都能看到。
如下,是创建EXCHANGE的过程,通过建造器,传入类型,根据类型进行对象创建:
public <T extends Exchange> T build() { AbstractExchange exchange; if (ExchangeTypes.DIRECT.equals(this.type)) { exchange = new DirectExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.TOPIC.equals(this.type)) { exchange = new TopicExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.FANOUT.equals(this.type)) { exchange = new FanoutExchange(this.name, this.durable, this.autoDelete, getArguments()); } else if (ExchangeTypes.HEADERS.equals(this.type)) { exchange = new HeadersExchange(this.name, this.durable, this.autoDelete, getArguments()); } else { exchange = new CustomExchange(this.name, this.type, this.durable, this.autoDelete, getArguments()); } }
标签:name,exchange,durable,05,工厂,getArguments,new,设计模式,type 来源: https://www.cnblogs.com/perferect/p/12981852.html