pest中EB的设计模式 你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是 因为,在程序运行时刻实现部分应可以被选择或者切换。 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时 Bridge模 式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充 对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。 2147 Adapter模式 它有两种方式实现。比较简单,我就不多说了。 22. Value object模式 值对象模式,在SUN的站点上有详细的描述,并且《J2EE核心模式》上也有详细描述 其实这种模式,我们每天都在使用,目的就是减少网络流量,提高效率,许多书上叫做的粗 粒度。和这种模式相关的还有复合实体,值对象组装器,值列表处理器。 我感觉在项目开发中有一个人负责系统的全部 entity Bean,为每个 entity Bean建立值对 象,并且建立他们之间的关系。其他的开发人员只管调用就行了,如果数据库修改,它来修 改。这种方式,我建议大家使用 221.意图 Efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data. 我们知道EJB的调用使用了远程方法,它的效率一般要远低于本地方法的 调用,这样我们要读取一个对象,如果使用他的 getXX方法就多次调用了远 程,效率很低,如果一次性读到值对象,这就减少了远程调用, 我们在实际开发中,一般为每个 EntityBean建一个值对象,并且在 entityBean中的Home接口中,增加一个 create方法,参数就是值对象.并 且在接口中增加 setData和 getData方法。 ■Home接口 public interface TAccountAcptRemoteHome extends javax. ejb eJBHome /在创建时,使用值对象,如果修改数据结构,接口不需改变,只修改 属性就行了 public TAccountAcptRemote create (TAccountAcptModel data) throws CreateException, RemoteException
PetStore 中 EJB 的设计模式 11/11 • 你不希望在抽象和它的实现部分之间有一个固定的绑定关系。例如这种情况可能是 因为,在程序运行时刻实现部分应可以被选择或者切换。 • 类的抽象以及它的实现都应该可以通过生成子类的方法加以扩充。这时B r i d g e 模 式使你可以对不同的抽象接口和实现部分进行组合,并分别对它们进行扩充。 • 对一个抽象的实现部分的修改应对客户不产生影响,即客户的代码不必重新编译。 2.1.4.7 Adapter 模式 它有两种方式实现。比较简单,我就不多说了。 2.2. Value Object 模式 值对象模式,在 SUN 的站点上有详细的描述,并且《J2EE 核心模式》上也有详细描述, 其实这种模式,我们每天都在使用,目的就是减少网络流量,提高效率,许多书上叫做的粗 粒度。和这种模式相关的还有复合实体,值对象组装器,值列表处理器。 我感觉在项目开发中有一个人负责系统的全部 entityBean,为每个 entityBean 建立值对 象,并且建立他们之间的关系。其他的开发人员只管调用就行了,如果数据库修改,它来修 改。这种方式,我建议大家使用。 2.2.1. 意图 Efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data. 我们知道 EJB 的调用使用了远程方法,它的效率一般要远低于本地方法的 调用,这样我们要读取一个对象,如果使用他的 getXX 方法就多次调用了远 程,效率很低,如果一次性读到值对象,这就减少了远程调用。 我们在实际开发中,一般为每个 EntityBean 建一个值对象,并且在 entityBean 中的 Home 接口中,增加一个 create 方法,参数就是值对象. 并 且在接口中增加 setData 和 getData 方法。 Home 接口 public interface TAccountAcptRemoteHome extends javax.ejb.EJBHome { //在创建时,使用值对象,如果修改数据结构,接口不需改变,只修改 属性就行了 public TAccountAcptRemote create(TAccountAcptModel data) throws CreateException, RemoteException;
PetStore中EJB的设计模式 声明接口(远程或本地接口) public interface TAccountAcptRemote extends javax. e jb. eJBob ject /通过这个接口,一次性获得值对象,不需分别使用 getXX,减少网络 调用次数,提高效率 public TAccountAcptModel getDatao throws RemoteException public void getData(TaccountAcptModel data) throws RemoteException 值对象 public class TAccountAcptModel implements java. io Serializable public java. lang. String acceptId public java. lang String accountId public java. lang String ownerId public java. lang String accountName public java. lang String accountAddr public java. lang String accountZip public java. lang String bankId public java. lang String account public java. lang String pay TypeId public java. lang String state public java. lang String post Mode public java. lang String postAddr public java. lang. String postzip
PetStore 中 EJB 的设计模式 12/12 } 声明接口(远程或本地接口) public interface TAccountAcptRemote extends javax.ejb.EJBObject { //通过这个接口,一次性获得值对象,不需分别使用 getXX,减少网络 调用次数,提高效率 public TAccountAcptModel getData() throws RemoteException; public void getData(TaccountAcptModel data) throws RemoteException; } 值对象 public class TAccountAcptModel implements java.io.Serializable { public java.lang.String acceptId ; public java.lang.String accountId ; public java.lang.String ownerId ; public java.lang.String accountName; public java.lang.String accountAddr; public java.lang.String accountZip ; public java.lang.String bankId ; public java.lang.String account ; public java.lang.String payTypeId ; public java.lang.String state ; public java.lang.String postMode ; public java.lang.String postAddr ; public java.lang.String postZip ;
pest中EB的设计模式 public java. lang. String postMan public java. math. BigDecimal pre Sum ignDat 222.动机 Some entities have a set of attributes that are always used together. In the Java Pet Store application, the contact information of a customer(city, state, zip code, etc.)is an example of one such group Suppose that in total, a customers contact information is composed of 15 individual attributes, each accessible by a corresponding getXXX method in Customer EJB. Then, every time a customer s contact information is requested, 15 remote getXXX calls will be invoked on the CustomerEJB one for each attribute. This will incur heavy costs in network traffic and ejb container resource usage The Java Pet Store application reduces these costs by adopting a coarse-grained view of the fine-grained contact information. It aggregates the contact information-related attributes of the CustomerEJb into an instance of Contact Information This instance, or value object, is then serialized and sent over the network to a requesting client, where the object is deserialized. Subsequent read accesses go through the value object rather than the remote entity object. Since these acces local, they require no server communication and use fewer resources 13/13
PetStore 中 EJB 的设计模式 13/13 public java.lang.String postMan ; public java.math.BigDecimal preSum ; public java.sql.Date createDate ; public java.sql.Date unsignDate ; } 2.2.2. 动机 Some entities have a set of attributes that are always used together. In the Java Pet Store application, the contact information of a customer (city, state, zip code, etc.) is an example of one such group. Suppose that in total, a customer's contact information is composed of 15 individual attributes, each accessible by a corresponding getXXX method in CustomerEJB. Then, every time a customer's contact information is requested, 15 remote getXXX calls will be invoked on the CustomerEJB, one for each attribute. This will incur heavy costs in network traffic and EJB container resource usage. The Java Pet Store application reduces these costs by adopting a coarse-grained view of the fine-grained contact information. It aggregates the contact information-related attributes of the CustomerEJB into an instance of ContactInformation. This instance, or value object, is then serialized and sent over the network to a requesting client, where the object is deserialized. Subsequent read accesses go through the value object rather than the remote entity object. Since these accesses are local, they require no server communication and use fewer resources
PetStore中EJB的设计模式 clent Customer ob」Gt itltyBBan Entity Bean create aiRr Customernfe Customer ob」ect Valueob]ect EntItyBean 19r 223.结构 EnterpriseBean业务对象,实现了业务逻辑 Value Object实现了串行化值对象。 224. PerStore的例子 PetStore中存在大量的例子,基本上每个EJB中都可以找到它的踪影
PetStore 中 EJB 的设计模式 14/14 2.2.3. 结构 EnterpriseBean 业务对象,实现了业务逻辑 ValueObject 实现了串行化值对象。 2.2.4. PerStore 的例子 PetStore 中存在大量的例子,基本上每个 EJB 中都可以找到它的踪影
pest中EB的设计模式 如值对象 Profilelnfo(一定要实现 Serializable接口,就是串行化) public class ProfileInfo implements java. io Serializable i private String defaultPreferredLanguage=null private String default Favorite Category =null private boolean defaultMyList Preference=false private boolean default Banner Preference false public Profilelnfo( String default PreferredLanguage, String default Favorite Category, boolean default Preference. boolean default Banner Preference)i this defaultPreferredLanguage =default PreferredLanguage: this default Favorite Category defaultFavorite Category this default MyListPreference= defaultMylistPreference this default Banner Preference= default Banner Preference public String getPreferredLanguageOi return default PreferredLanguage public String get Favorite Category 4 return default Favorite Category public boolean getBanner Preference(( return default Banner Preference public boolean getMyList Preference( return defaultMyList Preference ■使用类,提高调用粗粒度,使用值对象 public abstract class CustomerEJB implements javax. ejb Entity Bean i ∥一次调用,获得值对象,提高效率 public abstract ProfileLocal getProfile(; ∥设置值对象 public abstract void setProfile( ProfileLocal profile); 15/15
PetStore 中 EJB 的设计模式 15/15 如值对象 ProfileInfo(一定要实现 Serializable 接口,就是串行化) public class ProfileInfo implements java.io.Serializable { private String defaultPreferredLanguage = null; private String defaultFavoriteCategory = null; private boolean defaultMyListPreference = false; private boolean defaultBannerPreference = false; public ProfileInfo( String defaultPreferredLanguage, String defaultFavoriteCategory, boolean defaultMyListPreference, boolean defaultBannerPreference) { this.defaultPreferredLanguage = defaultPreferredLanguage; this.defaultFavoriteCategory = defaultFavoriteCategory; this.defaultMyListPreference = defaultMyListPreference; this.defaultBannerPreference = defaultBannerPreference; } public String getPreferredLanguage() { return defaultPreferredLanguage; } public String getFavoriteCategory() { return defaultFavoriteCategory; } public boolean getBannerPreference() { return defaultBannerPreference; } public boolean getMyListPreference() { return defaultMyListPreference; } } 使用类,提高调用粗粒度,使用值对象 public abstract class CustomerEJB implements javax.ejb.EntityBean { //一次调用,获得值对象,提高效率 public abstract ProfileLocal getProfile(); //设置值对象 public abstract void setProfile(ProfileLocal profile); }