码迷,mamicode.com
首页 > 其他好文 > 详细

基本设计模式06- 一般工厂模式

时间:2020-06-07 19:19:48      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:auto   rabl   pre   headers   测试   cto   dde   过程   简单   

????一般工厂模式用的比较广,在Java中尤为常见,因为相对比较简单,所以使用场景比较广泛;
???? 例如在Springboot在整合RabbitMQ,创建EXCHANGE的代码,QUEUE的代码等都能看到。

1.一般工厂模式

1.1 基本概念

  定义一个创建对象的接口,让子类决定实例化哪个类。工厂类的方法,使一个类的初始化延迟到其子类。

1.2 UML图

技术图片

2. 结合 RabbitMQ中Exchange创建分析

2.1 代码分析

对象类接口

public abstract class AbstractExchange extends AbstractDeclarable implements Exchange {

工厂类

public final class ExchangeBuilder extends AbstractBuilder {
}

生成具体对象类过程

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());
		}
		exchange.setInternal(this.internal);
		exchange.setDelayed(this.delayed);
		exchange.setIgnoreDeclarationExceptions(this.ignoreDeclarationExceptions);
		exchange.setShouldDeclare(this.declare);
		if (!ObjectUtils.isEmpty(this.declaringAdmins)) {
			exchange.setAdminsThatShouldDeclare(this.declaringAdmins);
		}
		return (T) exchange;
	}

3.测试案例代码

要进行的操作

public class Operation {

    protected double numberA = 0 ;
    protected double numberB = 0;


    public void setNumberA(double numberA){
        this.numberA = numberA;
    }
    public double getNumberA(){
        return numberA;
    }
    public void setNumberB(double numberB){
        this.numberB = numberB;
    }

    public double getNumberB(){
        return numberB;
    }

    public double getResult(){
        double result = 0;
        return result;
    }

}

具体的操作类(可以有多个)

public class OperationAdd extends Operation{
    @Override
    public double getResult() {//重写方法
        double result = 0d;
        result = numberA + numberB;
        return result;
    }
}


工厂类

public class OperationFactory {
   public static Operation createOperation(String operation){
       Operation oper = null;
       switch(operation){
           case "+" :
               oper = new OperationAdd();break;
           case "-":
               oper = new OperationSub();break;
           case "*":
               oper = new OperationMul();break;
           case "/":
               oper = new OperationDiv();break;
       }
       return oper ;
   }

基本设计模式06- 一般工厂模式

标签:auto   rabl   pre   headers   测试   cto   dde   过程   简单   

原文地址:https://www.cnblogs.com/perferect/p/13060324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!