码迷,mamicode.com
首页 > 编程语言 > 详细

Java 工厂设计模式

时间:2016-10-15 11:24:06      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

 1 //接口的应用:工厂方法的设计模式
 2 public class TestFactoryMethod {
 3     public static void main(String[] args) {
 4         IWorkFactory i = new StudentWorkFactory();
 5         i.getWork().doWork();
 6         
 7         IWorkFactory i1 = new TeacherWorkFactory();
 8         i1.getWork().doWork();
 9     }
10 }
11 
12 interface IWorkFactory{
13     Work getWork();
14 }
15 class StudentWorkFactory implements IWorkFactory{
16 
17     @Override
18     public Work getWork() {
19         return new StudentWork();
20     }
21     
22 }
23 class TeacherWorkFactory implements IWorkFactory{
24 
25     @Override
26     public Work getWork() {
27         return new TeacherWork();
28     }
29     
30 }
31 
32 interface Work{
33     void doWork();
34 }
35 
36 class StudentWork implements Work{
37 
38     @Override
39     public void doWork() {
40         System.out.println("学生写作业");
41     }
42     
43 }
44 class TeacherWork implements Work{
45 
46     @Override
47     public void doWork() {
48         System.out.println("老师批改作业");
49     }
50     
51 }

 

Java 工厂设计模式

标签:

原文地址:http://www.cnblogs.com/longc/p/5962903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!