清单6:StandaloneEditorAssembler.java
1 public class StandaloneEditorAssembler extends AbstractEditorAssembler {
2
3 private String compositeClassID;
4
5 private IEditorComposite bodyComposite;
6
7 /**
8
9 *
10
11 * @param compositeClassID
12
13 * :composite class qulified name,e.g. com.ibm..XXComposite;
14
15 */
16
17 public StandaloneEditorAssembler(String compositeClassID) {
18
19 this.compositeClassID = compositeClassID;
20
21 }
22
23 public void create(IEditorFacade editor) {
24
25 bodyComposite = createComposite(editor, compositeClassID);
26
27 if (bodyComposite != null)
28
29 bodyComposite.loadDataInfo();
30
31 }
32
33 public void showPreInfo() {
34
35 bodyComposite.showPreInfo();
36
37 }
38
39 }
40
41
2
3 private String compositeClassID;
4
5 private IEditorComposite bodyComposite;
6
7 /**
8
9 *
10
11 * @param compositeClassID
12
13 * :composite class qulified name,e.g. com.ibm..XXComposite;
14
15 */
16
17 public StandaloneEditorAssembler(String compositeClassID) {
18
19 this.compositeClassID = compositeClassID;
20
21 }
22
23 public void create(IEditorFacade editor) {
24
25 bodyComposite = createComposite(editor, compositeClassID);
26
27 if (bodyComposite != null)
28
29 bodyComposite.loadDataInfo();
30
31 }
32
33 public void showPreInfo() {
34
35 bodyComposite.showPreInfo();
36
37 }
38
39 }
40
41
接下来,是 EditorCompositeFactory 的实现,这个类的实现比较简单,只是根据类名产生类:
清单7:EditorCompositeFactory.java
1 public class EditorCompositeFactory {
2
3 /**
4
5 * create IEditorComposite
6
7 * @param clsName
8
9 * @param editor
10
11 * @return
12
13 */
14
15 public static IEditorComposite createComposite(String clsName,
16
17 IEditorFacade editor) {
18
19 IEditorComposite composite = null;
20
21 try {
22
23 Class cls = Class.forName(clsName);
24
25 if (cls != null)
26
27 composite = (IEditorComposite) cls.newInstance();
28
29 } catch (Exception e) {
30
31 e.printStackTrace();
32
33 }
34
35 if (composite != null) {
36
37 composite.setEditor(editor);
38
39 }
40
41 return composite;
42
43 }
44
45 }
46
47
2
3 /**
4
5 * create IEditorComposite
6
7 * @param clsName
8
9 * @param editor
10
11 * @return
12
13 */
14
15 public static IEditorComposite createComposite(String clsName,
16
17 IEditorFacade editor) {
18
19 IEditorComposite composite = null;
20
21 try {
22
23 Class cls = Class.forName(clsName);
24
25 if (cls != null)
26
27 composite = (IEditorComposite) cls.newInstance();
28
29 } catch (Exception e) {
30
31 e.printStackTrace();
32
33 }
34
35 if (composite != null) {
36
37 composite.setEditor(editor);
38
39 }
40
41 return composite;
42
43 }
44
45 }
46
47
最后,就是 EditorComposite 的实现了,很显然每个界面的 EditorComposite 都不一样,所以我们在这里只定义了一个接口来规范一下行为,具体的 EditorComposite 实现我会在代码附件中的测试包中给出。
清单8:IEditorComposite.java
1 public interface IEditorComposite {
2
3 /** set up composite UI */
4
5 public void create(Composite parent);
6
7 /** set the current editor for shell close and data set */
8
9 public void setEditor(IEditorFacade editor);
10
11 /** show previous data information in UI */
12
13 public void showPreInfo();
14
15 public void loadDataInfo();
16
17 }
18
19
2
3 /** set up composite UI */
4
5 public void create(Composite parent);
6
7 /** set the current editor for shell close and data set */
8
9 public void setEditor(IEditorFacade editor);
10
11 /** show previous data information in UI */
12
13 public void showPreInfo();
14
15 public void loadDataInfo();
16
17 }
18
19
下面,我们编写一些测试代码来测试它,这个测试应用是要编写一个电话簿,为了简单起见我只定义了一个 EditorComposite-PhoneBookComposite, 在编写组装逻辑时也只是示例性地改变了一下界面的标题和尺寸。(详细代码见代码下载)
清单9:PhoneBookEditorAssembler.java
1 public void create(IEditorFacade editor) {
2
3 if (compositeType == 0) {
4
5 //it is a phone book.
6
7 bodyComposite = createComposite(editor, "test.PhoneBookComposite");
8
9 |-------10--------20--------30--------40--------50--------60--------70--------80--------9|
10
11 |-------- XML error: The previous line is longer than the max of 90 characters ---------|
12
13 editor.getShell().setText("Phone Book");
14
15 editor.getShell().setSize(400, 300);
16
17 editor.getShell().redraw();
18
19 if (bodyComposite != null)
20
21 bodyComposite.loadDataInfo();
22
23 } else if (compositeType == 1) {
24
25 //it is a memo book.
26
27 bodyComposite = createComposite(editor, "test.PhoneBookComposite");
28
29 |-------10--------20--------30--------40--------50--------60--------70--------80--------9|
30
31 |-------- XML error: The previous line is longer than the max of 90 characters ---------|
32
33 editor.getShell().setText("Memo Book");
34
35 editor.getShell().setSize(500, 300);
36
37 editor.getShell().redraw();
38
39 if (bodyComposite != null)
40
41 bodyComposite.loadDataInfo();
42
43 }
44
45 }
46
47
2
3 if (compositeType == 0) {
4
5 //it is a phone book.
6
7 bodyComposite = createComposite(editor, "test.PhoneBookComposite");
8
9 |-------10--------20--------30--------40--------50--------60--------70--------80--------9|
10
11 |-------- XML error: The previous line is longer than the max of 90 characters ---------|
12
13 editor.getShell().setText("Phone Book");
14
15 editor.getShell().setSize(400, 300);
16
17 editor.getShell().redraw();
18
19 if (bodyComposite != null)
20
21 bodyComposite.loadDataInfo();
22
23 } else if (compositeType == 1) {
24
25 //it is a memo book.
26
27 bodyComposite = createComposite(editor, "test.PhoneBookComposite");
28
29 |-------10--------20--------30--------40--------50--------60--------70--------80--------9|
30
31 |-------- XML error: The previous line is longer than the max of 90 characters ---------|
32
33 editor.getShell().setText("Memo Book");
34
35 editor.getShell().setSize(500, 300);
36
37 editor.getShell().redraw();
38
39 if (bodyComposite != null)
40
41 bodyComposite.loadDataInfo();
42
43 }
44
45 }
46
47
清单10:Main.java
1 public static void main(String[] args) {
2
3 //定义PhoneBook EditorAssembler。
4
5 IEditorAssembler assembler = new PhoneBookEditorAssembler(0);
6
7 //定义PhoneBook 输入数据
8
9 IInputDataObject inputData = new PhoneBookInputDO("LYL", "010-8000100");
10
11 //定义PhoneBook editor
12
13 EditorFacade editor = new EditorFacade(assembler, inputData);
14
15 editor.show();
16
17 if (editor.isFinishedOK()) {
18
19 //取出PhoneBook 输出数据。
20
21 if (editor.getOutputData() instanceof PhoneBookOutputDO) {
22
23 PhoneBookOutputDO outputData = (PhoneBookOutputDO) editor
24
25 .getOutputData();
26
27 String name = outputData.getName();
28
29 String phone = outputData.getPhone();
30
31 System.out.println("name:" + name + "; phone:" + phone);
32
33 }
34
35 }
36
37 }
38
39
2
3 //定义PhoneBook EditorAssembler。
4
5 IEditorAssembler assembler = new PhoneBookEditorAssembler(0);
6
7 //定义PhoneBook 输入数据
8
9 IInputDataObject inputData = new PhoneBookInputDO("LYL", "010-8000100");
10
11 //定义PhoneBook editor
12
13 EditorFacade editor = new EditorFacade(assembler, inputData);
14
15 editor.show();
16
17 if (editor.isFinishedOK()) {
18
19 //取出PhoneBook 输出数据。
20
21 if (editor.getOutputData() instanceof PhoneBookOutputDO) {
22
23 PhoneBookOutputDO outputData = (PhoneBookOutputDO) editor
24
25 .getOutputData();
26
27 String name = outputData.getName();
28
29 String phone = outputData.getPhone();
30
31 System.out.println("name:" + name + "; phone:" + phone);
32
33 }
34
35 }
36
37 }
38
39
接下来,我们可以看一下架构的实现模型,注意,我在画下面的 UML 图时采用了分层的方式,所有的接口都会在上面一层,实现在下面一层,这种分层画 UML 图的方法有助于我们理清架构的思路,也便于与开发组的其他成员沟通。
图5. 架构的实现模型

至此,我们完成了界面组装器的核心架构的实现,注意,这只是一种实现,并不是界面组装模式的全部,作为一种模式,它必须有更广的外延,下面我们将要探讨它的模式本质。