"Getting Started with Java" Day10 Java proxy-static proxy

Source: Internet
Author: User

Today's introduction is a very important concept in Java--the agent.

What is an agent? Contact life to think about, the agent does not seem strange, the most image of the representative is a broker, the star generally has brokers, brokers as intermediaries, responsible for acting star related matters, for example, some people want to please star to sing performance, generally will not directly contact with the star, but contact his agent, his agent to arrange the trip , and the real singing performance is still the star himself, the broker only as an add-on exists.

In Java, the agent is the same concept, look at a chestnut:

First to create a Star class stars:

 Public classStarsImplementsistars{PrivateString name;  PublicStars (String name) { This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public voidSing () {System.out.println (GetName ()+ "sang a song."); }     Public voidDance () {System.out.println (GetName ()+ "a dance."); }}

This is the appropriate interface:

 Public Interface istars {    void  sing ();     void dance ();}

Now create a proxy class:

 Public classStarsproxyImplementsistars{//Save the received proxy object    PrivateIstars stars;  PublicStarsproxy (Istars stars) { This. Stars =stars; } @Override Public voidSing () {System.out.println ("I was the agent, and I received a singing request. ");        Stars.sing (); System.out.println ("Finish the song."); } @Override Public voidDance () {System.out.println ("I was the agent, and I received a dance request. ");        Stars.dance (); System.out.println ("Dance complete."); }}

To test:

 Public class Test {    publicstaticvoid  main (string[] args) {        // To Create a target object        New Stars ("Frank"); // proxy object, pass the target to the proxy object, establish the relationship        New Starsproxy (stars);        Starsproxy.sing ();        Starsproxy.dance ();    }}

Operation Result:

I am the agent, I received a singing request. Frank sang a song. I am the agent, I received the dance request. Frank danced a dance.

As we can see, the proxy class simply holds an instance of the Stars class, because the same interface is implemented, and the Starsproxy class must implement the methods of the Stars class that need to be represented, such as the dance and sing here, and this interface is the key to linking both. Because implementing an interface means that there must be a method declared in the interface.

So why use a proxy?

In fact, the main purpose is to expand the function of the original class, think about it, if that stars is not you write, but others write, now to the original sing or dance method to transform, such as the need to count the number of singing and dancing, the number of more than 10 does not carry out the operation of the direct return, At this time with the agent is very good implementation, to the proxy class slightly modified:

 Public classStarsproxyImplementsistars{//Save the received proxy object    PrivateIstars stars; //number of sing and dance saved    Private intnum;  PublicStarsproxy (Istars stars) { This. Stars =stars; } @Override Public voidSing () {if(!ifwork ()) {            return; } System.out.println ("I was the agent, and I received a singing request. ");        Stars.sing (); System.out.println ("Finish the song."); } @Override Public voidDance () {if(!ifwork ()) {            return; } System.out.println ("I was the agent, and I received a dance request. ");        Stars.dance (); System.out.println ("Dance complete."); }    /*** Whether to continue working *@returnis return true, otherwise returns false*/    Private Booleanifwork () {if(Num > 3) {System.out.println ("The star is already very tired today, come again tomorrow." "); return false; }Else{num++; return true; }    }}

Modify the test class:

 Public classTest { Public Static voidMain (string[] args) {//To Create a target objectStars stars =NewStars (); Stars.setname ("Frank"); //proxy object, pass the target to the proxy object, establish the relationshipStarsproxy Starsproxy =NewStarsproxy (stars);  for(inti = 0;i < 5; i++) {starsproxy.sing (); }    }}

The test results are as follows:

I am the agent, I received a singing request. Frank sang a song. I am the agent of singing, I received a request for singing. Frank sang a song. I am the agent of singing, I received a request for singing. Frank sang a song. I am the agent of singing, I received a request for singing. Frank sang a song. The star is already very tired today, come again tomorrow.

Look, simple and rude.

But in fact there is not much dry, here is only an agent of the idea, with this idea can be more convenient to modify the original class without directly under the premise of the original class method to expand.

But the restrictions are also obvious:

1. The proxy class needs to be implemented with the same interface as the proxy class, so that it can be transformed together to achieve polymorphism.

2. When the proxy class needs to be expanded, the management becomes more difficult, and then the modification of the proxy class requires that the proxy class be modified at the same time, increasing the cost of the modification.

So do not use in order to use, the application in the appropriate scene in order to play its true role.

At this point, this chapter is complete, welcome to continue to pay attention!

"Getting Started with Java" Day10 Java proxy-static proxy

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.