zoukankan      html  css  js  c++  java
  • 简单工厂模式

    Operation.java

    package calc;
    
    public abstract class Operation {
    
    	private static double _numberA = 0;
    	private static double _numberB = 0;
    	
    	static class NumberA {
    		
    		public static double get_numberA() {
    			return _numberA;
    		}
    		public static void set_numberA(double numberA) {
    			_numberA = numberA;
    		}
    	}
    	
    	static class NumberB {
    		
    		public static double get_numberB() {
    			return _numberB;
    		}
    		public static void set_numberB(double numberB) {
    			_numberB = numberB;
    		}
    	}
    	
    	public abstract double GetResult();
    }
    
    class OperationAdd extends Operation {
    
    	public double GetResult() {
    		double result = 0;
    		result = NumberA.get_numberA() + NumberB.get_numberB();
    		return result;
    	}
    	
    }
    
    class OperationSub extends Operation {
    
    	public double GetResult() {
    		double result = 0;
    		result = NumberA.get_numberA() - NumberB.get_numberB();
    		return result;
    	}
    	
    }
    
    class OperationMul extends Operation {
    
    	public double GetResult() {
    		double result = 0;
    		result = NumberA.get_numberA() * NumberB.get_numberB();
    		return result;
    	}
    	
    }
    
    class OperationDiv extends Operation {
    
    	@Override
    	public double GetResult() {
    		double result = 0;
    		if(NumberB.get_numberB() == 0)
    			try {
    				throw new Exception("not divide by zero");
    			} catch (Exception e) {
    				// TODO Auto-generated catch block
    				System.out.println("not divide by zero");
    			}
    			
    		result = NumberA.get_numberA() / NumberB.get_numberB();
    		return result;
    	}
    	
    }
    

      

    OperationFactory.java

    package calc;
    
    public class OperationFactory {
    
    	public static Operation createOperate (String operator) {
    		
    		Operation oper = null;
    		
    		if(operator == "+")
    			oper = new OperationAdd();
    		else if(operator == "-")
    			oper = new OperationSub();
    		else if(operator == "*")
    			oper = new OperationMul();
    		else if(operator == "/")
    			oper = new OperationDiv();
    		
    		return oper;
    		
    	}
    }
    

      

    测试类

    package calc;
    import java.io.*;
    
    class Calculator {	// 输入两个数, 相除得结果
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws IOException {
    
    //		BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); 
    //		System.out.println("please input a number A:");
    //		String A = b.readLine();
    //		System.out.println("please input a number B:");
    //		String B = b.readLine();
    //		int C = (new Integer(A)).intValue()/(new Integer(B)).intValue();
    //		System.out.println("The result is :" + C);
    		
    		Operation oper;
    		oper = OperationFactory.createOperate("/");
    		Operation.NumberA.set_numberA(1);
    		Operation.NumberB.set_numberB(0);
    		double result = oper.GetResult();
    		System.out.println(result);
    	}
    
    }
    

      

  • 相关阅读:
    iOS-mac下 svn的使用
    iOS-Model-View-ViewModel & ReactiveCocoa
    iOS-通过实现一个TableView来理解iOS UI编程(转)
    iOS-细说 iOS 消息推送(转)
    【原创】O2O,你真的知道怎么玩吗?
    谷歌提升虚拟运营商逼格,传统电信运营商的好日子到头?
    【技术贴】大型发布会现场的WiFi网络应该如何搭建?
    千万别把WIFI玩坏了!关于WIFI的新鲜玩法和商业模式探讨
    【深度分享】千团大战:看今天商业WiFi乱局及其破解之道
    (转)迎接 Entity Framework 7
  • 原文地址:https://www.cnblogs.com/mjn1/p/11469698.html
Copyright © 2011-2022 走看看