George Blank University Lecturer CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 10 More Design Patterns • I am somewhat conflicted by this chapter as an instructor. While I believe that using design patterns is a critical skill for a professional programmer in an objectoriented environment, I am not sure how much emphasis it should have in an introductory course in programming. Programming, not Design • The problem is that this course is identified as a programming course, and design is a secondary consideration. Key to Design • Craig Larman, the author of Applying UML and Patterns, has stated that the most important skill in Object-Oriented Design is assigning responsibility to objects. Understanding that simple statement helps to clarify Design Patterns. Most of them answer the question; “Which object should be assigned this responsibility?” What are patterns applied to? These patterns are applied to: 1. Programming Idioms 2. Coding Idioms 3. Data Structures 4. Algorithms 5. Protocols 6. Development of new frames What are patterns applied to? 7. Use of existing Frameworks 8. Analysis of models 9. System Architecture 10. Development Organization 11. Development Process. Creator Patterns • For example, the first group of patterns discussed in Chapter 10 are creator patterns. Primarily, a creator pattern is a tried and tested answer to the question; “If I need to create a new instance of an object, what object should have the responsibility for instantiation?” Course objective • In this course, I do not expect you to learn all the design patterns mentioned. However, I do expect you to develop a good understanding of a couple of patterns, and to have an intuitive sense of what a design pattern is, how it is described, and what its function is. Key Patterns • Some patterns that are important are the Singleton pattern and Controller patterns, especially since many object-oriented programmers use the Model-View-Controller pattern as a programming paradigm. You should also identify some of the patterns discussed in the text as important based on your own interests. Chapter 10 Patterns • • • • • Abstract Factory Prototype Builder Command Adapter Design Patterns • A Design Pattern is the one which typically shows the interactions and relationships between objects or classes without specifying the final applications or classes that are involved. Design Patterns • Patterns are usually concerned with some kind of architecture. • Each pattern is a three-part rule, which expresses a relation between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain software configuration which allows these forces to resolve themselves. Design Patterns • The design pattern identifies the participating classes and their instances, their roles and collaborations, along with the distribution of responsibilities. • Each design pattern focuses on a particular objectoriented design problem. • It describes when it applies, whether or not it can be applied in view of other design constraints. How are Design Patterns different from Frameworks? • Design Patterns are more abstract than Frameworks. • Design Patterns are smaller architectural elements than Frameworks. • Design Patterns are less specialized than Frameworks. How is a pattern different from a class? • A pattern is not an implementation. It just describes when, why, and how to go about creating an implementation. • Some solutions are responsive for description via implementations such as classes, frameworks, tools that provide developers everything they would need to know . Even so, the code itself is not the pattern. Session Façade, Business Delegate and Model-ViewController Design Patterns Praveen Mula Subba Reddy Daka Session Facade • Session Facade is a design pattern used in developing Enterprise applications. • It is implemented as a high level component (like a Session EJB) and has all the interactions between low level components (like an Entity EJB). • It provides a single interface for the functionality of the application. Session Facade Class Diagram Sequence diagram before and after adding Session Facade Session Facade • Performance – An Entity bean is equivalent to a row in the database. If the Entity beans were to be accessed directly, a network call would result for each row access. – On the other hand, a Session bean is equivalent to a stored procedure. Accessing a session bean that is colocated with an entity bean emulates accessing a row through a stored procedure. Design patterns: Session Facade • Facade session bean with EJB 1.1 remote interface Servlet Presentation logic Servlet Presentation logic Web container Session facade Entity Bean Business logic Entity Bean Session facade Business logic Entity Bean EJB container Communication layer Database Design patterns: EJB 2.0 local • Session facade with EJB 2.0 local interface Entity Beans Local interface Servlet Presentation logic Servlet Presentation logic Web container Session facade Business logic Session facade Business logic EJB container Entity Bean Entity Bean Entity Bean Database Session Facade Reusability The session bean layer is powerful because it externalizes all business logic from the entity beans. This means the entity beans contain data and data-related logic only. This promotes high re-use of entity beans. Data abstraction layer The session bean layer is a facade. The particular way the session bean persists (via JDBC directly or via entity beans) is an implementation detail of the session bean. Benefits of Session Facade • Lower Network overhead Shorter execution time • Short and local Transaction • Higher Concurrency • Lower Coupling • Better Reusability and maintainability Business Delegate • The Business Delegate hides the underlying implementation details of the business service, such as lookup and access details of the EJB architecture. • The Business Delegate pattern manages the complexity of distributed component lookup and exception handling, Business Delegate • Business Delegate may adapt the business component interface to a simpler interface for use by views. • It reduces coupling between client and system’s business services and possibly shields client from volatility in implementation of business service API. Business Service Component A business service component implements the actual business logic. Some examples of the business service components are stateless session EJB and entity EJB, a CORBA object or an RPC server Business Delegate Class Diagram Note: POJO is “Plain Old Java Object” Business Delegate Sequence Diagram Business Interface Pattern • Business Interface pattern allows us to provide a business-specific interface for interacting with session beans. Business Delegate and Business Interface patterns • The Business Interface pattern defines the business logic available for use while the Business Delegate pattern provides access to logic without introducing a dependency on the implementation technology. Model-View-Controller (MVC) The goal of a Model-View-Controller design pattern is to achieve decoupling among the software components that are responsible for encapsulating business functions, presenting the content and controlling the navigation. Model / view / controller (MVC) View Controller Model Create() Main (mediates) } (holds data) Register() Model { Model m; Controller c(&m); View v(&c); Create() Controller (displays data) Create() View calls Register() MVC (cont.) Subject Register(Observer) Unregister(Observer) NotifyAll() Controller 1 * Observer virtual OnUpdate() for all o in observers { o.OnUpdate() } View virtual OnUpdate() MVC using Struts Bibliography • Jia, Xiaoping, Object Oriented Software Development Using Java. Addison Wesley, 2003 • Larman, Craig, Applying UML and Patterns, 3rd Edition, Prentice Hall, 2002 • Cooper, James, The Design Patterns Java Companion, IBM Thomas J. Watson Research Center, 1998 Online Resource • A good introduction to patterns for Java developers is the free on-line book by James Cooper of IBM at http://www.patterndepot.com/put/8/JavaPatter ns.htm Other Web References: • http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/d efault.htm • http://en.wikipedia.org/wiki/Design_pattern_(computer_science) • http://www.cmcrossroads.com/bradapp/docs/patternsintro.html#KindsOfDesignPatterns • http://www-128.ibm.com/developerworks/library/j-ejb1022.html • http://www.corej2eepatterns.com/Patterns2ndEd/BusinessDeleg ate.htm • http://www.allapplabs.com/j2ee_design_patterns/j2ee_design_p atterns_business_delegate.htm References • http://java.sun.com/blueprints/patterns/Sessio nFacade.html • http://java.sun.com/blueprints/corej2eepattern s/Patterns/SessionFacade.html • http://www.jguru.com/ • http://www.javaworld.com/