Original: fanix
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 so that your code can be easily understood and communicated. The observer pattern is a commonly used pattern, especially 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, is a common model for many online stores. This mode is similar to observer patern.
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;
Program code:
- // The product class can be called by JSP directly using usebean. This class mainly performs product database insertion and update.
- Public ClassProductExtendsObservable {
- Private StringName;
- Private FloatPrice;
- Public StringGetname (){ReturnName ;}
- Public VoidSetname (){
- This. Name = Name;
- // Set the Change Point
- Setchanged ();
- Yyobservers (name );
- }
- Public FloatGetprice (){ReturnPrice ;}
- Public VoidSetprice (){
- 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 VoidSavetodb (){
- .....................
- }
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 ), in 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:
Program code:
- // The Observer nameobserver is mainly used to observe the product name (name ).
- Public ClassNameobserverImplementsObserver {
- Private StringName =Null;
- Public VoidUpdate (observable OBJ,ObjectArg ){
- If(ARGInstanceof 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 ClassPriceobserverImplementsObserver {
- Private FloatPrice = 0;
- Public VoidUpdate (observable OBJ,ObjectArg ){
- If(ARGInstanceof Float){
- Price = ((Float) Arg). floatvalue ();
-
- System. Out. println ("priceobserver: price changet to" + price );
- }
- }
- }
In JSP, We can formally execute this observer program:
Program code:
- <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:
Program code:
- Public ClassTest {
- Public Static VoidMain (StringARGs []) {
- Product =NewProduct ();
- Nameobserver nameobs =NewNameobserver ();
- Priceobserver priceobs =NewPriceobserver ();
- // 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 .!!