Java is in depth to a certain extent, which is inevitable to meet the concept of Design Pattern, which will make yourself have a deeper understanding of interfaces or abstract classes in Java. Design mode in Java medium-sized system The application is extensive, follow a certain programming mode, in order to make your code easy to understand, easy to communicate, Observer mode is a relatively common model, especially in interface design, and this site is concerned about Java Applying in an e-commerce system and thus analyzes Observer applications from e-commerce instances.
Although there is a variety of online stores, each site has its own characteristics, but there is also its general commonality, and "the" community "is in order to inform the subscriber in time, it is a common model of many online stores, this model is similar to Observer Patern Observer mode.
Specifically, if there is a change in the online store in the name price, if the system can automatically notify the member, it will be a major feature from the online store to distinguish the traditional store. This needs to add Observer to the role such as Product. When the details change, Observer can automatically observe this change and can perform a timely UPDATE or NOTIFY action.
Java's API also provides us with ready-made Observer interface java.util.observer. We can use it directly.
We must use extends java.util.observer to truly use it: 1. Provide the method of add / delete observer; 2. Provide all Observer methods for Notisfy;
// class products are available directly UseBean Jsp call the main class products perform database insert 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 (); notifyObservers (name);} public float getprice () {returnrate;} public void setprice (float price) {this.price = price; // set change Point setChanged (); NotifyObservers (new float (price));} // The following can be a database update insert command. Public void savetoDB () {................... ..}
We noticed that in the setxxx method in the Product class, we set up the Notify method, when the JSP form calls setXXX (how to call some other articles), actually trigger the NotisfyobServers method, which will notify The corresponding observers should take action.
Let's take a look at these observer's code. What actions they have taken:
// observer NameObserver mainly used public class NameObserver implements Observer {private String Name of the product (name) viewed 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 Changt to" name);}}} // Observer PriceOBServer mainly used to observe product prices (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 ";}}} JSP we can formally implement this observer program: