zoukankan      html  css  js  c++  java
  • 工厂方法模式(Java代码)

    工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

    运算类:

    //运算类
    public abstract class Operation {
        private double numberA=0;
        private double numberB=0;
        public double getNumberB() {
            return numberB;
        }
        public void setNumberB(double numberB) {
            this.numberB = numberB;
        }
        public double getNumberA() {
            return numberA;
        }
        public void setNumberA(double numberA) {
            this.numberA = numberA;
        }
        public abstract double GetResult();
    }
    
    //加法类
    public class OperationAdd extends Operation {
        public double GetResult()
        {
            double result=0;
            result=super.getNumberA()+super.getNumberB();
            return result;
        }
    }
    
    //减法类
    public class OperationSub extends Operation {
        public double GetResult()
        {
            double result=0;
            result=super.getNumberA()-super.getNumberB();
            return result;
        }
    }
    
    //乘法类
    public class OperationMul extends Operation {
        public double GetResult()
        {
            double result=0;
            result=super.getNumberA()*super.getNumberB();
            return result;
        }
    }
    
    //除法类
    public class OperationDiv extends Operation {
        public double GetResult()
        {
            double result=0;
            result=super.getNumberA()/super.getNumberB();
            return result;
        }
    }

    先创建一个工厂接口:

    public interface IFactory {
        Operation CreateOperation();
    }

    然后加减乘除各建一个具体工厂去实现这个接口:

    public class AddFactory implements IFactory {
        public Operation CreateOperation()
        {
            return new OperationAdd();
        }
    }
    
    public class SubFactory implements IFactory {
        public Operation CreateOperation()
        {
            return new OperationSub();
        }
    }
    
    public class MulFactory implements IFactory {
        public Operation CreateOperation()
        {
            return new OperationMul();
        }
    }
    
    public class DivFactory implements IFactory {
        public Operation CreateOperation()
        {
            return new OperationDiv();
        }
    }

    客户端实现:

    public class Calc {
        public static void main(String args[])
        {
            IFactory operFactory=new DivFactory();
            Operation oper=operFactory.CreateOperation();
            oper.setNumberA(1);
            oper.setNumberB(2);
            double result=oper.GetResult();
            System.out.println(result);
        }
    }
  • 相关阅读:
    机器学习决策树
    Apriori算法实战福彩双色球
    机器学习Random Forest算法简介
    机器学习CART决策树
    基本算法求最大子数组和 及其变种
    【语言处理与Python】3.6规范化文本
    【语言处理与Python】3.3使用Unicode进行文字处理
    【语言处理与Python】3.2字符串:最底层的文本处理
    【语言处理与Python】4.1回到基础
    【语言处理与Python】3.7用正则表达式为文本分词
  • 原文地址:https://www.cnblogs.com/weixiaojun/p/3373888.html
Copyright © 2011-2022 走看看