The observer mode of the Java design Pattern (Observer mode) is introduced _java

Source: Internet
Author: User

Java depth to a certain extent, it is inevitable to encounter the concept of design patterns, understanding design patterns, will make their Java interface or abstract class applications have a deeper understanding. Design patterns are widely used in midsize systems in Java, follow certain programming pattern, can make own code easy to understand, easy to communicate, OBSERVER (Observer) pattern is a more commonly used pattern, especially in the interface design application widely, and this tutorial is concerned with Java in the electronic Commerce system application , so I want to analyze the application of observer from the example of e-commerce.

Although online stores in various forms, each site has its own characteristics, but also has its general commonalities, the single "commodity changes in order to inform subscribers in a timely manner" is a common model of many online stores, this model is similar to observer patern.

Specifically, if the online store in the name of the goods in the price changes, if the system can automatically notify members, will be online stores to distinguish the traditional store a major feature. This involves adding observer to the product product so that when the product details change, observer can automatically observe the change and be able to update or notify the action in a timely manner.

The Java API also provides us with a ready-made Observer interface Java.util.Observer. We just need to use it directly.

We have to extends Java.util.Observer to really use it:
1. Methods of providing add/delete observer;
2. Provide notification (NOTISFY) of all observer methods.

Copy Code code as follows:

The product class can be used by the JSP directly using Usebean to invoke this class primary execution product database insert Update
public class product extends observable{
private String name;
private float price;
Public String GetName () {return name;}
public void SetName () {
This.name=name;
Set Change point
Setchanged ();
Notifyobservers (name);
}
public float GetPrice () {return price;}
public void Setprice () {
This.price=price;
Set Change point
Setchanged ();
Notifyobservers (New Float (price));
}
The following can be the database update Insert command.
public void Savetodb () {
.....................
}
}

We note that in the Setxxx method in the product class, we set the Notify (notification) method, which, when the JSP table is used in a monotone setxxx, actually triggers the Notisfyobservers method, which informs the corresponding observer that action should be taken.

Let's look at the code of these observers and what exactly they are doing:

Copy Code code as follows:

The observer Nameobserver is primarily 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 price of the product.
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:p rice Changet to" +price);
}
}
}

In JSP we can formally execute this viewer program:

Copy Code code as follows:

<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 ("Product Data change save!") and has automatically notified the customer ");
}else{
Join the Observer
Product.addobserver (Nameobs);
Product.addobserver (Priceobs);
%>
Request.getrequesturi () is the name of the program that produces this JSP, which is calling itself
<form action= "<%=request.getrequesturi ()%>" method=post>
<input Type=hidden name= "Save" value= "1" >
Products name £ º <input type=text name= "Name" >
Product Price: <input type=text name= "Prices" >
<input type=submit>
</form>
<%
}
%>

The implementation of the JSP program, there will be a form input interface, you need to enter product name product price, click Submit, or execute the JSP if (Request.getparameter ("save")!=null between the code.

Because the automatic assignment concept of data JavaBeans is used here, the actual program executes the SetName Setprice statement automatically. You will find the following information in the server console:

Copy Code code as follows:

Nameobserver:name Changet to????? (The product name entered in the JSP form)
Priceobserver:p Rice Changet to??? (The price of the product entered in the JSP form);

This means that the observer is already on the move.

At the same time you will get the information on the browser side of the JSP:

1. Product Data Change Save! and has automatically notified the customer

As a result of the use of JSP concepts, there are many automatic actions, now invoke the Observer's Java code is written as follows:

Copy Code code as follows:

public class Test {
public static void Main (String args[]) {
Product Product=new product ();
Nameobserver nameobs=new nameobserver ();
Priceobserver priceobs=new priceobserver ();

Join 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 red.
Priceobserver:p Rice Changet to 9.22

This means that the observer is in action.

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.