Java Multithreading (ii)--the design pattern used

Source: Internet
Author: User

Next: Java Multithreading (i) http://www.cnblogs.com/ChaosJu/p/4528895.html

Java implementation of multi-threading mode two, the implementation of the Runable interface design mode-static proxy mode

I. Proxy mode
    • Definition of proxy mode

Proxy pattern is the structural pattern of an object, which provides a proxy object to an object and controls the reference to the original object by the proxy object.

Proxy mode does not change the original interface and behavior, but the agent to do something, the agent can control the original target, such as: agents, agents will only sell things, but will not change behavior, will not make things.

    • Roles involved in proxy mode
    1. Abstract role: A common interface for declaring real objects and proxy objects;
    2. Proxy role: The proxy object role contains a reference to the real object, allowing you to manipulate the real object, while the proxy object provides the same interface as the real object to replace the real object at any time with the same interface so that the real object can be substituted at any moment. At the same time, the proxy object can execute the real object operation, attach other operations, which is equivalent to the object real object to encapsulate;
    3. Real role: The real object represented by the proxy role is the object we end up applying.
    • Class structure diagram for proxy mode
Second, static agent
    • Example of proxy mode

Landlord I want to get married, but afraid of trouble, so find wedding company help decorate bridal chamber, take the scene, host the wedding, we want to know the real marriage is I am not a wedding company, our common purpose is to marry smoothly,

In fact, I married to find a wedding company to help, wedding company to help me get married, and add a lot of modification.

Role Analysis:

Real role: Landlord

Agent role: Wedding Company

Abstract role (Common interface): Marriage

/*** Static proxy design mode * 1, Real role * 2, Proxy role: Hold a reference to a real role * 3, both implement the same interface * *@authorChaosju **/ Public classStaticproxy {     Public Static voidMain (string[] args) {//create a real roleMarry you =NewLouzhu (); //Create a proxy role + real role referenceMarry Company =NewWeddingcompany (you); //Perform TasksCompany.marry (); }}//InterfaceInterfacemarry{ Public Abstract voidmarry ();}//Real charactersclassLouzhuImplementsmarry{@Override Public voidmarry () {System.out.println ("Louzhu and Chang-e are married ...."); }    }//Agent RoleclassWeddingcompanyImplementsmarry{PrivateMarry Louzhu;  PublicWeddingcompany () {} PublicWeddingcompany (Marry louzhu) { This. Louzhu =Louzhu; }    Private voidbefore () {System.out.println ("The Bridal chamber ..."); }    Private voidAfter () {System.out.println ("Bridal chamber ..."); }
@Override Public voidmarry () {before (); Louzhu.marry (); After (); } }

Execution Result:

Disadvantages:

1) The proxy class and the delegate class implement the same interface, and the proxy class implements the same method through the delegate class. There is a lot of code duplication. If an interface adds a method, all proxy classes need to implement this method in addition to all implementation classes that need to implement this method. Increases the complexity of code maintenance.

2) The proxy object serves only one type of object if it is to serve multiple types of objects. It is bound to be a proxy for each object, static agent in the program is a little larger than the capacity of the.

Example: The agent can be unified management of the implementation class, such as before invoking the concrete implementation class, need to print the log and other information, so we just need to add a proxy class, in the proxy class to add the ability to print the log, and then call the implementation class, so as to avoid modifying the concrete implementation class. Meet what we call the opening and closing principle. However, if you want each implementation class to add the ability to print the log, you need to add more than one proxy class, and each method in the proxy class needs to add the print log function (such as removing, modifying, and querying on the proxy method above).

That is, a static proxy class can only serve a specific interface (service). If you want to serve multiple interfaces, you need to set up a number of proxy classes. You need to introduce dynamic proxies.

  • Application of static proxy mode in Java Multithreading implementation of Runable interface mode
  • //threading classes that implement the Runnable interface Public classProgrammerImplementsRunnable { Public voidrun () { for(inti=0;i<1000;i++) {System.out.println ("Knock HelloWorld ..."); }    }} Public classProgrammerapp { Public Static voidMain (string[] args) {//1), create a real roleProgrammer Pro =NewProgrammer (); //2), create proxy role + real role referenceThread proxy =NewThread (PRO); //3), invoke. Start () Start threadProxy.start (); }}

    Why do static proxies be implemented in the above way?

  • Both thread and Programer implement the Runnable interface
  • Thread as a proxy role----hold real role references
  • Programer is a real character.

Java Multithreading (ii)--the design pattern used

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.