ITPub博客

首页 > IT职业 > IT生活 > 我工作的那点事--学习《设计模式》实例应用(composite模式)

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

原创 IT生活 作者:biqing0427 时间:2007-12-04 11:35:16 0 删除 编辑
早上一到公司就看到德国那边的新需求,唉,没办法我们睡觉做着美梦的时候,那边正在想着让我们怎么工作充实呢。别人安排完了睡觉,我们醒来就要完成别人想的,没有办法,工作嘛,就这样了……[@more@]

德国那边通常会把一个项目给我们中国这边的一个经理,然后经理安排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 iResult +=((Component)leaf.get(i)).add(a);
}
return iResult;
};

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

//组装乘的功能
public int mul(int a[]){
int iResult = 0;
for(int i=0;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 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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9879276/viewspace-988275/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    24
  • 访问量
    11586