zoukankan      html  css  js  c++  java
  • 基本设计模式-06 一般工厂模式

        一般工厂模式用的比较广,在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 ;
       }
    
  • 相关阅读:
    jquery、js调用iframe父窗口与子窗口元素的方法整理
    js中的各种宽高以及位置总结
    javascript call与apply关键字的作用
    javascript之window对象
    CSS3 3D立方体效果
    CSS3 3D transform变换
    JS查找字符串中出现次数最多的字符
    写一个函数将传入的字符串转换成驼峰表示法
    数组去重方法
    内核升级得一个模范
  • 原文地址:https://www.cnblogs.com/perferect/p/13060324.html
Copyright © 2011-2022 走看看