When Java goes deeper to a certain level, it will inevitably encounter the concept of design pattern. Understanding the design pattern will give you a deeper understanding of interfaces or abstract class applications in Java. Design mode is widely used in Java medium-sized systems. Only by following certain programming modes can your code be easy to understand and communicate. The Observer mode is a relatively common model, especially in interface design. This tutorial focuses on the application of Java in e-commerce systems, so I want to analyze the application of Observer from e-commerce examples.
Although online stores are diverse in form and each site has its own characteristics, it also has its general commonality. Just talking about "changes in products to promptly notify subscribers" is a common model among many online stores. This model is similar to Observer patron .
Specifically, if the product in the online store changes in terms of name and price, and if the system can automatically notify members, it will be a major feature of the online store distinguishing between traditional stores. This requires adding the role of Observer to the product product, so that when product details change, Observer can automatically observe this change and perform timely update or notify actions.
Java API also provides us with an off-the-shelf Observer interface Java.util.Observer. We just need to use it directly.
We have to extends Java.util.Observer to actually use it:
1. Provide the method of Add/Delete observer;
2. Provide notification (notisfy) methods for all observers.
The code copy is as follows:
//The product class can be directly used by Jsp to call the class 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(){
this.name=name;
//Set the change point setChanged();
notifyObservers(name);
}
public float getPrice(){ return price;}
public void setPrice(){
this.price=price;
//Set the change point setChanged();
notifyObservers(new Float(price));
}
//The following can be the database update insert command.
public void saveToDb(){
.............
}
}
We noticed that in the setXXX method in the product class, we set the notify method. When the Jsp form calls setXXX, the notisfyObservers method is actually triggered, which will notify the corresponding observer that action should be taken.
Here is a look at the codes of these observers, what exactly did they take:
The code copy is as follows:
//Observer NameObserver is mainly used to observe 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;
//The product name change value is in name System.out.println("NameObserver :name change to "+name);
}
}
}
//Observer PriceObserver is mainly used to observe product prices.
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 change to "+price);
}
}
}
In Jsp, we can officially execute this observer program:
The code copy is as follows:
<jsp:useBean id="product" scope="session" />
<jsp:setProperty name="product" property="*" />
<jsp:useBean id="nameobs" scope="session" />
<jsp:setProperty name="product" property="*" />
<jsp:useBean id="priceobs" scope="session" />
<jsp:setProperty name="product" property="*" />
<%
if (request.getParameter("save")!=null)
{
product.saveToDb();
out.println("Product data changes are saved! and the customer has been automatically notified");
}else{
//Add to observer product.addObserver(nameobs);
product.addObserver(priceobs);
%>
//request.getRequestURI() is the program name that generates this jsp, which means calling yourself <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 executing the Jsp program, a form entry interface will appear. You need to enter the product name and product price. After clicking Submit, you will still execute the code between the if (request.getParameter("save")!=null) of the jsp.
Since the automatic assignment concept 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:
The code copy is as follows:
NameObserver :name change to ????? (The product name entered in the Jsp form)
PriceObserver:price change to ???(the product price entered in the Jsp form);
This shows that the observer is already acting.
At the same time, you will get information on the browser where you execute jsp:
1. Save product data changes! And automatically notify customers
Since the above article uses the concept of jsp, it implies many automatic actions. Now write the Java code that calls the observer as follows:
The code copy is as follows:
public class Test {
public static void main(String args[]){
Product product=new Product();
NameObserver nameobs=new NameObserver();
PriceObserver priceobs=new PriceObserver();
//Add to observer product.addObserver(nameobs);
product.addObserver(priceobs);
product.setName("Orange is red");
product.setPrice(9.22f);
}
}
You will find the following information:
NameObserver :name change to Orange is red
PriceObserver:price change to 9.22
This shows that the observer is acting.