Presentation & Business Tier Design Patterns Pearce
26 Slides148.00 KB
Presentation & Business Tier Design Patterns Pearce
Definitions Pattern reusable design Pattern Catalog catalog of design patterns – Horizontal – Vertical Architectural pattern Macro pattern a composite pattern
Four Tier Architecture web browser Client Tier HTTP Web Server Presentation Tier RMI JDBC App Server Business Tier dbase server JDBC Data Tier
Issues Migrating Business Logic from Presentation to Business Tier Providing B2B Business Tier
The Model-View-Controller Macro Pattern Model View Presentation Tier business Tier Controller
MVC Variations Service to Worker View-Dispatcher
View-Dispatcher : Client : Controller command dispatch : Model : View fetch/update content
Service to Worker : Client : Controller command : View update/fetch content dispatch content : Model
Business Tier (Model) Patterns Entities Beans Value Objects Data Access Objects DAO Factories etc.
Entities An entity is an object that represents a persistent business entity such as an account or a customer. Entities must persist between the sessions or transactions that use them. Entities are stored in files or databases Entities are beans – Simple or EJB.
Java Beans A Java bean is an object that conforms to the Java Bean Component Model. – – – – getters & setters for attributes notifies listeners of attribute changes serializable etc.
Example: A Person Entity public class Person extends Entity { private String first; private String last; private Address address; private Phone phone; public Phone getPhone() { return phone; } public void setPhone(Phone p) { phone p; } // etc. }
Value Objects A value object holds the attributes of one or more entities in public fields. Pass value objects, not entities, between layers. This reduces network traffic. Value objects can update and create entities. Entities can create value objects.
public class PersonVO extends ValueObject { public String first; public String last; public int addressOid; public String street; public String apartment; public String city; public String state ; public String zip; public int phoneOid; public String phone; public void update(Person per) { . } public Person makePerson() { . } } Example: Person VO
Address Book Entity serializable Bean Entities Address street apartment city 1 state zip ValueObject oid : int Person last * first * PersonVO last first street apartment city state zip addressOid phone phoneOid update() makePerson() 1 Phone number
Data Access Objects (DAOs) A DAO represents a data source. DAOs are created by DAO factories. DAOs hide the complexities of connecting with the underlying data source.
Example: Person DAO public interface PersonDAO { public int insert(PersonVO pvo) throws DAOException; public boolean delete(String name) throws DAOException; public PersonVO find(String name) throws DAOException; public boolean update(PersonVO pvo) throws DAOException; }
DAOFactory mak ePersonDAO() makeDAOFactory() MapDAOFactory makePersonDAO() DBaseDAOFactory makePersonDAO() creates creates MapPersonDAO DBasePersonDAO DBaseDAO DAOs & Factories Interface PersonDAO insert() update() delete() find()
Presentation Tier Patterns (Controller and View) View View Helper Front Controller
Front Controller Single access point for all forms. May verify & validate request May analyzes request May assemble business data needed by view (Service-to-Worker) Selects and dispatches to view
View Helper Acts as intermediary between view and business tier Performs view-specific tasks
View Generates HTML response View may assemble the data it needs (Dispatcher-View) Needed data may be assembled by controller (Service-to-Worker) Provides utilities for generating HTML
Dispatcher-View Pattern Servlet View request response out Helper PersonView PersonHelper 1 dispatchesTo Servlet FrontController processRequest() dispatch() 1 1 1 1 Interface PersonDAO creates 1 DAOFactory
: FrontController : PersonView : PersonHelper : DAOFactory : PersonDAO container dispatch creates makeDAO() creates makeBody() execute(cmmd) cmmd result dispatch makeBody() execute(cmmd) cmmd result
ServletContainer 1 Interface ServletRequest getParameters() Interface HttpServletRequest Interface ServletResponse Interface Servlet init() * service() destroy() GenericServlet 1 Interface ServletConfig 1 Interface ServletContext getAttribute() setAttribute() getRequestDispatcher() type HttpServlet doGet() doPost() doPut() doDelete() View getHelper() * Helper PersonHelper Interface HttpServletResponse PersonView PersonDAO * Interface RequestDispatcher forward() include()
Every Servlet has 3 AMaps HttpServlet Interface ServletRequest getParameters() getAttribute() setAttribute() HttpSession getAttribute() setAttribute() AMap Interface Map Interface ServletContext getAttribute() setAttribute() getRequestDispatcher()