Design Pattern-Observer

Source: Internet
Author: User
Design Pattern-Observer

Banqiao Renren http://www.jdon.com

Click here to attend monthly design model lectures

To a certain extent, Java will inevitably encounter the design pattern concept. Understanding the design pattern will give you a deeper understanding of interfaces or abstract class applications in Java. the design pattern is widely used in medium-sized Java systems and follows a certain programming pattern.CodeIt is easy to understand and communicate with each other. The Observer (observer) mode is a commonly used mode, which is especially widely used in interface design. This site focuses on the application of Java in e-commerce systems, therefore, we want to analyze the application of observer from e-commerce instances.

Although online stores have various forms and each site has its own characteristics, it also has its common characteristics. A single site is "product changes, so that subscribers can be notified in a timely manner, it is a common model for many online stores. This mode is similar to the observer patern observer mode.

Specifically, if the name and price of products in the online store change, if the system can automatically notify members, it will be a major difference between the online store and traditional store. therefore, you need to add the observer role to the product so that when the product details change, the observer can automatically observe this change and perform timely update or y actions.

The Java API also provides the ready-made observer interface java. util. Observer for us. We only need to use it directly.

We must use extends java. util. Observer to actually use it:
1. Provides the Add/delete observer method;
2. provides methods for notifying all observers;

// The product class can be called by JSP directly using usebean. This class mainly performs product database insertion and update.
Public class product extends observable {

Private string name;
Private float price;

Public String getname () {return name ;}
Public void setname (string name ){
This. Name = Name;
// Set the Change Point
Setchanged ();
Yyobservers (name );

}

Public float getprice () {return price ;}
Public void setprice (float price ){
This. Price = price;
// Set the Change Point
Setchanged ();
Notifyobservers (new float (price ));

}

// The following commands can be used to update and insert a database.
Public void savetodb (){
.....................

}

We noticed that in the setxxx method of the product class, we set the notify y (notification) method. When the JSP form calls setxxx (for how to call it, see my other article.ArticleIn fact, the notisfyobservers method is triggered, which notifies the corresponding observer to take action.

Let's take a look at the code of these observers and what actions they have taken:

// The Observer nameobserver is mainly used to observe the product name (name ).
Public class nameobserver implements observer {

Private string name = NULL;

Public void Update (observable OBJ, object Arg ){

If (ARG instanceof string ){

Name = (string) ARG;
// Product name change value in name
System. Out. println ("nameobserver: Name changet to" + name );

}

}

}

// The Observer priceobserver is mainly used to observe the product price.
Public class priceobserver implements observer {

Private float price = 0;

Public void Update (observable OBJ, object Arg ){

If (ARG instanceof float ){

Price = (float) Arg). floatvalue ();

System. Out. println ("priceobserver: price changet to" + price );

}

}

}

In JSP, we can officially execute this observer.Program:

<JSP: usebean id = "product" Scope = "session" class = "product"/>
<JSP: setproperty name = "product" property = "*"/>

<JSP: usebean id = "nameobs" Scope = "session" class = "nameobserver"/>
<JSP: setproperty name = "product" property = "*"/>

<JSP: usebean id = "priceobs" Scope = "session" class = "priceobserver"/>
<JSP: setproperty name = "product" property = "*"/>

<%

If (request. getparameter ("save ")! = NULL)
{
Product. savetodb ();

Out. println ("save product data changes! And the customer has been notified automatically ");

} Else {

// Add the observer
Product. addobserver (Nameobs);

Product. addobserver (Priceobs);

%>

// Request. getrequesturi () is the name of the program that generates the JSP, that is, it calls itself
<Form action = "<% = request. getrequesturi () %>" method = post>

<Input type = hidden name = "save" value = "1">
Product Name: <input type = text name = "name">
Product Price: <input type = text name = "price">
<Input type = submit>

</Form>

<%

}

%>

 

 

 

When you run the JSP program, a form entry interface is displayed. You need to enter the product price of the product name. Click "Submit" to continue executing the JSP program.
If (request. getparameter ("save ")! = NULL.

Because the automatic value assignment of Data JavaBeans is used here, the actual program automatically executes the setname setprice statement. You will find the following information in the server console ::

Nameobserver: Name changet ????? (The product name entered in the JSP form)

Priceobserver: price changet ??? (The product price entered in the JSP form );

This indicates that the observer is already in action .!!
At the same time, you will get the information on the browser that executes JSP:

Save product data changes! The customer has been notified automatically.

Due to the use of JSP, many automatic actions are implied. the Java code of the called observer is as follows:

 

Public class test {

Public static void main (string ARGs []) {

Product = new product ();

Nameobserver nameobs = new nameobserver ();
Priceobserver priceobs = new priceobserver ();

// Add the observer
Product. addobserver (nameobs );
Product. addobserver (priceobs );

Product. setname ("orange red ");
Product. setprice (9.22f );

}

}

 

 

 

You will find the following information ::

Nameobserver: Name changet to orange

Priceobserver: price changet to 9.22

This indicates that the observer is moving .!!

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.