Virtual proxy mode-virtual proxy (Java implementation)

Source: Internet
Author: User

Virtual Agent Mode-virtual Proxy

Virtual proxy mode defers the actual time required for object instantiation. If a proxy object can handle a real object before it needs to work, the real object is not needed for the moment.

Pros: The advantage of this approach is that when the application starts, it accelerates application startup because it does not require all objects to be created and loaded.

disadvantage: because there is no guarantee that a particular application object will be created, any access to this object needs to be detected to verify that it is not empty (null). Performance reduction is not just a lot of code so simple, ' if ' This jump class statement is likely to block the CPU's instruction flow, although there is a score prediction technology, but branch prediction is also a hit rate ....

Let's go back and talk about the subject of this article ....

For example: There are a group of people who come to the boss to talk about things and need to make an appointment to add these appointments to the schedule list. "Add Appointment to plan list" This thing, in itself does not need the boss to show himself, the boss no longer when you can find an assistant to do, only to perform tasks in the list of tasks, the boss needs to show up (scene is: Everyone is to look for the boss to do things, not to find assistants to do things, The Assistant is only responsible for the boss in the absence of help collect everyone's needs, and finally collected, he invited the boss, the boss to deal with all things).

Approvable interface

Both the boss and the assistant need to implement this interface.

The helper implements this interface, and when the helper's approve method is invoked, the assistant invites the boss to come over and let the boss handle the list.

The boss implements this interface because these lists need to be handled by the Boss (approve)

Public interface Approvable {    void approve ();}
Boss Class

The boss is worth it, a trip is not easy, so the small things to the assistant to do (for example: Collect visitors to find the boss what things, and statistics a list). There is no need to call the boss out as soon as it comes up.

Import Java.util.linkedlist;import Java.util.list;public class Boss implements approvable {    list<string> Orders;    {        System.out.println ("\nboss appears ... \ n");    }    Public Boss () {        this.orders = new linkedlist<> ();    }    Public Boss (list<string> orders) {        if (orders! = null) {            this.orders = orders;        } else {            This.orde rs = new linkedlist<> ();        }    }    public void AddOrder (String order) {        this.orders.add (order);    }    @Override public    Void Approve () {        while (orders.size () >0) {            String order = orders.remove (0);            SYSTEM.OUT.PRINTLN ("Boss handles tasks <" + order + ">");        }        System.out.println ();    }}
Assistant Class

Boss of the agent class, the owner's assistant.

Import Java.util.linkedlist;import java.util.list;/** * Boss's agent * is responsible for collecting orders list, processing inventory before the collection work will not be the boss show up, * Boss can appear one o'clock in the evening * * public class Assistant implements approvable {    list<string> orders;    Volatile boss boss;    Public Assistant () {        orders = new linkedlist<> ();    }    public void AddOrder (String order) {        if (boss! = null) {            SYSTEM.OUT.PRINTLN ("Boss personally adds < + order +" > Tasks to the list ") ;            Boss.addorder (order);        } else {            System.out.println ("Assistant adds < + order +" > Task to List ");            This.orders.add (order);        }    }    @Override public    Void Approve () {        inviteboss ();        Boss.approve ();    }    private void Inviteboss () {        if (boss = = null) {            synchronized (this) {                if (boss = = null) {                    boss = new B OSS (orders);}}}}    
Main

For running, scene simulation

public class Main {public    static void Main (string[] args) {        //A lot of people come to the boss, the boss is busy, the assistant first put everything in place        Assistant Assistan t = new Assistant ();        Assistant.addorder ("I'm looking for boss interview");        Assistant.addorder ("I'm looking for the boss to borrow money");        Assistant.addorder ("I'm looking for boss chat");        Collected, the assistant's responsibility is completed, the boss called out, let boss processing. Or approve this matter, the Assistant is unable to do, can only call out boss to do.        Assistant.approve ();        Boss has just been invited to come, now on the scene. So you don't need an assistant to tell the boss. Everyone told the assistant, boss will also hear        assistant.addorder ("I look for boss to eat");        Assistant.addorder ("I'm looking for a boss to drink");        Assistant.approve ();    }}

Virtual proxy mode-virtual proxy (Java implementation)

Related Article

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.