디자인 모델 - 공장 모델 (Factory)

추상 류, 추상 류 의 구체 류, 추상 공장 류, 추상 공장 류 의 구체 류 를 실현 한다.
Salary.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public interface Salary { 4 5 void computerSalary(); 6 7 } 8

 
ASalary.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public class ASalary implements Salary { 4 5 @Override 6 public void computerSalary() { 7 System.out.println( " A " ); 8 } 9 10 } 11

 
BSalary.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public class BSalary implements Salary { 4 5 @Override 6 public void computerSalary() { 7 System.out.println( " B " ); 8 } 9 10 } 11

 
SalaryFacotry.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public interface SalaryFactory { 4 5 Salary createSalary(); 6 7 } 8

 
ASalaryFactory.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public class ASalaryFactory implements SalaryFactory { 4 5 @Override 6 public Salary createSalary() { 7 return new ASalary(); 8 } 9 10 } 11

 
BSalaryFacotry.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public class BSalaryFactory implements SalaryFactory { 4 5 @Override 6 public Salary createSalary() { 7 return new BSalary(); 8 } 9 10 } 11

 
Main.java

   
   
   
   
1 package cn.foxeye.design.factory; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Salary salary = new ASalaryFactory().createSalary(); 7 salary.computerSalary(); 8 9 Salary salary2 = new BSalaryFactory().createSalary(); 10 salary2.computerSalary(); 11 12 } 13 14 } 15

좋은 웹페이지 즐겨찾기