简单工厂模式

设计模式的意义在于:面向业务内容、业务数据结构和系统架构,高内聚低耦合、优雅的将平面逻辑立体化。

 1 package designPattern;
 2 
 3 /**
 4  * 简单工厂模式
 5  * @author Administrator
 6  */
 7 public class A2_SimpleFactoryTest {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         try {
14             Person person = Recruiment.recruiment("student");
15             person.experience();
16 
17             Person middle_programmer = Recruiment.recruiment("middle_programmer");
18             middle_programmer.experience();
19         } catch (Exception e) {
20             System.out.println(e.getMessage());
21         }
22     }
23 
24     public static Person recruiment(String type) {
25         if (type.equals("student")) {
26             student s = new student();
27             return s;
28         }
29         if (type.equals("middle_programmer")) {
30             Middle_programmer mp = new Middle_programmer();
31             return mp;
32         }
33         if (type.equals("senior_programmer")) {
34             senior_programmer sp = new senior_programmer();
35             return sp;
36         }
37         return null;
38     }
39 }
40 
41 interface Person {
42     public void experience();
43 }
44 
45 class student implements Person {
46     public void experience() {
47         System.out.println("我是学生没有工作经验....");
48     }
49 }
50 
51 class Middle_programmer implements Person {
52     public void experience() {
53         System.out.println("有2年工作经验...");
54     }
55 }
56 
57 class senior_programmer implements Person {
58     public void experience() {
59         System.out.println("有5年工作经验...");
60     }
61 }
62 
63 class Recruiment {
64     public static Person recruiment(String type) {
65         if (type.equals("student")) {
66             student s = new student();
67             return s;
68         }
69         if (type.equals("middle_programmer")) {
70             Middle_programmer mp = new Middle_programmer();
71             return mp;
72         }
73         if (type.equals("senior_programmer")) {
74             senior_programmer sp = new senior_programmer();
75             return sp;
76         }
77         return null;
78     }
79 }

 

环境:JDK1.6,MAVEN,tomcat,eclipse

源码地址:https://files.cnblogs.com/files/xiluhua/designPattern.rar

欢迎亲们评论指教。

posted @ 2015-04-09 22:00  xiluhua  阅读(185)  评论(0编辑  收藏  举报