Count proxy mode in Java Design Mode

Source: Internet
Author: User
Java design pattern-count proxy pattern-Linux general technology-Linux programming and kernel information. The following is a detailed description. The counting proxy mode is useful when a client object calls a service provider object before and after a series of additional functions such as logging and counting. In the Count proxy mode, we recommend that you encapsulate these additional functions in a single object. This object refers to the counting proxy object, rather than putting these additional functions into the service provider. One feature of a good object design is that objects must focus on providing specific functions. In other words, the ideal object should not be irrelevant. Similar functions such as logging and counting are encapsulated into a separate object, so that the service provider object only provides its own specific functions. That is to say, only service provider objects are allowed to execute well-defined and specific tasks.

The counting proxy is designed as an object with the same interface as the service provider that can be accessed by the customer. The customer object does not directly access the service provider, but calls the method on the counting proxy object. After the counting proxy executes the necessary logging and counting functions, then pass the method call to the Service to provide objects. 1
2828o18m1i2q
Figure1: Generic Class Association When the Counting Proxy Pattern Is Applied


The following example shows how to use the Count proxy in an application.

Example:

Let's design an Order Class, Class Level 2. The OrderIF interface declares a simple method for getAllOrders to read all orders in the database.
Q0hxcf0kyz9y

Figure2: Order Class Hierarchy

Public interface OrderIF {
Public Vector getAllOrders ();
}

As a Real-Time getallorderspipeline, The orderclass uploads the fileutiltool class to read order items from the order.txt file.

Public class Order implements OrderIF {
Public Vector getAllOrders (){
FileUtil fileUtil = new FileUtil ();
Vector v = fileUtil. fileToVector ("orders.txt ");
Return v;
}
}

Let's assume that when getAllOrders () is called, the time and number of log files to be recorded need to be retrieved.

This additional function can be implemented by designing a separate OrderProxy class, which implements the OrderIF interface like the real object Order. This ensures that the OrderProxy object provides the same interface as the real object Order. 3
6l0166q4mc67
Figure3: Order Class Hierarchy with the Counting Proxy

Public class OrderProxy implements OrderIF {
Private int counter = 0;
Public Vector getAllOrders (){
Order order = new Order ();
Counter ++;
Long t1 = System. currentTimeMillis ();
Vector v = order. getAllOrders ();
Long t2 = System. currentTimeMillis ();
Long timeDiff = t2? T1;
String msg = "Iteration =" + counter + ": Time =" + timeDiff + "ms ";
// Log the message
FileUtil fileUtil = new FileUtil ();
FileUtil. writeToFile ("log.txt", msg, true, true );
Return v;
}
}

The customer's MainApp calls the getAllOrders () method on the OrderProxy object just like calling the real object Order. The OrderProxy object passes this call to the real object Order, calculate the time spent reading all orders and use the FileUtil help class to record the log files. In this process, OrderProxy plays the role of counting proxy.

Public class MainApp {
Public static void main (String [] args ){
OrderIF order = new OrderProxy ();
Vector v = order. getAllOrders ();
V = order. getAllOrders ();
V = order. getAllOrders ();
V = order. getAllOrders ();
}
}

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.