我工作的那點事--學習《設計模式》例項應用(composite模式)

biqing0427發表於2007-12-04
早上一到公司就看到德國那邊的新需求,唉,沒辦法我們睡覺做著美夢的時候,那邊正在想著讓我們怎麼工作充實呢。別人安排完了睡覺,我們醒來就要完成別人想的,沒有辦法,工作嘛,就這樣了……[@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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章