Architectural Patterns
Total Page:16
File Type:pdf, Size:1020Kb
Architectural patterns Open Source & DOTNET platform Understanding architectural design patterns (like MVC, MVP, MVVM etc.) is essential for producing a maintainable, clean, extendable and testable source code. MVC MVC stands for Model-View-Controller. It is a software design pattern which was introduced in 1970s. Also, MVC pattern forces a separation of concerns, it means domain model and controller logic are decoupled from user interface (view). As a result maintenance and testing of the application become simpler and easier. MVC design pattern splits an application into three main aspects: Model, View and Controller MVC Model The Model represents a set of classes that describe the business logic i.e. business model as well as data access operations i.e. data model. It also defines business rules for data means how the data can be changed and manipulated. View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI. Controller The Controller is responsible to process incoming requests. It receives input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Typically, it acts as the coordinator between the View and the Model. MVP This pattern is similar to MVC pattern in which controller has been replaced by the presenter. This design pattern splits an application into three main aspects: Model, View and Presenter. This pattern is commonly used with ASP.NET Web Forms applications which require to create automated unit tests for their code-behind pages. This is also used with windows forms. MVP Model The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated. View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the presenter as the result. This also transforms the model(s) into UI. Presenter The Presenter is responsible for handling all UI events on behalf of the view. This receive input from users via the View, then process the user's data with the help of Model and passing the results back to the View. Unlike view and controller, view and presenter are completely decoupled from each other’s and communicate to each other’s by an interface. Also, presenter does not manage the incoming request traffic as controller. Key Points about MVP Pattern: User interacts with the View. There is one-to-one relationship between View and Presenter means one View is mapped to only one Presenter. View has a reference to Presenter but View has not reference to Model. Provides two way communication between View and Presenter. MVVM MVVM stands for Model- View-View Model. This pattern supports two-way data binding between view and View model. This enables automatic propagation of changes, within the state of view model to the View. Typically, the view model uses the observer pattern to notify changes in the view model to model. MVVM Model The Model represents a set of classes that describes the business logic and data. It also defines business rules for data means how the data can be changed and manipulated. View The View represents the UI components like CSS, jQuery, html etc. It is only responsible for displaying the data that is received from the controller as the result. This also transforms the model(s) into UI.. View Model The View Model is responsible for exposing methods, commands, and other properties that helps to maintain the state of the view, manipulate the model as the result of actions on the view, and trigger events in the view itself. This pattern is commonly used by the WPF, Silverlight, Caliburn, nRoute etc. Key Points about MVVM Pattern: User interacts with the View. There is many-to-one relationship between View and ViewModel means many View can be mapped to one ViewModel. View has a reference to ViewModel but View Model has no information about the View. Supports two-way data binding between View and ViewModel. Open Source Development Framework for developing web applications Using Spring MVC Java Web Development Framework There are a majority of enterprises running Java applications and working on Java web development framework. What remains to be seen is that a number of companies are tied to the conventional web development framework and haven’t actually started to anticipate what could be the best Java web development framework. 10 best Java web development framework 1. Struts 2 2. JSF (Java Server Faces) 3. Spring MVC 4. Wicket 5. Stripes 6. Tapestry 7. RIFE 8. Seam 9. Google Web Toolkit (GWT) 10. OpenXava Java Web Development Frameworks “Full-stack” frameworks: SEAM, RIFE, Spring (?) Address back-end soup-to-nuts web development Typically include MVC component AND integration with enterprise systems ADVANTAGES: complete stack, limited glue code DISADVANTAGES: less plug-n-play; “heavyweight” MVC frameworks: Struts, Spring MVC, etc. Address “page flow” and reusability in web development Focused on simplifying MVC-based web application Limited out-of-the-box integration with “enterprise” ADVANTAGES: pluggable architecture, “lightweight” DISADVANTAGES: glue code Choosing an MVC Framework Two types of MVC frameworks: • Action-based (aka Push-based MVC) Data is pushed from controller to view Focused more on request flow Struts, Spring MVC, Stripes • Component-based (aka Pull-based MVC) Data is pulled in view Focused more on view rendering JSF, Wicket, Tapestry Spring MVC & Struts 1)Struts is a web framework while spring is an application framework in which spring MVC is one of the modules of spring framework. 2)Spring is a Layered Architecture while Struts is not. 3)Struts is heavy weight while Spring is light weight. 4)Struts supports tag Library while Spring does not. 5)Spring is loosely coupled while Struts is tightly coupled. 6)Spring provides easy integration with ORM technologies while in struts, we need to do coding manually. 7)Struts easily integrate with other client side technologies. It is not easy in case of spring. Spring MVC Architecture Front Controller design pattern This design pattern enforces a single point of entry for all the incoming requests. All the requests are handled by a single piece of code which can then further delegate the responsibility of processing the request to further application objects. Spring MVC Architecture MVC design pattern This design pattern helps us develop loosely coupled application by segregating various concerns into different layers. MVC design pattern enforces the application to be divided into three layers, Model, View and Controller. Spring MVC Architecture Spring’s MVC module Spring’s MVC module is based on front controller design pattern followed by MVC design pattern. All the incoming requests are handled by the single servlet named DispatcherServlet which acts as the front controller in Spring’s MVC module. The DispatcherServlet then refers to the HandlerMapping to find a controller object which can handle the request. DispatcherServlet then dispatches the request to the controller object so that it can actually perform the business logic to fulfil the user request. (Controller may delegate the responsibility to further application objects known as service objects). The controller returns an encapsulated object containing the model object and the view object (or a logical name of the view). In Spring’s MVC, this encapsulated object is represented by class ModelAndView. In case ModelAndView contains the logical name of the view, the DispatcherServlet refers the ViewResolver to find the actual View object based on the logical name. DispatcherServlet then passes the model object to the view object which is then rendered to the end user. Spring MVC Architecture Spring’s MVC module Open Source Project Development Framework To build upon a basic project development framework for NIC, Uttar Pradesh so that any project development team can develop an application with less effort and in a structured and efficient manner. The framework shall be used as a basic template for development of web applications in open source framework. The framework is a direction and not binding for the development by different groups. It is open for upgradation and improvement based on the inputs received from various divisions/groups of NIC and never shall be thought of as a frozen or proprietary thing. 1. Controller 2. View (JSP pages) OSPDF 7 Layers: 3. DTO (Data Transfer Objects) (Document) 4. Service 5. Utilities 6. Entities 7. DAO (Data Access Object) Open Source Project Development Framework User Request (request URL) DispatcherServlet (Front Controller) Response back to User HandlerMapping PersonController [stores form entries to DTO] through PersonService(DTO) PersonDAO Utility Class PersonService Calls GenericDAO for CRUD for [Converts DTO to Entity for operations using Hibernate. Authentications WRITE and Entity to DTO for , validations, READ] conversions etc. calls PersonDAO(Entity) OSPDF using Spring MVC Development Environment For project development using Spring MVC the following things are required: IDE Eclipse: Eclipse is an integrated development environment (IDE) for Java and other programming languages like C, C++, PHP, and Ruby etc. Development environment provided by Eclipse includes the Eclipse Java development tools (JDT) for Java, Eclipse CDT for C/C++, and Eclipse PDT for PHP, among others. Application Server JBoss AS 8.0: The JBoss applications server is a J2EE platform for developing and deploying enterprise Java applications, Web applications and services, and portals. J2EE allows the use of standardized modular components and enables the Java platform to handle many aspects of programming automatically. OSPDF using Spring MVC Development Environment Database PostgreSQL 9.4: PostgreSQL is a powerful, open source object-relational database system.