码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之工厂模式

时间:2014-10-27 15:41:24      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:java   设计模式   工厂模式   

??

interface Work{
 public void doWork();
}

class StudentWork implements Work{

 @Override
 public void doWork() {
  System.out.println("student 写作业");
 }
 
}

class TeacherWork implements Work{

 @Override
 public void doWork() {
  System.out.println("teacher 批改作业");
 }
}

interface WorkFactory{
 public Work getWork();
}

class StudentWorkFactory implements WorkFactory{

 @Override
 public Work getWork() {
  return new StudentWork();
 }
 
}

class TeacherWorkFactory implements WorkFactory{

 @Override
 public Work getWork() {
  return new TeacherWork();
 }
 
}



public class TestFactory {

 /**
  * @Title: main
  * @Description:
  * @param:
  * @return void 
  * @user: wangzg
  * @Date:2014-10-27
  * @throws
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub

  WorkFactory workFactory1 = new StudentWorkFactory();
  workFactory1.getWork().doWork();
  
  System.out.println();
  
  WorkFactory workFactory2 = new TeacherWorkFactory();
  workFactory2.getWork().doWork();
 }

}

设计模式之工厂模式

标签:java   设计模式   工厂模式   

原文地址:http://blog.csdn.net/sd_tz_wzg/article/details/40507427

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