The counting agent mode of Java design pattern

Source: Internet
Author: User

Describe:

The Count agent mode is useful when the client object invokes a series of additional features, such as log (logging) and count (counting), before and after the method on the service provider object. The Count agent pattern suggests encapsulating these additional functions in a single object, which refers to the counting agent object, rather than putting these additional functionality implementations into the service provider's interior. One feature of good object design is that objects are focused on providing specific functionality. In other words, the ideal object should not do all sorts of irrelevant things. Encapsulate similar functions such as log (logging) and count (counting) into a single object, and let the service provider object provide only its own specific functionality. That is, only the service provider object is allowed to perform well-defined, specific tasks.

The Count agent is designed to be a client-accessible object with the same interface as the service provider. Instead of directly accessing the service provider, the client object is invoking the method on the Count proxy object, and the Count agent performs the necessary record log (logging) and count (counting) functions, and then passes the method call to the service providing the object. As shown in Figure 1

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 where the class hierarchy, like the diagram 2,orderif interface, declares an easy way to read all orders in the database getallorders.

Figure2: Order Class Hierarchy

public interface OrderIF {

public Vector getAllOrders();

}

As part of the implementation of the Getallorders method, the order class is useful for the Fileutil tool class to read orders from Order.txt files.

public class Order implements OrderIF {

public Vector getAllOrders() {

FileUtil fileUtil = new FileUtil();

Vector v = fileUtil.fileToVector("orders.txt");

return v;

}

}

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.