简单工厂模式的UML类图与源码实现(转)

 

简单工厂模式的UML图

 

 

 

 

源码:Operation类

 

Java代码
  1. package Operation;   
  2.   
  3. public abstract class Operation {   
  4.     double numberA=0;   
  5.     double numberB=0;   
  6.        
  7.     public double getNumberA() {   
  8.         return numberA;   
  9.     }   
  10.   
  11.     public void setNumberA(double numberA) {   
  12.         this.numberA = numberA;   
  13.     }   
  14.   
  15.     public double getNumberB() {   
  16.         return numberB;   
  17.     }   
  18.   
  19.     public void setNumberB(double numberB) {   
  20.         this.numberB = numberB;   
  21.     }   
  22.   
  23.     public abstract double operate();   
  24. }  
package Operation;

public abstract class Operation {
	double numberA=0;
	double numberB=0;
	
	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 operate();
}

 加类:

 

Java代码
  1. package Operation;   
  2.   
  3. public class Add extends Operation {   
  4.   
  5.     @Override  
  6.     public double operate() {   
  7.         return numberA+numberB;   
  8.     }   
  9. }  
package Operation;

public class Add extends Operation {

	@Override
	public double operate() {
		return numberA+numberB;
	}
}

 减类:

 

Java代码
  1. package Operation;   
  2.   
  3. public class Sub extends Operation {   
  4.   
  5.     @Override  
  6.     public double operate() {   
  7.         return numberA-numberB;   
  8.     }   
  9. }  
package Operation;

public class Sub extends Operation {

	@Override
	public double operate() {
		return numberA-numberB;
	}
}

 乘类:

 

Java代码
  1. package Operation;   
  2.   
  3. public class Mul extends Operation {   
  4.   
  5.     @Override  
  6.     public double operate() {   
  7.         return numberA*numberB;   
  8.     }   
  9. }  
package Operation;

public class Mul extends Operation {

	@Override
	public double operate() {
		return numberA*numberB;
	}
}

 除类:

 

Java代码
  1. package Operation;   
  2.   
  3. import java.io.IOError;   
  4.   
  5. public class Div extends Operation {   
  6.   
  7.     @Override  
  8.     public double operate() {   
  9.         try {   
  10.             return numberA/numberB;   
  11.         } catch (IOError e) {   
  12.             System.out.println("除数不能为空!");   
  13.             return 0;   
  14.         }   
  15.     }   
  16. }  
package Operation;

import java.io.IOError;

public class Div extends Operation {

	@Override
	public double operate() {
		try {
			return numberA/numberB;
		} catch (IOError e) {
			System.out.println("除数不能为空!");
			return 0;
		}
	}
}

 

工厂类:

 

Java代码
  1. import Operation.Add;   
  2. import Operation.Div;   
  3. import Operation.Mul;   
  4. import Operation.Operation;   
  5. import Operation.Sub;   
  6.   
  7. public class OperationFactory {   
  8.     public static Operation createOperation(char operate){   
  9.         Operation operation=null;   
  10.         switch (operate) {   
  11.         case '+':   
  12.             operation=new Add();   
  13.             break;   
  14.         case '-':   
  15.             operation=new Sub();   
  16.             break;   
  17.         case '*':   
  18.             operation=new Mul();   
  19.             break;     
  20.         case '/':   
  21.             operation=new Div();   
  22.             break;   
  23.         }   
  24.         return operation;   
  25.     }   
  26. }  
import Operation.Add;
import Operation.Div;
import Operation.Mul;
import Operation.Operation;
import Operation.Sub;

public class OperationFactory {
	public static Operation createOperation(char operate){
		Operation operation=null;
		switch (operate) {
		case '+':
			operation=new Add();
			break;
		case '-':
			operation=new Sub();
			break;
		case '*':
			operation=new Mul();
			break;	
		case '/':
			operation=new Div();
			break;
		}
		return operation;
	}
}

 客户端:

 

Java代码
  1. import Operation.Operation;   
  2. public class Client {   
  3.   
  4.     public static void main(String[] args) {   
  5.         Operation operation = OperationFactory.createOperation('*');   
  6.         operation.setNumberA(12);   
  7.         operation.setNumberB(6);   
  8.         System.out.println(operation.operate());   
  9.     }   
  10. }  

 

转自:http://hlzhou.javaeye.com/blog/551492

你可能感兴趣的:(软件工程)