首页 > 其他 > 详细

场景适用设计模式

时间:2015-03-26 10:25:20      阅读:182      评论:0      收藏:0      [点我收藏+]

1.工厂方法模式

当一个类新建时,需要依赖很多其他类时,如:创建汽车类时,需要先组装轮子,椅子,方向盘等等。

  1. interface IFactory {  
  2.     public ICar createCar();  
  3. }  
  4. class Factory implements IFactory {  
  5.     public ICar createCar() {  
  6.         Engine engine = new Engine();  
  7.         Underpan underpan = new Underpan();  
  8.         Wheel wheel = new Wheel();  
  9.         ICar car = new Car(underpan, wheel, engine);  
  10.         return car;  
  11.     }  
  12. }  
  13. public class Client {  
  14.     public static void main(String[] args) {  
  15.         IFactory factory = new Factory();  
  16.         ICar car = factory.createCar();  
  17.         car.show();  
  18.     }  

场景适用设计模式

原文:http://www.cnblogs.com/ztSpace/p/4367747.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!