CDO的全称为Common Data Object 是公用数据结构的意思,这是抽象的一种新型的数据结构,可以设置基本的数据类型,比如:byte、short、int、long、float、double、String,也可以存储这些类型的数组,同样,CDO里可以嵌套新的CDO或者CDO数组,这种数据结构几乎是万能的数据结构,它可以很方便的转化为XML表示,也可以很方便的转化为Json的表示,同样XML或者Json也可以很方便的转化为CDO对象,设想有如下的代码:
如果将它转化为XML表示如下:
如果将这个对象转化为Json,将会有如下的表示:
每个实体bean对应一个Class,如果实体多的话维护起来会很不方便,通过CDO,省去了这些代码:
它们的XML表示分别为:
通过如下的代码便可转化为CDO对象:
这样便可打印出如下的Json字符串:
01 | CDO cdo1= new CDO(); |
02 | cdo1.setIntegerValue( "classid" , 1 ); |
03 | cdo1.setStringValue( "classname" , "一年级一班" ); |
04 | cdo1.setStringArrayValue( "alias" , new String[]{ "尖子班级" , "优秀班级" } ); |
05 | cdo1.setStringArrayValue( "students" , new String[]{ "张三" , "李四" }); |
06 | CDO scdo1= new CDO(); |
07 | scdo1.setStringValue( "username" , "userone" ); |
08 | scdo1.setStringValue( "password" , "......." ); |
09 | scdo1.setIntegerValue( "age" , 18 ); |
10 | cdo1.setCDOValue( "scdo1" , scdo1); |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < CDO > |
03 | < NF N = "classid" V = "1" /> |
04 | < STRF N = "classname" V = "一年级一班" /> |
05 | < STRAF N = "alias" > |
06 | < STR >尖子班级</ STR > |
07 | < STR >优秀班级</ STR > |
08 | </ STRAF > |
09 | < STRAF N = "students" > |
10 | < STR >张三</ STR > |
11 | < STR >李四</ STR > |
12 | </ STRAF > |
13 | < CDOF N = "scdo1" > |
14 | < CDO > |
15 | < STRF N = "username" V = "userone" /> |
16 | < STRF N = "password" V = "..........." /> |
17 | < NF N = "age" V = "18" /> |
18 | </ CDO > |
19 | </ CDOF > |
20 | </ CDO > |
01 | { |
02 | "classid" : 1 , |
03 | "classname" : "一年级一班" , |
04 | "alias" :[ |
05 | "尖子班级" , |
06 | "优秀班级" |
07 | ], |
08 | "students" :[ |
09 | "张三" , |
10 | "李四" |
11 | ], |
12 | "scdo1" :{ |
13 | "username" : "userone" , |
14 | "password" : "..........." , |
15 | "age" : 18 |
16 | } |
17 | } |
可以看出CDO这个数据结构还是设计的是非常优秀的。
在我们以往的开发中,如果大家稍加思索的话,大家都在写好多好多的bean,每个bean对应一个实体Entity,这样在开发大型的项目的过程中会感觉不合理,通过CDO这个结构,不需要针对每个entity都编写一个java bean类,假若我们有学生和班级两类实体,如果是按照以往的设计,可能会先设计出两个bean class:
01 | package com.google.beans; |
02 |
03 | public class Student |
04 | { |
05 | private Integer id; |
06 | private String username; |
07 | private String address; |
08 | private boolean sex; |
09 | private double height; |
10 | public Integer getId() |
11 | { |
12 | return id; |
13 | } |
14 | public void setId(Integer id) |
15 | { |
16 | this .id = id; |
17 | } |
18 | public String getUsername() |
19 | { |
20 | return username; |
21 | } |
22 | public void setUsername(String username) |
23 | { |
24 | this .username = username; |
25 | } |
26 | public String getAddress() |
27 | { |
28 | return address; |
29 | } |
30 | public void setAddress(String address) |
31 | { |
32 | this .address = address; |
33 | } |
34 | public boolean isSex() |
35 | { |
36 | return sex; |
37 | } |
38 | public void setSex( boolean sex) |
39 | { |
40 | this .sex = sex; |
41 | } |
42 | public double getHeight() |
43 | { |
44 | return height; |
45 | } |
46 | public void setHeight( double height) |
47 | { |
48 | this .height = height; |
49 | } |
50 | } |
01 | package com.google.beans; |
02 |
03 | public class Classes |
04 | { |
05 | private Integer classId; |
06 | private String className; |
07 | private Student[] students; |
08 | public Integer getClassId() |
09 | { |
10 | return classId; |
11 | } |
12 | public void setClassId(Integer classId) |
13 | { |
14 | this .classId = classId; |
15 | } |
16 | public String getClassName() |
17 | { |
18 | return className; |
19 | } |
20 | public void setClassName(String className) |
21 | { |
22 | this .className = className; |
23 | } |
24 | public Student[] getStudents() |
25 | { |
26 | return students; |
27 | } |
28 | public void setStudents(Student[] students) |
29 | { |
30 | this .students = students; |
31 | } |
32 | } |
01 | CDO cdoStudent1 = new CDO(); |
02 | cdoStudent1.setIntegerValue( "id" , 1 ); |
03 | cdoStudent1.setStringValue( "username" , "userone" ); |
04 | cdoStudent1.setStringValue( "address" , "上海市。。。。。。" ); |
05 | cdoStudent1.setBooleanValue( "sex" , true ); |
06 | cdoStudent1.setDoubleValue( "height" , 1.74 ); |
07 |
08 | CDO cdoStudent2 = new CDO(); |
09 | cdoStudent2.setIntegerValue( "id" , 2 ); |
10 | cdoStudent2.setStringValue( "username" , "usertwo" ); |
11 | cdoStudent2.setStringValue( "address" , "深圳市" ); |
12 | cdoStudent2.setBooleanValue( "sex" , false ); |
13 | cdoStudent2.setDoubleValue( "height" , 1.67 ); |
14 |
15 | CDO cdoClasses = new CDO(); |
16 | cdoClasses.setIntegerValue( "classId" , 1 ); |
17 | cdoClasses.setStringValue( "className" , "一年级一班" ); |
18 | cdoClasses.setCDOArrayValue( "students" , new CDO[]{cdoStudent1,cdoStudent2}); |
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
2 | < CDO > |
3 | < NF N = "id" V = "1" /> |
4 | < STRF N = "username" V = "userone" /> |
5 | < STRF N = "address" V = "上海市。。。。。。" /> |
6 | < BF N = "sex" V = "true" /> |
7 | < DBLF N = "height" V = "1.74" /> |
8 | </ CDO > |
1 | <? xml version = "1.0" encoding = "UTF-8" ?> |
2 | < CDO > |
3 | < NF N = "id" V = "2" /> |
4 | < STRF N = "username" V = "usertwo" /> |
5 | < STRF N = "address" V = "深圳市" /> |
6 | < BF N = "sex" V = "false" /> |
7 | < DBLF N = "height" V = "1.67" /> |
8 | </ CDO > |
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < CDO > |
03 | < NF N = "classId" V = "1" /> |
04 | < STRF N = "className" V = "一年级一班" /> |
05 | < CDOAF N = "students" > |
06 | < CDO > |
07 | < NF N = "id" V = "1" /> |
08 | < STRF N = "username" V = "userone" /> |
09 | < STRF N = "address" V = "上海市。。。。。。" /> |
10 | < BF N = "sex" V = "true" /> |
11 | < DBLF N = "height" V = "1.74" /> |
12 | </ CDO > |
13 | < CDO > |
14 | < NF N = "id" V = "2" /> |
15 | < STRF N = "username" V = "usertwo" /> |
16 | < STRF N = "address" V = "深圳市" /> |
17 | < BF N = "sex" V = "false" /> |
18 | < DBLF N = "height" V = "1.67" /> |
19 | </ CDO > |
20 | </ CDOAF > |
21 | </ CDO > |
非常的清晰明了!
CDO不但能方便地转化为XML表示,而且能很方便的把XML文本串转化为CDO对象,假设有如下的XML文本串:
01 | <?xml version= "1.0" encoding= "UTF-8" ?> |
02 | <CDO> |
03 | <CDOF N= "cdoReturn" > |
04 | <CDO> |
05 | <NF N= "nCode" V= "0" /> |
06 | <STRF N= "strText" V= "OK" /> |
07 | <STRF N= "strInfo" V= "OK" /> |
08 | </CDO> |
09 | </CDOF> |
10 | <CDOF N= "cdoResponse" > |
11 | <CDO> |
12 | <CDOAF N= "cdosMember" > |
13 | <CDO> |
14 | <NF N= "uid" V= "5" /> |
15 | <STRF N= "username" V= "wanghui" /> |
16 | <STRF N= "password" V= "e4994c81a0c0db8e1ab137f9fdf7d5ae" /> |
17 | <STRF N= "secques" V= "" /> |
18 | </CDO> |
19 | <CDO> |
20 | <NF N= "uid" V= "353" /> |
21 | <STRF N= "username" V= "bainiuwang" /> |
22 | <STRF N= "password" V= "35b36dd98779303e3cdc84ff3951be9e" /> |
23 | <STRF N= "secques" V= "" /> |
24 | </CDO> |
25 | <CDO> |
26 | <NF N= "uid" V= "636" /> |
27 | <STRF N= "username" V= "wangli3849" /> |
28 | <STRF N= "password" V= "e3b2b5b80d57c3254c0ed1a83059d7ce" /> |
29 | <STRF N= "secques" V= "" /> |
30 | </CDO> |
31 | <CDO> |
32 | <NF N= "uid" V= "1006" /> |
33 | <STRF N= "username" V= "wangle520208" /> |
34 | <STRF N= "password" V= "059e5d9a6347d61859d7e2b740ef721b" /> |
35 | <STRF N= "secques" V= "" /> |
36 | </CDO> |
37 | <CDO> |
38 | <NF N= "uid" V= "1087" /> |
39 | <STRF N= "username" V= "wangyuepeng" /> |
40 | <STRF N= "password" V= "a5c74d745f45875d62fb418ed7e65ebb" /> |
41 | <STRF N= "secques" V= "" /> |
42 | </CDO> |
43 | </CDOAF> |
44 | <NF N= "count" V= "5" /> |
45 | </CDO> |
46 | </CDOF> |
47 | </CDO> |
01 | StringBuilder builder = new StringBuilder(); |
02 | builder.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ); |
03 | builder.append( "<CDO>" ); |
04 | builder.append( " <CDOF N=\"cdoReturn\">" ); |
05 | builder.append( " <CDO>" ); |
06 | builder.append( " <NF N=\"nCode\" V=\"0\"/>" ); |
07 | builder.append( " <STRF N=\"strText\" V=\"OK\"/>" ); |
08 | builder.append( " <STRF N=\"strInfo\" V=\"OK\"/>" ); |
09 | builder.append( " </CDO>" ); |
10 | builder.append( " </CDOF>" ); |
11 | builder.append( " <CDOF N=\"cdoResponse\">" ); |
12 | builder.append( " <CDO>" ); |
13 | builder.append( " <CDOAF N=\"cdosMember\">" ); |
14 | builder.append( " <CDO>" ); |
15 | builder.append( " <NF N=\"uid\" V=\"5\"/>" ); |
16 | builder.append( " <STRF N=\"username\" V=\"wanghui\"/>" ); |
17 | builder.append( " <STRF N=\"password\" V=\"e4994c81a0c0db8e1ab137f9fdf7d5ae\"/>" ); |
18 | builder.append( " <STRF N=\"secques\" V=\"\"/>" ); |
19 | builder.append( " </CDO>" ); |
20 | builder.append( " <CDO>" ); |
21 | builder.append( " <NF N=\"uid\" V=\"353\"/>" ); |
22 | builder.append( " <STRF N=\"username\" V=\"bainiuwang\"/>" ); |
23 | builder.append( " <STRF N=\"password\" V=\"35b36dd98779303e3cdc84ff3951be9e\"/>" ); |
24 | builder.append( " <STRF N=\"secques\" V=\"\"/>" ); |
25 | builder.append( " </CDO>" ); |
26 | builder.append( " <CDO>" ); |
27 | builder.append( " <NF N=\"uid\" V=\"636\"/>" ); |
28 | builder.append( " <STRF N=\"username\" V=\"wangli3849\"/>" ); |
29 | builder.append( " <STRF N=\"password\" V=\"e3b2b5b80d57c3254c0ed1a83059d7ce\"/>" ); |
30 | builder.append( " <STRF N=\"secques\" V=\"\"/>" ); |
31 | builder.append( " </CDO>" ); |
32 | builder.append( " <CDO>" ); |
33 | builder.append( " <NF N=\"uid\" V=\"1006\"/>" ); |
34 | builder.append( " <STRF N=\"username\" V=\"wangle520208\"/>" ); |
35 | builder.append( " <STRF N=\"password\" V=\"059e5d9a6347d61859d7e2b740ef721b\"/>" ); |
36 | builder.append( " <STRF N=\"secques\" V=\"\"/>" ); |
37 | builder.append( " </CDO>" ); |
38 | builder.append( " <CDO>" ); |
39 | builder.append( " <NF N=\"uid\" V=\"1087\"/>" ); |
40 | builder.append( " <STRF N=\"username\" V=\"wangyuepeng\"/>" ); |
41 | builder.append( " <STRF N=\"password\" V=\"a5c74d745f45875d62fb418ed7e65ebb\"/>" ); |
42 | builder.append( " <STRF N=\"secques\" V=\"\"/>" ); |
43 | builder.append( " </CDO>" ); |
44 | builder.append( " </CDOAF>" ); |
45 | builder.append( " <NF N=\"count\" V=\"5\"/>" ); |
46 | builder.append( " </CDO>" ); |
47 | builder.append( " </CDOF>" ); |
48 | builder.append( "</CDO>" ); |
49 | |
50 | CDO cdoResponse = CDO.fromXML(builder.toString()); |
51 | System.out.println(cdoResponse.toJSON()); |
01 | { |
02 | "cdoReturn" :{ |
03 | "nCode" : 0 , |
04 | "strText" : "OK" , |
05 | "strInfo" : "OK" |
06 | }, |
07 | "cdoResponse" :{ |
08 | "cdosMember" :[ |
09 | { |
10 | "uid" : 5 , |
11 | "username" : "wanghui" , |
12 | "password" : "e4994c81a0c0db8e1ab137f9fdf7d5ae" , |
13 | "secques" : "" |
14 | }, |
15 | { |
16 | "uid" : 353 , |
17 | "username" : "bainiuwang" , |
18 | "password" : "35b36dd98779303e3cdc84ff3951be9e" , |
19 | "secques" : "" |
20 | }, |
21 | { |
22 | "uid" : 636 , |
23 | "username" : "wangli3849" , |
24 | "password" : "e3b2b5b80d57c3254c0ed1a83059d7ce" , |
25 | "secques" : "" |
26 | }, |
27 | { |
28 | "uid" : 1006 , |
29 | "username" : "wangle520208" , |
30 | "password" : "059e5d9a6347d61859d7e2b740ef721b" , |
31 | "secques" : "" |
32 | }, |
33 | { |
34 | "uid" : 1087 , |
35 | "username" : "wangyuepeng" , |
36 | "password" : "a5c74d745f45875d62fb418ed7e65ebb" , |
37 | "secques" : "" |
38 | } |
39 | ], |
40 | "count" : 5 |
41 | } |
42 | } |
确实是非常的方便!
CDO数据结构是整个CDO框架的核心,理解并掌握了这个数据接口,可以说掌握了CDO框架的百分之六十!