新型的几乎万能的数据结构CDO

CDO的全称为Common Data Object 是公用数据结构的意思,这是抽象的一种新型的数据结构,可以设置基本的数据类型,比如:byte、short、int、long、float、double、String,也可以存储这些类型的数组,同样,CDO里可以嵌套新的CDO或者CDO数组,这种数据结构几乎是万能的数据结构,它可以很方便的转化为XML表示,也可以很方便的转化为Json的表示,同样XML或者Json也可以很方便的转化为CDO对象,设想有如下的代码:
		CDO cdo1= new CDO();
		cdo1.setIntegerValue("classid", 1);
		cdo1.setStringValue("classname", "一年级一班");
		cdo1.setStringArrayValue("alias",new String[]{"尖子班级","优秀班级"} );
		cdo1.setStringArrayValue("students", new String[]{"张三","李四"});
		CDO scdo1= new CDO();
		scdo1.setStringValue("username", "userone");
		scdo1.setStringValue("password", ".......");
		scdo1.setIntegerValue("age", 18);
		cdo1.setCDOValue("scdo1", scdo1);

如果将它转化为XML表示如下:
<?xml version="1.0" encoding="UTF-8"?>
<CDO>
	<NF N="classid" V="1"/>
	<STRF N="classname" V="一年级一班"/>
	<STRAF N="alias">
		<STR>尖子班级</STR>
		<STR>优秀班级</STR>
	</STRAF>
	<STRAF N="students">
		<STR>张三</STR>
		<STR>李四</STR>
	</STRAF>
	<CDOF N="scdo1">
		<CDO>
			<STRF N="username" V="userone"/>
			<STRF N="password" V="..........."/>
			<NF N="age" V="18"/>
		</CDO>
	</CDOF>
</CDO>

如果将这个对象转化为Json,将会有如下的表示:
{
    "classid":1,
    "classname":"一年级一班",
    "alias":[
        "尖子班级",
        "优秀班级"
    ],
    "students":[
        "张三",
        "李四"
    ],
    "scdo1":{
        "username":"userone",
        "password":"...........",
        "age":18
    }
}

可以看出CDO这个数据结构还是设计的是非常优秀的。

在我们以往的开发中,如果大家稍加思索的话,大家都在写好多好多的bean,每个bean对应一个实体Entity,这样在开发大型的项目的过程中会感觉不合理,通过CDO这个结构,不需要针对每个entity都编写一个java bean类,假若我们有学生和班级两类实体,如果是按照以往的设计,可能会先设计出两个bean class:

package com.google.beans;

public class Student
{
	private Integer id;
	private String username;
	private String address;
	private boolean sex;
	private double height;
	public Integer getId()
	{
		return id;
	}
	public void setId(Integer id)
	{
		this.id = id;
	}
	public String getUsername()
	{
		return username;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public String getAddress()
	{
		return address;
	}
	public void setAddress(String address)
	{
		this.address = address;
	}
	public boolean isSex()
	{
		return sex;
	}
	public void setSex(boolean sex)
	{
		this.sex = sex;
	}
	public double getHeight()
	{
		return height;
	}
	public void setHeight(double height)
	{
		this.height = height;
	}
}

package com.google.beans;

public class Classes
{
	private Integer classId;
	private String className;
	private Student[] students;
	public Integer getClassId()
	{
		return classId;
	}
	public void setClassId(Integer classId)
	{
		this.classId = classId;
	}
	public String getClassName()
	{
		return className;
	}
	public void setClassName(String className)
	{
		this.className = className;
	}
	public Student[] getStudents()
	{
		return students;
	}
	public void setStudents(Student[] students)
	{
		this.students = students;
	}
}

每个实体bean对应一个Class,如果实体多的话维护起来会很不方便,通过CDO,省去了这些代码:
		CDO cdoStudent1 = new CDO();
		cdoStudent1.setIntegerValue("id", 1);
		cdoStudent1.setStringValue("username", "userone");
		cdoStudent1.setStringValue("address", "上海市。。。。。。");
		cdoStudent1.setBooleanValue("sex", true);
		cdoStudent1.setDoubleValue("height", 1.74);
		
		CDO cdoStudent2 = new CDO();
		cdoStudent2.setIntegerValue("id", 2);
		cdoStudent2.setStringValue("username", "usertwo");
		cdoStudent2.setStringValue("address", "深圳市");
		cdoStudent2.setBooleanValue("sex", false);
		cdoStudent2.setDoubleValue("height", 1.67);
		
		CDO cdoClasses = new CDO();
		cdoClasses.setIntegerValue("classId", 1);
		cdoClasses.setStringValue("className", "一年级一班");
		cdoClasses.setCDOArrayValue("students", new CDO[]{cdoStudent1,cdoStudent2});

它们的XML表示分别为:
<?xml version="1.0" encoding="UTF-8"?>
<CDO>
	<NF N="id" V="1"/>
	<STRF N="username" V="userone"/>
	<STRF N="address" V="上海市。。。。。。"/>
	<BF N="sex" V="true"/>
	<DBLF N="height" V="1.74"/>
</CDO>

<?xml version="1.0" encoding="UTF-8"?>
<CDO>
	<NF N="id" V="2"/>
	<STRF N="username" V="usertwo"/>
	<STRF N="address" V="深圳市"/>
	<BF N="sex" V="false"/>
	<DBLF N="height" V="1.67"/>
</CDO>

<?xml version="1.0" encoding="UTF-8"?>
<CDO>
	<NF N="classId" V="1"/>
	<STRF N="className" V="一年级一班"/>
	<CDOAF N="students">
		<CDO>
			<NF N="id" V="1"/>
			<STRF N="username" V="userone"/>
			<STRF N="address" V="上海市。。。。。。"/>
			<BF N="sex" V="true"/>
			<DBLF N="height" V="1.74"/>
		</CDO>
		<CDO>
			<NF N="id" V="2"/>
			<STRF N="username" V="usertwo"/>
			<STRF N="address" V="深圳市"/>
			<BF N="sex" V="false"/>
			<DBLF N="height" V="1.67"/>
		</CDO>
	</CDOAF>
</CDO>

非常的清晰明了!

CDO不但能方便地转化为XML表示,而且能很方便的把XML文本串转化为CDO对象,假设有如下的XML文本串:

<?xml version="1.0" encoding="UTF-8"?>
<CDO>
	<CDOF N="cdoReturn">
		<CDO>
			<NF N="nCode" V="0"/>
			<STRF N="strText" V="OK"/>
			<STRF N="strInfo" V="OK"/>
		</CDO>
	</CDOF>
	<CDOF N="cdoResponse">
		<CDO>
			<CDOAF N="cdosMember">
				<CDO>
					<NF N="uid" V="5"/>
					<STRF N="username" V="wanghui"/>
					<STRF N="password" V="e4994c81a0c0db8e1ab137f9fdf7d5ae"/>
					<STRF N="secques" V=""/>
				</CDO>
				<CDO>
					<NF N="uid" V="353"/>
					<STRF N="username" V="bainiuwang"/>
					<STRF N="password" V="35b36dd98779303e3cdc84ff3951be9e"/>
					<STRF N="secques" V=""/>
				</CDO>
				<CDO>
					<NF N="uid" V="636"/>
					<STRF N="username" V="wangli3849"/>
					<STRF N="password" V="e3b2b5b80d57c3254c0ed1a83059d7ce"/>
					<STRF N="secques" V=""/>
				</CDO>
				<CDO>
					<NF N="uid" V="1006"/>
					<STRF N="username" V="wangle520208"/>
					<STRF N="password" V="059e5d9a6347d61859d7e2b740ef721b"/>
					<STRF N="secques" V=""/>
				</CDO>
				<CDO>
					<NF N="uid" V="1087"/>
					<STRF N="username" V="wangyuepeng"/>
					<STRF N="password" V="a5c74d745f45875d62fb418ed7e65ebb"/>
					<STRF N="secques" V=""/>
				</CDO>
			</CDOAF>
			<NF N="count" V="5"/>
		</CDO>
	</CDOF>
</CDO>

通过如下的代码便可转化为CDO对象:
StringBuilder builder = new StringBuilder();
		builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		builder.append("<CDO>");
		builder.append("	<CDOF N=\"cdoReturn\">");
		builder.append("		<CDO>");
		builder.append("			<NF N=\"nCode\" V=\"0\"/>");
		builder.append("			<STRF N=\"strText\" V=\"OK\"/>");
		builder.append("			<STRF N=\"strInfo\" V=\"OK\"/>");
		builder.append("		</CDO>");
		builder.append("	</CDOF>");
		builder.append("	<CDOF N=\"cdoResponse\">");
		builder.append("		<CDO>");
		builder.append("			<CDOAF N=\"cdosMember\">");
		builder.append("				<CDO>");
		builder.append("					<NF N=\"uid\" V=\"5\"/>");
		builder.append("					<STRF N=\"username\" V=\"wanghui\"/>");
		builder.append("					<STRF N=\"password\" V=\"e4994c81a0c0db8e1ab137f9fdf7d5ae\"/>");
		builder.append("					<STRF N=\"secques\" V=\"\"/>");
		builder.append("				</CDO>");
		builder.append("				<CDO>");
		builder.append("					<NF N=\"uid\" V=\"353\"/>");
		builder.append("					<STRF N=\"username\" V=\"bainiuwang\"/>");
		builder.append("					<STRF N=\"password\" V=\"35b36dd98779303e3cdc84ff3951be9e\"/>");
		builder.append("					<STRF N=\"secques\" V=\"\"/>");
		builder.append("				</CDO>");
		builder.append("				<CDO>");
		builder.append("					<NF N=\"uid\" V=\"636\"/>");
		builder.append("					<STRF N=\"username\" V=\"wangli3849\"/>");
		builder.append("					<STRF N=\"password\" V=\"e3b2b5b80d57c3254c0ed1a83059d7ce\"/>");
		builder.append("					<STRF N=\"secques\" V=\"\"/>");
		builder.append("				</CDO>");
		builder.append("				<CDO>");
		builder.append("					<NF N=\"uid\" V=\"1006\"/>");
		builder.append("					<STRF N=\"username\" V=\"wangle520208\"/>");
		builder.append("					<STRF N=\"password\" V=\"059e5d9a6347d61859d7e2b740ef721b\"/>");
		builder.append("					<STRF N=\"secques\" V=\"\"/>");
		builder.append("				</CDO>");
		builder.append("				<CDO>");
		builder.append("					<NF N=\"uid\" V=\"1087\"/>");
		builder.append("					<STRF N=\"username\" V=\"wangyuepeng\"/>");
		builder.append("					<STRF N=\"password\" V=\"a5c74d745f45875d62fb418ed7e65ebb\"/>");
		builder.append("					<STRF N=\"secques\" V=\"\"/>");
		builder.append("				</CDO>");
		builder.append("			</CDOAF>");
		builder.append("			<NF N=\"count\" V=\"5\"/>");
		builder.append("		</CDO>");
		builder.append("	</CDOF>");
		builder.append("</CDO>");
	    
		CDO cdoResponse = CDO.fromXML(builder.toString());
		System.out.println(cdoResponse.toJSON());

这样便可打印出如下的Json字符串:
{
    "cdoReturn":{
        "nCode":0,
        "strText":"OK",
        "strInfo":"OK"
    },
    "cdoResponse":{
        "cdosMember":[
            {
                "uid":5,
                "username":"wanghui",
                "password":"e4994c81a0c0db8e1ab137f9fdf7d5ae",
                "secques":""
            },
            {
                "uid":353,
                "username":"bainiuwang",
                "password":"35b36dd98779303e3cdc84ff3951be9e",
                "secques":""
            },
            {
                "uid":636,
                "username":"wangli3849",
                "password":"e3b2b5b80d57c3254c0ed1a83059d7ce",
                "secques":""
            },
            {
                "uid":1006,
                "username":"wangle520208",
                "password":"059e5d9a6347d61859d7e2b740ef721b",
                "secques":""
            },
            {
                "uid":1087,
                "username":"wangyuepeng",
                "password":"a5c74d745f45875d62fb418ed7e65ebb",
                "secques":""
            }
        ],
        "count":5
    }
}

确实是非常的方便!

CDO数据结构是整个CDO框架的核心,理解并掌握了这个数据接口,可以说掌握了CDO框架的百分之六十!

你可能感兴趣的:(Java新型,封装的数据结构)