440 likes | 657 Views
@Diego Thanh Nguyen - http:// free.smartbiz.vn. J2ee design pattern. Table Of Content. I. GoF Design Pattern I.1. Creation Patterns I.2. Structural Patterns I.3. Behavioral Patterns II. J2EE Presentation : III. J2EE Business: IV. J2EE Integration. 1. Problem. 2. Context. Forces.
E N D
@Diego Thanh Nguyen - http://free.smartbiz.vn J2ee design pattern www.smartbiz.vn
Table Of Content • I. GoF Design Pattern • I.1. Creation Patterns • I.2. Structural Patterns • I.3. Behavioral Patterns • II. J2EE Presentation: • III. J2EE Business: • IV. J2EE Integration www.smartbiz.vn
1.Problem 2. Context Forces 3. Solution Benefits Consequences Related Patterns I. How Design Patterns Arise ? IF you find yourself in CONTEXT for example EXAMPLES, with PROBLEM, entailing FORCES THEN for some REASONS, apply DESIGN FORM AND/OR RULE to construct SOLUTION leading to NEW CONTEXT & OTHER PATTERNS www.smartbiz.vn
Defer object creation to another object Describe ways to assemble objects Describe algorithms and flow control I. Design-Pattern Catalog www.smartbiz.vn
I.1. Creation Patterns • Creational Patterns prescribe the way that objects are created. These patterns are used when a decision must be made at the time a class is instantiated. • Singleton: ensure a class has one Instance, and provide a global point of access to it. • Abstract Factory: provide an interface for creating families of related or dependent objects without specifying their concrete classes. www.smartbiz.vn
I.1.1 Abstract Factory • Intent: create families of related objects without specifying subclass names • Applicability: when clients cannot anticipate groups of classes to instantiate • Concrete factories create groups of strategies www.smartbiz.vn
I.2. Structural Patterns • Structural Patterns prescribe the organization of classes and objects. • Adapter • Convert the interface of a class into another interface that clients expect • Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces • Decorator • Extend the functionality of the original class in a way that is transparent to the client class www.smartbiz.vn
I.2.1. Composite • Intent • treat individual objects & multiple, recursively-composed objects uniformly • Applicability • Objects must be composed recursively, • and no distinction between individual & composed elements, • and objects in structurecan be treated uniformly www.smartbiz.vn
I.2.1. Composite • CORBA Naming Service example using CosNaming::BindingIterator (which is an example of the “Batch Iterator” pattern compound from POSA5) Composite Node Leaf Node www.smartbiz.vn
I.3. Behavioral Patterns • Behavioral Patterns prescribe the way objects interact with each other. They help make complex behavior manageable by specifying the responsibilities of objects and the ways they communicate with each other. • State: allow an object to alter its behavior when its internal state changes www.smartbiz.vn
I.3.1. Strategy • Intent • define a family of algorithms, encapsulate each one, & make them • interchangeable to let clients & algorithms vary independently • Applicability • When object is configurable with one of many algorithms, • and all algorithms can be encapsulated, • and one interface covers all encapsulations www.smartbiz.vn
I.3.1. Strategy • Strategy applied in distributed (middleware) Hook for marshaling strategy Hook for the request demuxing strategy Hook for the event demuxing strategy Hook for the connection management strategy Hook for the concurrency strategy Hook for the underlying transport strategy www.smartbiz.vn
I.3.1. Oracle Fusion Component OracleFusion Groupware API: A unified interface for all connectors that access the groupware servers via WebDAV, applying Singleton, Façade and Factory design patterns All connectors utilize the Jakarta Jackrabbit (client-side) API to send requests to and receive responses from a variety of WebDAV-enabled groupware servers Jakarta Jackrabbit re-uses some of open source libraries such as the Jakarta Commons library (HTTP Client &Logging), JDOM (JSR 102), JTA, JMX, …
I.3.2. OF Design Pattern • Singleton:the Connector class must only have one instance because it’s wasteful& uselessto keepseveral instances on memory. Keepingsomanyconnectorswilldifficult to manage lifecycle & impact the performance of application. • FactoryMethod:because of weassume our API canbeusedagainst multiple servers sothatthere must be a Connector for each server (i.e. ECExchangeConnector, ECLotusDominoConnector, ECOpenGroupwareConnector). Eachconnectorisresponsible for manufacturingitsown managers (MailManager, CalendarManager, TasksManager) and delegate the jobs to them. The connectorwillonlybedetermined by the client depending on the configuration. • For example, if a client (portlet) need to send a mail by Exchange server thenitwill call the ECExchangeConnector; thisconnectorthenwill use itslocalized manager ExchangeMailManager to make life easier. • Abstract Factory:ECGroupwareFactoryis the abstract factory class for all connectorsbecauseitwillexpose a unified interface for connectorsto client. • Facade: the unified interface willbedefined in the Façade pattern. As statedbefore, a specificConnectorwilldelegate the appropriate jobs to itssubsystems (managers). The façade pattern alsodecouples the managers from the client and other managers, therebypromotingindependence and portability. www.smartbiz.vn
I.3.1. OF Design Pattern www.smartbiz.vn
Action Action Action Action Action Action Action Action I.4.1. Observer Pattern • Easier to keep a consistent and maintainable view of the data • Half as many connections between actions, views • Views are independent and unaware of each other Data View View & Action View Messages about the data Data notifies observers via events when the state of the data changes www.smartbiz.vn
I.4.2. Implementing an Observer • Assign the subject to theobservers (MainForm_Load) IssueSubject www.smartbiz.vn
Assign the subject to theobservers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subjectpaneB.Subject = m_subject ... b c d a I.4.3. Implementing an Observer IssueSubject www.smartbiz.vn
Assign the subject to theobservers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subjectpaneB.Subject = m_subject ... b c d a I.4.4. Implementing an Observer • Observers bind and savechanges to Subject.DataSet IssueSubject www.smartbiz.vn
Assign the subject to theobservers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subjectpaneB.Subject = m_subject ... b c d a IssueSubject I.4.5. Implementing an Observer • Observers bind and savechanges to Subject.DataSet • When data changes, subjectraises DataChanged event www.smartbiz.vn
Assign the subject to theobservers (MainForm_Load) m_subject = new IssueSubject() paneA.Subject = m_subjectpaneB.Subject = m_subject ... b c d a • Observers handle subjectevents to rebind data, if they care m_subject.DataChanged += … ‘ rebind I.4.6. Implementing an Observer • Observers bind and savechanges to Subject.DataSet • When data changes, subjectraises DataChanged event IssueSubject www.smartbiz.vn
I.5.1. Coordinate Command State • Related menu and toolbar widgets are not automatically handled together • Command Pattern • Command objects unify the state and action for related UI widgets • Example: • “Save” action • Menu item • Toolbar button • Context menu www.smartbiz.vn
I.5.2. Implementing a Command • Create a Command for each action in MainForm_Load() action = new Command.Action(this.WorkOffline_Action) offlineCommand = new Command(action) • Wire menu items and toolbar buttons to the Command using Commander objects MenuItemCommander.Connect(menuWorkOffline, offlineCommand) ToolBarButtonCommander.Connect(tlbOffline, offlineCommand) • Control button and menu item state through the Command ' Disable all UI widgets connected to this command offlineCommand.IsEnabled = false www.smartbiz.vn
I.6.1. Accelerated J2EE Development • Architect Service-Oriented Components • Design for Flexible, Agile Applications and Iterative Development • Apply/Extend Pre-Built COTS Components • Infrastructure and Common Business Components • Generate/Extend Custom Components • Rapidly Specify and Provision New Components • Leverage Industry Development Standards • Frameworks (STRUTS, Spring / Weld, Hibernate) • Patterns (MVC) • XML, Best Practices www.smartbiz.vn
RMI RMI JMS JMS II. J2EE Application Design Patterns Presentation Business Logic &Messaging Fabric Data Integration& Persistence EJB Container EJB Container Response 8. ExecuteBiz Method Resources Business Service Facade 13. CRUDData Request 14. Query Database 10b.Get/Set Remove Data Access Object 9.Assemble Data <<de-serialize>> Web Server &Servlet Engine 18. Response Directory JMS Serialized Value Object 16. Forward JSF, JSP (Taglibs) Value Object Assembler 12.Get/Set Remove <<creates>> 1. Request 17. State? <<uses>> Action Servlet Data Access Proxy <<uses>> 10a.Invoke 4. Dispatch 2. Fill State 3. Validate Data Connection Factory Connection Pool Entity Bean 11. CRUDData XMLAction to Form & Forward Mappings (Stateless) Session Bean Value Object <<uses>> Action Form 5. Get Delegate Action Object Key Other Infrastructure 15. Fill State <<uses>> 6. Execute <<supplies>> EJB Container BusinessDelegateFactory BusinessDelegateAdapter <<uses>> Value Object ModelPattern M 7a. Execute Biz Method Event Interceptor Framework Security Management Framework Messaging Command Factory Event Notification Framework ViewPattern V Business Service Proxy <<uses>> ControllerPattern <<uses>> C Data Access Proxy Filter Interceptors Service Activation Message Q& Channel Locator 7b. CRUD Data OtherPattern P To DAO www.smartbiz.vn
II. Model-View-Controller – Passive :Model :View :Controller handleEvent service update getData www.smartbiz.vn
<<interface>> Observer +update() II. Model-View-Controller – Active Controller Model View :Model :View handleEvent notify update getData data www.smartbiz.vn
II.1. Presentation Tier Patterns • Intercepting Filter • Front Controller: Use of a Controller to handle all web requests • Dispatcher View: The controller works with a Dispatcher that handles navigation • Composite View • View Helper • Service to Worker www.smartbiz.vn
II.2. Intercepting Filter • Problems: • Verify authentication • Check type of client • Is the session valid? • Solution: implement one or more Intercepting filters. • Sample: Filter-servlet - can transparently pre and post process all requests in an unlimited length filter chain. www.smartbiz.vn
II.3. Front Controller & Dispatcher • Front Controller - Problems: • Multiple views. • Navigation logic embedded in views. • Difficult to maintain view logic as application grows. • Want to reuse view navigation within multiple clients. www.smartbiz.vn
II.3. Front Controller - Dispatcher www.smartbiz.vn
III.4. View Helper • Problem: • Many common operations in each view. • Often view output is dependant on business data. • Create more cohesive look and feel. • Like to hand off UI to web design group • Solution: • Basically anything that helps you display your model data in a reusable fashion. • Implemented as JavaBeans, Tags or XML/XSLT • See the JSTL, JSF and Struts. www.smartbiz.vn
II.5. Composite View • Problem: • Many common & recurring components in a view. • Fragments may display differently given user type (i.e. faculty, student). • Look and feel in flux needs to be easily updateable. • Differs from View Helper as the Composite View decides which sub-views to include while View Helper generally decides how to display a given sub-view. www.smartbiz.vn
III. Business Tier Patterns • Business Delegate: use of a Business Delegate to hide Implementation Detail. • Session Façade • Service Locator • Value Object • Composite Entity • Value Object Assembler • Value List Handler www.smartbiz.vn
III.1. Business Delegate www.smartbiz.vn
III.2. Session Façade Pattern • Problem: • Need very loose coupling between client and ever changing business tier. • Data tier changing; need to improve reuse. • Session Bean provides unified approach • Façade hides Entity Bean Details www.smartbiz.vn
IV. Integration • Data Access Object • Service Activator • User Workflow Integrator www.smartbiz.vn
IV.1. Data Access Object www.smartbiz.vn