Observer design pattern

xiaoxiao2021-03-06  73

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:

<% IF (Request.GetParameter ("save")! = null) {product.savetoDB (); out.println ("Product data change saves! And automatically notify customers" );} else {// Add observer product.addobserver (NameObs); pRODUCT.ADDOBSERVER (PriceObs);%> //Request.getRequesturi () is a program name that generates this JSP, that is, call yourself

Product Name: Product price: <%}%> Performing a change JSP program, there will be a table single entry interface, you need to enter the product name product price, click Submit, or execute The code between the JSP IF ("Save")! = NULL is used. Because the data JavaBeans automatic assignment concept, the actual program automatically performs the setName setPrice statement. You will find the following in the server console Information :: NameObserver: Name ChangeT to ????? (YSP form Product Name) PriceObserver: Price ChangeT to ??? (Enter product price in the JSP form);

This shows that the observer is already in action. !! At the same time, you will get information on the JSP browser side: product data changes! And you have automatically notified customers

Due to the JSP concept, the above is implied with many automatic actions, and the Java code to call the observer is written as follows:

public class Test {public static void main (String args []) {Product 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 be found below :: NameObserver: Name Change To Orange PriceOBserver: Price Changet To 9.22

This shows that the observer is acting.!!

转载请注明原文地址:https://www.9cbs.com/read-93065.html

New Post(0)