Introduction to design patterns used in the JSF framework

Source: Internet
Author: User
Tags abstract

Design patterns can help users abstract details at a higher level and better understand the architecture. If you are familiar with the GoF design pattern and the JavaServer Faces (JSF) framework, this article can help you gain insight into the design patterns used in the JSF framework and understand how they work.

This article explores the design patterns used in the JSF framework. The design patterns discussed in detail include Singleton, Model-view-controller, Factory method, state, composite, decorator, strategy, Template methods, and Observer mode.

Design Patterns and JavaServer Faces (JSF) technology

Start with a brief introduction to the schema and JSF framework.

Patterns, design patterns are a universally applicable method of abstracting problems and solutions. Because schemas are recognized by all developers and architects, schemas can save time and resources. In the case of layman's terms, the pattern is a proven solution to a well-known problem. Patterns can be reused, and reuse makes the solution more robust.

Java Server Faces, the JSF architecture is a WEB application framework. It is driven by the Java Community Process (JCP) and is expected to be a standard framework for WEB application development. There are more than 50 frameworks currently used to develop WEB applications, which illustrates the urgent need to standardize the framework, which is the goal of the JSF framework!

Deep analysis of JSF patterns

Now let's talk about the various design patterns in the JSF architecture. This article discusses in detail Singleton, Model-view-controller, Factory method, state, composite, decorator, strategy, Template method, and Observer design pattern. I will analyze the purpose of each pattern and its role in the JSF framework.

Singleton mode

The purpose of the Singleton mode is to ensure that only one instance of a class is loaded, and that the instance provides a global access point. When you start a Web application with JSF support, the Web container initializes a Facesservlet instance. At this stage, Facesservlet instantiates application and lifecycle instances for each WEB application. These instances take a well-known Singleton pattern, and usually require only one instance of that type.

A WEB application that uses JSF requires only one instance of the application and lifecycle classes. Lifecycle manages the entire lifetime of multiple JSF requests. Because its state and behavior are shared among all requests, these objects are justified by using the Singleton pattern. The Phaselisteners of lifecycle maintenance is also Singleton mode. Phaselisteners is shared by all JSF requests. The Singleton pattern can be widely used in the JSF framework to reduce memory footprint and provide global access to objects. Navigationhandler (used to determine the logical result of the request) and Viewhandler (used to create the view) is also an example of using the Singleton pattern.

Model-view-controller (MVC)

The purpose of the MVC pattern is to separate the data (that is, model) from the data Representation (View). If your application has multiple representations, you can reuse the controller and model code only by replacing the view layer. Similarly, if you need to change the model, you can largely do without changing the view layer. The controller handles user actions, and user actions can cause model changes and view updates. When a user requests a JSF page, the request is sent to Facesservlet. Facesservlet is the front-end controller servlet used by JSF. Like many other Web application frameworks, JSF uses the MVS pattern to eliminate coupling between views and models. To centralize processing of user requests, the controller servlet changes the model and navigates the user to the view.

Facesservlet is the controller element that all user requests will pass through in the JSF framework. Facesservlet analyzes user requests and invokes various actions on the model using managed beans. Background (backing) or managed (managed) beans are examples of this model. The JSF user interface (UI) component is an example of a view layer. The MVC pattern breaks down tasks to developers with different skills so that the tasks can be performed concurrently so that the GUI designer can create JSF pages with rich UI components, while back-end developers can create managed beans to write specialized business logic code.

Factory method Mode

The purpose of the Factory method pattern is to define an interface for creating objects, but to defer object instantiation to subclasses. In the JSF architecture, the Factory method pattern is used to create objects. Lifecyclefactory is a factory object that creates and returns a lifecycle instance. The Getlifecycle (String Lifecycleid) method of the lifecyclefactory takes the Factory mode pattern, creates (if necessary) according to Lifecycleid, and returns the lifecycle instance. A custom JSF implementation can redefine a getlifecycle abstract method to create a custom lifecycle instance. The default JSF implementation provides the default lifecycle instance. In addition, for each JSF request, Facesservlet gets facescontext from Facescontextfactory. Facescontextfactory is an abstract class that exposes a getfacescontext API,JSF implementation that provides a concrete implementation of the Facescontextfactory and Getfacescontext APIs. This is another example using the Factory method pattern, where the specific facescontextfactory implementation creates the Facescontext object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.