`
biqing0427
  • 浏览: 55138 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

我工作的那点事--学习《设计模式》实例应用(composite模式)

阅读更多
早上一到公司就看到德国那边的新需求,唉,没办法我们睡觉做着美梦的时候,那边正在想着让我们怎么工作充实呢。别人安排完了睡觉,我们醒来就要完成别人想的,没有办法,工作嘛,就这样了……

德国那边通常会把一个项目给我们中国这边的一个经理,然后经理安排developers开发各自的模块,然后最后自己组装各个模块,形成产品。德国那边不管我们怎么分工的,最后他们只要成品的。

这种工作模式 其实有点像builder的,各个模块分开,最后组装。不过今天这里我想用下composite模式。

1.组件:Component 一个工作组

public abstract class Component {
public abstract int add(int a[]);//假设产品加的功能

public abstract int del(int a[]);//假设产品减的功能

public abstract int mul(int a[]);//假设产品乘的功能

public void addComponent(Component comp) {
};//负责增加小组成员

public void removeComponent(Component comp) {
};//负责减少小组成员

public Component getChildComponent(int i) {
  return null;
};//得到小组成员,这样可以直接和他进行对话
}


2.项目经理:Composite 负责组装 和 德国的交互
import java.util.ArrayList;
import java.util.List;

public class Composite {

List leaf = new ArrayList();

//组装加的功能
public int add(int a[]){
  int iResult = 0;
  for(int i=0;i<leaf.size();i++){
    iResult +=((Component)leaf.get(i)).add(a);
  }
  return iResult;
};

//组装减的功能
public int del(int a[]){
  int iResult = 0;
  for(int i=0;i<leaf.size();i++){
    iResult +=((Component)leaf.get(i)).del(a);
  }
  return iResult;
};

//组装乘的功能
public int mul(int a[]){
  int iResult = 0;
  for(int i=0;i<leaf.size();i++){
    iResult +=((Component)leaf.get(i)).mul(a);
  }
  return iResult;
};

//增加小组成员
public void addComponent(Component comp) {
  leaf.add(comp);
};

//减少小组成员
public void removeComponent(Component comp) {
  int i = leaf.indexOf(comp);
  if(i>=0){
  leaf.remove(i);
  }
};

//提供德国那边直接和某个成员直接交互
public Component getChildComponent(int i) {
   Component comp = null;
   if(leaf.size()>0&&i<leaf.size()){
     comp = (Component)leaf.get(i);
   }
   return comp;
};
}

3.小组成员A,B,C,D
//小组成员A
public class LeafA extends Component {

public int add(int a[]) {
  int iResult = 0;
  for (int i = 0; i < a.length; i++) {
    iResult += a[i];
  }
  System.out.println("call leafA.add()");
  return iResult;
}

public int del(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
     iResult = a[i] - iResult;
   }
   System.out.println("call leafA.del()");
   return iResult;
}

public int mul(int a[]) {
   int iResult = 1;
   for (int i = 0; i < a.length; i++) {
     iResult = iResult * a[i];
   }
   System.out.println("call leafA.mul()");
   return iResult;
}
}

//小组成员B
public class LeafB extends Component {

public int add(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
     iResult += a[i];
   }
   System.out.println("call leafB.add()");
   return iResult * 2;
}

public int del(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
     iResult = a[i] - iResult;
   }
   System.out.println("call leafB.del()");
   return iResult * 2;
}

public int mul(int a[]) {
   int iResult = 1;
   for (int i = 0; i < a.length; i++) {
     iResult = iResult * a[i];
   }
   System.out.println("call leafB.mul()");
   return iResult * 2;
}
}

//小组成员C
public class LeafC extends Component {

public int add(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
      iResult += a[i];
   }
   System.out.println("call leafC.add()");
   return iResult * 3;
}

public int del(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
      iResult = a[i] - iResult;
   }
   System.out.println("call leafC.del()");
   return iResult * 3;
}

public int mul(int a[]) {
   int iResult = 1;
   for (int i = 0; i < a.length; i++) {
       iResult = iResult * a[i];
   }
   System.out.println("call leafC.mul()");
   return iResult * 3;
}
}

//小组成员D
public class LeafD extends Component {

public int add(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
      iResult += a[i];
   }
   System.out.println("call leafD.add()");
   return iResult * 4;
}

public int del(int a[]) {
   int iResult = 0;
   for (int i = 0; i < a.length; i++) {
      iResult = a[i] - iResult;
   }
   System.out.println("call leafD.del()");
   return iResult * 4;
}

public int mul(int a[]) {
   int iResult = 1;
   for (int i = 0; i < a.length; i++) {
      iResult = iResult * a[i];
   }
   System.out.println("call leafD.mul()");
   return iResult * 4;
}
}


4.看看整个过程吧:
public void testComposite() {

Composite composite = new Composite();//任命项目经理
LeafA leafA = new LeafA();//成员A
LeafB leafB = new LeafB();//成员B
LeafC leafC = new LeafC();//成员C
LeafD leafD = new LeafD();//成员D

composite.addComponent(leafA);//将A加入项目
composite.addComponent(leafB);//将A加入项目
composite.addComponent(leafC);//将A加入项目
composite.addComponent(leafD);//将A加入项目
int a[] = {1,2,3,4,5,6,7,8,9};

//composite.add(a);德国要求得到加的功能
System.out.println("composite.add(a):"+composite.add(a));

//composite.del(a);德国要求得到减的功能
System.out.println("composite.del(a):"+composite.del(a));

//composite.mul(a);德国要求得到乘的功能
System.out.println("composite.mul(a):"+composite.mul(a));

composite.removeComponent(leafA);

//composite.add(a);LeafA 离职了
System.out.println("composite removes leafA");
System.out.println("composite.add(a):"+composite.add(a));

//composite.del(a);
System.out.println("composite.del(a):"+composite.del(a));

//composite.mul(a);
System.out.println("composite.mul(a):"+composite.mul(a));

//composite.add(a);德国那边要求详细看看B的工作情况
LeafB b = (LeafB)composite.getChildComponent(0);

System.out.println("directly ask leafB");
System.out.println("composite.add(a):"+b.add(a));

//composite.del(a);
System.out.println("composite.del(a):"+b.del(a));

//composite.mul(a);
System.out.println("composite.mul(a):"+b.mul(a));
}


结果如下:

call leafA.add()
call leafB.add()
call leafC.add()
call leafD.add()
composite.add(a):450
call leafA.del()
call leafB.del()
call leafC.del()
call leafD.del()
composite.del(a):50
call leafA.mul()
call leafB.mul()
call leafC.mul()
call leafD.mul()
composite.mul(a):3628800
composite removes leafA
call leafB.add()
call leafC.add()
call leafD.add()
composite.add(a):405
call leafB.del()
call leafC.del()
call leafD.del()
composite.del(a):45
call leafB.mul()
call leafC.mul()
call leafD.mul()
composite.mul(a):3265920
directly ask leafB
call leafB.add()
composite.add(a):90
call leafB.del()
composite.del(a):10
call leafB.mul()
composite.mul(a):725760
分享到:
评论

相关推荐

    设计模式:可复用面向对象软件的基础--详细书签版

    因此我们欢迎广大读者的批评与指正,无论从书中采用的实例、参考,还是我们遗漏的已知应用,或应该包含的设计模式等方面。你可以通过Addison-Wesley写信给我们,或发送电子邮件到:design-patterns@cs.uiuc.edu。你...

    深入浅出设计模式(中文版)

    1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...

    设计模式--C++

    1.1 什么是设计模式 2 1.2 Smalltalk MVC 中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    深入浅出设计模式(中文版电子版)

    1.1什么是设计模式 2 1.2设计模式的作用 3 1.3GRASP模式的分类 4 1.4GoF设计模式的分类 4 1.5模式的学习阶段 6 第2章负责任地设计对象——GRASP 9 2.1InformationExpert(信息专家) 11 2.2Creator(创造者)...

    设计模式可复用面向对象软件的基础.zip

    本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用...

    《设计模式》中文版(23个设计模式的介绍与运用)

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    设计模式在地图制图软件开发中的应用

    摘要:数字地图制图实际上是...文中从数字地图制图软件的需求出发,对制图软件设计中常用设计模式(包括MVC模式、OBSERVER模式、COMPOSITE模式、COMMAND模式)的一般原理和结构进行简要介绍,并通过实例进行具体的说明。

    33种JAVA设计模式DEMO

    这些设计模式提供了一种在创建对象的同时隐藏创建逻辑的方式,而不是使用 new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。 工厂模式(Factory Pattern) 抽象工厂模式...

    设计模式 GOF 23

    本书设计实例从面向对象的设计中精选出23个设计模式,总结了面向对象设计中最有价值的经验,并且用简洁可复用的形式表达出来。本书分类描述了一组设计良好,表达清楚的软件设计模式,这些模式在实用环境下有特别有用...

    设计模式_组合模式.zip

    这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。 这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。 我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的...

    设计模式(.PDF)

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    GOLF设计模式(C++语言版)

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 ...

    《国外写的,翻译版本》设计模式

    最出名的设计模式,语言诙谐明了。 目 录 序言 前言 读者指南 第1章 引言 1 1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决...

    软件设计师必读的书-设计模式

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    设计模式--可复用面向对象软件的基础

    1.2 Smalltalk MVC中的设计模式 1.3 描述设计模式 1.4 设计模式的编目 1.5 组织编目 1.6 设计模式怎样解决设计问题 1.7 怎样选择设计模式 1.8 怎样使用设计模式 第二章 实例研究:设计一个文档编辑器 2.1 设计问题...

    Erich Gamma、Richard Helm、Ralph Johnson和John Vlissides23种设计模式

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    二十三种设计模式【PDF版】

    为能和大家能共同探讨"设计模式",我将自己在学习中的心得写下来,只是想帮助更多人更容易理解 GoF 的《设计模式》。由 于原著都是以C++为例, 以Java为例的设计模式基本又都以图形应用为例,而我们更关心Java在中间件等...

    设计模式文档

    1.1 什么是设计模式 2 1.2 Smalltalk MVC中的设计模式 3 1.3 描述设计模式 4 1.4 设计模式的编目 5 1.5 组织编目 7 1.6 设计模式怎样解决设计问题 8 1.6.1 寻找合适的对象 8 1.6.2 决定对象的粒度 9 1.6.3 指定对象...

    C#23种设计模式_示例源代码及PDF

    5、 、 SINGLETON —俺有 6 个漂亮的老婆, 她们的老公都是我, 我就是我们家里的老公 Sigleton, 她们只要说道“老公”,都是指的同一个人,那就是我(刚才做了个梦啦,哪有这么好的事) 单例模式: 而且自行实例...

Global site tag (gtag.js) - Google Analytics