簡單工廠模式的UML圖

 

原始碼:Operation類

Java程式碼 複製程式碼
  1. package Operation;   
  2. publicabstractclass Operation {   
  3. double numberA=0;   
  4. double numberB=0;   
  5. publicdouble getNumberA() {   
  6. return numberA;   
  7.     }   
  8. publicvoid setNumberA(double numberA) {   
  9. this.numberA = numberA;   
  10.     }   
  11. publicdouble getNumberB() {   
  12. return numberB;   
  13.     }   
  14. publicvoid setNumberB(double numberB) {   
  15. this.numberB = numberB;   
  16.     }   
  17. publicabstractdouble operate();   
  18. }  
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. publicclass Add extends Operation {   
  3. @Override
  4. publicdouble operate() {   
  5. return numberA+numberB;   
  6.     }   
  7. }  
package Operation;

public class Add extends Operation {

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

 減類:

Java程式碼 複製程式碼
  1. package Operation;   
  2. publicclass Sub extends Operation {   
  3. @Override
  4. publicdouble operate() {   
  5. return numberA-numberB;   
  6.     }   
  7. }  
package Operation;

public class Sub extends Operation {

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

 乘類:

Java程式碼 複製程式碼
  1. package Operation;   
  2. publicclass Mul extends Operation {   
  3. @Override
  4. publicdouble operate() {   
  5. return numberA*numberB;   
  6.     }   
  7. }  
package Operation;

public class Mul extends Operation {

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

 除類:

Java程式碼 複製程式碼
  1. package Operation;   
  2. import java.io.IOError;   
  3. publicclass Div extends Operation {   
  4. @Override
  5. publicdouble operate() {   
  6. try {   
  7. return numberA/numberB;   
  8.         } catch (IOError e) {   
  9.             System.out.println("除數不能為空!");   
  10. return0;   
  11.         }   
  12.     }   
  13. }  
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. publicclass OperationFactory {   
  7. publicstatic Operation createOperation(char operate){   
  8.         Operation operation=null;   
  9. switch (operate) {   
  10. case'+':   
  11.             operation=new Add();   
  12. break;   
  13. case'-':   
  14.             operation=new Sub();   
  15. break;   
  16. case'*':   
  17.             operation=new Mul();   
  18. break;     
  19. case'/':   
  20.             operation=new Div();   
  21. break;   
  22.         }   
  23. return operation;   
  24.     }   
  25. }  
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. publicclass Client {   
  3. publicstaticvoid main(String[] args) {   
  4.         Operation operation = OperationFactory.createOperation('*');   
  5.         operation.setNumberA(12);   
  6.         operation.setNumberB(6);   
  7.         System.out.println(operation.operate());   
  8.     }   
  9. }