Zeroc Ice返回值类型对象的实现(一)

2014-11-24 01:39:58 · 作者: · 浏览: 4

引言:

最近比较搓,忙得没空写写博客,回想一下又好像没忙什么事。得反省一下了,当然此是后话。

本文就Zeroc Ice方法返回复杂类的对象(return by-value, not by-reff),做以简单说明。之所以有这篇文章,只因笔者发现网上流传的中文文章中有这么个空白,英文的也没个直接的说明。

此文用BBCode编写。

内容:

一、ICE方法返回对象的实现
二、机制的简要说明
三、一个Exception的解决
四、资源信息

正文:

一、ICE方法返回对象的实现。

1,模型设计。

\

如上图“class_diagram.JPG”所示,Bond(债券)为JavaBean,MainOperator(主操作者)有一个方法“Bond getBean(String beanUID)”返回一个JavaBean。

2,具体实现。(各代码所在文件名,请参看首注释中的“file”注释)

A)slice定义

Java代码
/*
* file: BondDef.ice
* by: zhaoningbo
* date: 2011-07-25 15:51
*/
#ifndef BEAN_BOND_DEF
#define BEAN_BOND_DEF
module com{
module number{
module bean{

// 债券Bean
class Bond{

// Files
string bName;
string bCode;

// Methods
string getbName();
void setbName(string bName);

string getbCode();
void setbCode(string bCode);

};

};
};
};
#endif


Java代码
/*
* file: MainOperatorDef.ice
* by: zhaoningbo
* date: 2011-07-25 16:02
*/
#ifndef OPERATOR_MAINOPERATOR_DEF
#define OPERATOR_MAINOPERATOR_DEF
module com{
module number{

// 预定义
module bean{
class Bond;
};

module operator{

// 总执行者
interface MainOperator{
// 获取Bond对象
idempotent com::number::bean::Bond getBean(string beanUID);
};
};
};
};
#endif


B)slice2java生成ice的java接口类集

C)编写服务方

(i)实现Bond。因为Bond是个抽象类,需要给定一个实现BondI。

Java代码
/*
* file: BondI.java
*/
package com.number.bond;

import java.io.Serializable;
import Ice.Current;
import com.number.bean.Bond;

/**
* 自定义债券Bean
* 注:实现Bond时,直接实现Override即可,无需添加其他的类元素。
* @author zhnb
*
*/
public class BondI extends Bond implements Serializable {

private static final long serialVersionUID = 8758902536680272427L;

@Override
public String getbCode(Current current) {
return this.bCode;
}

@Override
public String getbName(Current current) {
return this.bName;
}

@Override
public void setbCode(String bCode, Current current) {
this.bCode = bCode;
}

@Override
public void setbName(String bName, Current current) {
this.bName = bName;
}

}


(ii)加个dao层数据提供者(仅图好看)

Java代码
/*
* file: BondLCData.java
*/
package com.number.dao;

import java.io.Serializable;
import com.number.bond.BondI;

/**
* 数据提供类
* @author zhnb
*
*/
public class BondLCData implements Serializable {

private static final long serialVersionUID = -5413237344986060553L;

// 单值
public static BondI BONDLC_DATA_SINGLE = null;
static{
BondI bondI= new BondI();
bondI.setbCode("600006");
bondI.setbName("青岛啤酒");

BONDLC_DATA_SINGLE = bondI;
}

}


(iii)实现操作者业务类

Java代码
/*
* file: MainOperatorI.java
*/
package com.number.operator;

import java.io.Serializable;
import Ice.Current;
import com.number.bean.Bond;
import com.number.dao.BondLCData;

/**
* 主操作业务类
* @author zhnb
*
*/
publ