简单工厂模式java源码实现(大话设计模式学习备忘录)

/**
 * 简单工厂模式</br>
 * QS:请用C++,JAVA,C#任意一种以面向对象语言实现一个计算器控制台程序:要求输入2个数和运算符号,得到结果
 * 第一次程序
 * @author xangqun
 *
 */
public class Program {

	/** 
	 * PS:
	 *   <PRE>用a,b,c,d这样的命名是非常不规范的</PRE>
	 *   <PRE>判断分支(4个if)这样的写法,意味着每个条件都要做出判断,等于计算机做了3次无用功</PRE>
	 *   <pre>如果除数为0或是不填怎么办?</pre>
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		System.out.println("请输入数字A:"); 
		String a = new BufferedReader(new InputStreamReader( 
		System.in)).readLine();
		System.out.println("请输入运算符:");
		String b=new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
        System.out.println("请输入数字c:");
        String c=new BufferedReader(new InputStreamReader(System.in)).readLine();
        double d = 0;
        if("+".equalsIgnoreCase(b)){
        	d=Double.valueOf(a)+Double.valueOf(c);
        }
        if("-".equalsIgnoreCase(b)){
        	d=Double.valueOf(a)-Double.valueOf(c);       	
        }
        if("*".equalsIgnoreCase(b)){
        	d=Double.valueOf(a)*Double.valueOf(c);       
        }
        if("/".equalsIgnoreCase(b)){
        	d=Double.valueOf(a)/Double.valueOf(c);  
        }
        System.out.println("结果为:"+d);
	}

}
/**
 * 简单工厂模式</br>
 * QS:请用C++,JAVA,C#任意一种以面向对象语言实现一个计算器控制台程序:要求输入2个数和运算符号,得到结果
 * 改进的程序1
 * @author xangqun
 *
 */
public class ProgramTwo {

	/**<b>面向对象(活字印刷启迪)</b> 
	 * <ul>
	 * <li>1.要改只需更改要改之字,此为可维护</li>
	 * <li>2.这些字并非用完这次就无用完全可以在后来的印刷中重复使用,此为可复用</li>
	 * <li>3.此诗若要加字只需另外刻字加入即可,此为可扩展</li>
	 * <li>4.字的排列其实可能是竖排可能横排此时只需将活字移动就可做到满足排列需求,此是灵活性好</li>
	 * </ul>
	 * 
	 * PS:计算要和显示分离,准确的说就是:<b>业务逻辑与界面逻辑分开<b>
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			System.out.println("请输入数字1:");
			String strA=new BufferedReader(new InputStreamReader(System.in)).readLine();
			System.out.println("请输入运算符:");
			String stroperate=new BufferedReader(new InputStreamReader(System.in)).readLine();
			System.out.println("请输入数字2:");
			String strB=new BufferedReader(new InputStreamReader(System.in)).readLine();
			String strResult;
			if("+".equalsIgnoreCase(stroperate)){
				strResult=String.valueOf(Double.valueOf(strA)+Double.valueOf(strB));
			}else if("-".equalsIgnoreCase(stroperate)){
				strResult=String.valueOf(Double.valueOf(strA)-Double.valueOf(strB));
			}else if("*".equalsIgnoreCase(stroperate)){
				strResult=String.valueOf(Double.valueOf(strA)*Double.valueOf(strB));
			}else{
               if("0".equalsIgnoreCase(strB)){
            	   strResult="除数不能为0"; 
               }else{
            	   strResult=String.valueOf(Double.valueOf(strA)/Double.valueOf(strB));  
               }
			}
			System.out.println("结果为:"+strResult);
		}catch(Exception e){
			System.out.println("你的输入有错误!");
		}

	}

}
public abstract class Operation {

	private double numberA;
	private double numberB;
	
	public double getNumberA() {
		return numberA;
	}

	public void setNumberA(double numberA) {
		this.numberA = numberA;
	}

	public double getNumberB() {
		return numberB;
	}

	public void setNumberB(double numberB) {
		this.numberB = numberB;
	}

	public abstract double getResult()throws Exception;
}
public class OperationAdd extends Operation {

	@Override
	public double getResult() {
		double numberd;
		numberd=getNumberA()+getNumberB();
		return numberd;
	}

}
public class OperationDiv extends Operation {

	@Override
	public double getResult() throws Exception {
		double numberd;
		if(getNumberB()==0){
			throw new Exception("除数不为0");
		}
		numberd=getNumberA()/getNumberB();
		return numberd;
	}

}
public class OperationMul extends Operation {

	@Override
	public double getResult() {
		double numberd;
		numberd=getNumberA()*getNumberB();
		return numberd;
	}

}
public class OperationSub extends Operation {

	@Override
	public double getResult() {
		double numberd;
		numberd=getNumberA()-getNumberB();
		return numberd;
	}

}
/**
 * 简单工厂模式</br>
 * QS:请用C++,JAVA,C#任意一种以面向对象语言实现一个计算器控制台程序:要求输入2个数和运算符号,得到结果
 * 改进的程序2
 * @author xangqun
 * 
 *PS:面向对象的三大特性:封装,继承,多态,(<b>java的4个特性:抽象,封装,继承,多态</b>)
 *   <br>这里只用到了特性:<b>封装</b>
 */
public class ProgramThree {

	public static double getResult(double numberA,double numberB,String operate){
		double result=0;
		if("+".equalsIgnoreCase(operate)){
			result=numberA+numberB;
		}else if("-".equalsIgnoreCase(operate)){
			result=numberA-numberB;
		}else if("*".equalsIgnoreCase(operate)){
			result=numberA*numberB;
		}else{
        	result=numberA/numberB;
		}
		return result;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			System.out.println("请输入数字1:");
			String strA=new BufferedReader(new InputStreamReader(System.in)).readLine();
			System.out.println("请输入运算符:");
			String stroperate=new BufferedReader(new InputStreamReader(System.in)).readLine();
			System.out.println("请输入数字2:");
			String strB=new BufferedReader(new InputStreamReader(System.in)).readLine();
			double strResult;
			strResult=getResult(Double.valueOf(strA), Double.valueOf(strB), stroperate);
			System.out.println("结果为:"+strResult);
		}catch(Exception e){
			System.out.println("你的输入有错误!");
		}

	}

}
public class OperationFactory {

	public static Operation createOperate(String operate){
		Operation operation=null;
		if("+".equalsIgnoreCase(operate)){
			operation=new OperationAdd();
		}else if("-".equalsIgnoreCase(operate)){
			operation=new OperationSub();
		}else if("*".equalsIgnoreCase(operate)){
			operation=new OperationMul();
		}else{
			operation=new OperationDiv();
		}
		return operation;
	}
}
/**
 * 简单工厂模式</br> 
 * QS:请用C++,JAVA,C#任意一种以面向对象语言实现一个计算器控制台程序:要求输入2个数和运算符号,得到结果 改进的程序3
 * 
 * @author xangqun
 * 
 *        PS:面向对象的三大特性:封装,继承,多态,(<b>java的4个特性:抽象,封装,继承,多态</b>)
 */
public class ProgramFour {

	/** <h1>简单工厂模式</h1>
	 * 到底要实例化谁,将来会不会增加实例化的对象,应该考虑用一个单独的类来做这个创建实例的过程,这就是工厂
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println("请输入数字1:");
			String strA = new BufferedReader(new InputStreamReader(System.in))
					.readLine();
			System.out.println("请输入运算符:");
			String stroperate = new BufferedReader(new InputStreamReader(
					System.in)).readLine();
			System.out.println("请输入数字2:");
			String strB = new BufferedReader(new InputStreamReader(System.in))
					.readLine();
			Operation oper;
			oper=OperationFactory.createOperate(stroperate);
			oper.setNumberA(Double.valueOf(strA));
			oper.setNumberB(Double.valueOf(strB));
			double result=oper.getResult();
			System.out.println(result);
		} catch (Exception e) {

		}

	}

}

相关推荐