当前位置: 技术问答>java相关
设计模式大讨论(跟贴有分)
来源: 互联网 发布时间:2015-08-01
本文导语: 1 Value Objects Purpose to provide a coarse-grained view of remote, fine-grained data. as alocal-access alternative to entity beans. Like an entity bean, a value objectrepresents a business object, but it doesn't need to provide business methods ont...
1 Value Objects
Purpose
to provide a coarse-grained view of remote, fine-grained data.
as alocal-access alternative to entity beans. Like an entity bean, a value objectrepresents a business object, but it doesn't need to provide business methods ontop of its data; it only provides methods to read its data, which makes it an ideal candidate forlocal access rather than remote access.
Benefits
Improved network traffic and response time. Fewer remote calls are made and lessdata is passed back and forth.
Reduced load on enterprise beans. A client makes only one remote call to theenterprise bean to fetch the value object for the attributes it wants; from thenon, whenever the client needs to use those attributes, it can just get themlocally from the value object.
Potential Liability
Stale data. Whenever a value object is backed by a volatile set of values (suchas a stock's price and trading volume, for example) it should only be treated asa snapshot of those values.
2 Data Access Objects
Purpose
Business components that rely on specific features of an underlying dataresource, such as a particular vendor database, tie together business logic anddata access logic. This leads to a couple of problems:
Applications that use these components are difficult to modify whenthey need to use a different type of resource.
Thecomponents themselves serve a limited segment of the market, since they arelocked into using a particular type of resource.
These problems can be avoided by removing data access logic from enterprisebeans and abstracting data access functionality into a separate interface.Enterprise beans carry out their business logic in terms of operations on thatinterface, which is implemented by a data access object (DAO) appropriate for the type of resourcebeing used.
Benefits
Separates business logic from data access logic. Hence, changes to the type ofdata source used should have no impact on the data access carried out bybusiness objects and other clients.
Greaterdeployment flexibility. Data access objects represent data sources through aninterface. The implementation of that interface can be configured at deploymenttime.
Resourcevendor independence. Components that use vendor-specific API to access data resources lockthemselves into using that particular vendor. The DAO pattern isolatesvendor-specific code where it can be easily replaced.
Resourceimplementation independence. For example, persistent data store can be implemented by a relational database, an objectdatabase, flat files in a filesystem, etc.. The DAO pattern allows designers tofocus on the behavior of application objects, and deal with data accessmechanisms separately.
Improvedextensibility. New resource types are easy to add, requiring only a new DAOimplementation for the resource type, plus integration of that class into theexisting framework.
Potential Liability
Increased complexity. This pattern adds a level of indirection to data access, increasing the number of objectsin the system and making the code slightly less clear.
3 Model-View-Controller
Purpose
to enable enterprise data to be presented via different views: e.g. HTML, WML, JFC/Swing, XML.
to enableenterprise data to be updated through different interactions: e.g. linkselections on an HTML page or WML card, button clicks on a JFC/Swing GUI, SOAPmessages written in XML.
to ensurethat multiple types of views and interactions do not impact the components thatprovide the core functionality of the enterprise application.
Benefits
Multiple views using the same model: The separation of model and view allowsmultiple views to use the same enterprise model. Consequently, an enterpriseapplication's model components are easier to implement, test, and maintain,since all access to the model goes through these components.
Easiersupport for new types of clients: To support a new type of client, you simplywrite a view and controller for it and wire them into the existing enterprisemodel.
4 Front Component: Provides centralized dispatching of requests, e.g. via a controller servlet; enablescentralized handling of security, navigation, and presentation formatting
5 Facade: Provides a layer between clientsand subsystems of a complex system; shields clients from subsystem components,making the subsystems easier to use.
6 Factory: Handles requests for object creation
7 Singleton: A class which can have at most oneinstance
8 Template Method: The key idea is that there isa basic algorithm we want to staythe same, but we want to let subclasses vary how they do the steps. The TemplateMethod says to define the algorithm in the parent class, but implementations ofthe steps in the subclasses.
9 Bimodal Data Access: Under certain conditions,allows designer to trade off data consistency for access efficiency. JDBC providesread-only, potentially dirty reads of lists of objects, bypassing thefunctionality, and the overhead, of Entity enterprise beans. At the same time,Entity enterprise beans can stillbe used for transactional access to enterprise data. Which mechanism to selectdepends on the requirements of the application operation.
10 Session Entity Facade: Being an instance ofthe Facade pattern, this pattern shares its applicability. In the context of J2EE application development, use the Session Facade pattern when youwant to provide a simple interface to a complex subsystem of enterprise beans orwhen you want to reduce communication and dependencies between client objectsand enterprise beans.
Purpose
to provide a coarse-grained view of remote, fine-grained data.
as alocal-access alternative to entity beans. Like an entity bean, a value objectrepresents a business object, but it doesn't need to provide business methods ontop of its data; it only provides methods to read its data, which makes it an ideal candidate forlocal access rather than remote access.
Benefits
Improved network traffic and response time. Fewer remote calls are made and lessdata is passed back and forth.
Reduced load on enterprise beans. A client makes only one remote call to theenterprise bean to fetch the value object for the attributes it wants; from thenon, whenever the client needs to use those attributes, it can just get themlocally from the value object.
Potential Liability
Stale data. Whenever a value object is backed by a volatile set of values (suchas a stock's price and trading volume, for example) it should only be treated asa snapshot of those values.
2 Data Access Objects
Purpose
Business components that rely on specific features of an underlying dataresource, such as a particular vendor database, tie together business logic anddata access logic. This leads to a couple of problems:
Applications that use these components are difficult to modify whenthey need to use a different type of resource.
Thecomponents themselves serve a limited segment of the market, since they arelocked into using a particular type of resource.
These problems can be avoided by removing data access logic from enterprisebeans and abstracting data access functionality into a separate interface.Enterprise beans carry out their business logic in terms of operations on thatinterface, which is implemented by a data access object (DAO) appropriate for the type of resourcebeing used.
Benefits
Separates business logic from data access logic. Hence, changes to the type ofdata source used should have no impact on the data access carried out bybusiness objects and other clients.
Greaterdeployment flexibility. Data access objects represent data sources through aninterface. The implementation of that interface can be configured at deploymenttime.
Resourcevendor independence. Components that use vendor-specific API to access data resources lockthemselves into using that particular vendor. The DAO pattern isolatesvendor-specific code where it can be easily replaced.
Resourceimplementation independence. For example, persistent data store can be implemented by a relational database, an objectdatabase, flat files in a filesystem, etc.. The DAO pattern allows designers tofocus on the behavior of application objects, and deal with data accessmechanisms separately.
Improvedextensibility. New resource types are easy to add, requiring only a new DAOimplementation for the resource type, plus integration of that class into theexisting framework.
Potential Liability
Increased complexity. This pattern adds a level of indirection to data access, increasing the number of objectsin the system and making the code slightly less clear.
3 Model-View-Controller
Purpose
to enable enterprise data to be presented via different views: e.g. HTML, WML, JFC/Swing, XML.
to enableenterprise data to be updated through different interactions: e.g. linkselections on an HTML page or WML card, button clicks on a JFC/Swing GUI, SOAPmessages written in XML.
to ensurethat multiple types of views and interactions do not impact the components thatprovide the core functionality of the enterprise application.
Benefits
Multiple views using the same model: The separation of model and view allowsmultiple views to use the same enterprise model. Consequently, an enterpriseapplication's model components are easier to implement, test, and maintain,since all access to the model goes through these components.
Easiersupport for new types of clients: To support a new type of client, you simplywrite a view and controller for it and wire them into the existing enterprisemodel.
4 Front Component: Provides centralized dispatching of requests, e.g. via a controller servlet; enablescentralized handling of security, navigation, and presentation formatting
5 Facade: Provides a layer between clientsand subsystems of a complex system; shields clients from subsystem components,making the subsystems easier to use.
6 Factory: Handles requests for object creation
7 Singleton: A class which can have at most oneinstance
8 Template Method: The key idea is that there isa basic algorithm we want to staythe same, but we want to let subclasses vary how they do the steps. The TemplateMethod says to define the algorithm in the parent class, but implementations ofthe steps in the subclasses.
9 Bimodal Data Access: Under certain conditions,allows designer to trade off data consistency for access efficiency. JDBC providesread-only, potentially dirty reads of lists of objects, bypassing thefunctionality, and the overhead, of Entity enterprise beans. At the same time,Entity enterprise beans can stillbe used for transactional access to enterprise data. Which mechanism to selectdepends on the requirements of the application operation.
10 Session Entity Facade: Being an instance ofthe Facade pattern, this pattern shares its applicability. In the context of J2EE application development, use the Session Facade pattern when youwant to provide a simple interface to a complex subsystem of enterprise beans orwhen you want to reduce communication and dependencies between client objectsand enterprise beans.
|
Factory Method
意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
适用性 当一个类不知道它所必须创建的对象的类的时候。
当一个类希望由它的子类来指定它所创建的对象的时候。
当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。
名称 Abstract Factory
意图 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性 一个系统要独立于它的产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
当你要强调一系列相关的产品对象的设计以便进行联合使用时。
当你提供一个产品类库,而只想显示它们的接口而不是实现时。
Adapter
意图 将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适用性 你想使用一个已经存在的类,而它的接口不符合你的需求。
你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
(仅适用于对象A d a p t e r )你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
Observer
意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用性 当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
适用性 当一个类不知道它所必须创建的对象的类的时候。
当一个类希望由它的子类来指定它所创建的对象的时候。
当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。
名称 Abstract Factory
意图 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性 一个系统要独立于它的产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
当你要强调一系列相关的产品对象的设计以便进行联合使用时。
当你提供一个产品类库,而只想显示它们的接口而不是实现时。
Adapter
意图 将一个类的接口转换成客户希望的另外一个接口。A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
适用性 你想使用一个已经存在的类,而它的接口不符合你的需求。
你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。
(仅适用于对象A d a p t e r )你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。
Observer
意图 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用性 当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
|
《The Design Patterns Java Companion》by James W. Cooper,
good book for java users to learn Design Patterns!
good book for java users to learn Design Patterns!
|
领分!呵呵
|
and
11.data transfer object
12.message bean facade(for asynchronized response)
13.JDO,a substitution of Entity Bean
14.rowset
11.data transfer object
12.message bean facade(for asynchronized response)
13.JDO,a substitution of Entity Bean
14.rowset
|
我接!!
|
up
呵呵
呵呵