Novice Java design mode-from buying a house to see proxy mode

Source: Internet
Author: User

I learned the proxy mode today.

Compared to adapter specific adapter mode, or adorner mode, the proxy mode is easier to understand.

Agent This word should be better understood, instead of to do is agent.

For example, we buy and sell houses, then we will find intermediaries, I want to sell the house, but we do not have time to sell, I volunteered to the intermediary, intermediary will help me to publish, will help me find buyers, with buyers to see the house, finally I just receive money on the line.

Buyer's Purpose: sell the house to get the money. The ultimate goal of selling is to replace the house with money.

Intermediary work: The collection of real estate information, publishing property information, such as buyers call, with buyers to see the room, buyers fancy, pay the intermediary, intermediary transfer procedures, transfer success, draw off their commission, pay the seller.

The purpose of the seller, to give money, to take the house


The sellers may be busy and have no time to do so many things at all.

Let's switch to work examples ....

At the database level, he is only responsible for receiving the requested parameters and returning the data. It does not really care about how the data is handled, what format is returned to the presentation layer, but the presentation layer does not care.

Just as the seller does not care who comes to see the house, who will give me the money to pass this parameter, I give to who return a house.


And the front-end display layer is responsible for transmitting parameters, he does not care who the seller is, give money will get the house.


So in the middle of those who do the proof of the process of the House to do, of course, by real estate agents to do.

Now, let's make the whole process of buying a house.

In order not to understand the adapter pattern and adorner pattern as abstract as before, we introduce parameters and return values this time.


Set up an interface to sell the house first.

Public interface Sell {public String sellhoues (int money);}

Come back to the house.

public class House {private string Househost;public House (String househost) {This.househost = househost;//constructor method The name of the incoming householder} Public String Gethousehost () {return househost;} public void Sethousehost (String househost) {this.househost = Househost;}}


Then, we define a seller, this seller is no money, he only house ... He needs to sell the house now, so the interface of selling the house is realized. For the seller, he called the sale of the house is who give me money, I give the house, I do not care about the property transfer ah these things.

public class Seller implements Sell {private String name;//seller's name private house house;//seller's home private int money;//seller's Money Seller (String name) {this.name = Name;house = new house (name);//Instantiate a building and pass in the name of the owner}//the way to sell the house, give me the money, the house is your public home sellhoues (int money) { This.money = This.money+money; SYSTEM.OUT.PRINTLN ("Seller receives money"); return this.house;}}


Then there was the buyer, the buyer had money and no house, so he had 2 ways to pay and take the house.

public class Buyer {private int money;private house House;public Buyer () {//Buyer only money without houses this.money= 9999;//9999 yuan Money, Estimated to be able to buy 1 square string house= "";//no House}public void Givemoney (int topay) {this.money=this.money-topay;// Buyer's money minus price System.out.println ("buyer pays");} public void Gethouse [house house] {this.house = house; SYSTEM.OUT.PRINTLN ("buyer gets the House");} public int Getmoney () {return money;} Public house Gethouse () {return house;}}



If you do not use the proxy mode, we want to let buyers direct and sellers, sellers of the sale of the house definitely need to rewrite, this is certainly the sellers do not want, this is a waste of sellers great time. As in our project, if a DAO class is used for database operation, some users may only provide him with the permission to query, do not provide modify permissions, this time with the proxy mode, with a proxy class proxy to operate this DAO object, for different users to provide different proxy classes, This does not need to modify the original code, but also to meet the requirements of the business.


Below, we use an intermediary to directly connect with the seller.

public class Proxy implements Sell  {Seller seller;public proxy () {This.seller = new Seller ("seller");// Intermediary in the presence of the time should have a customer, and this customer has a house, or his appearance does not have any meaning}//sell the method of the House, this method mainly serve the sellers, to sellers money, sellers to you house, but relative to the seller @overridepublic houses sellhoues (int money) {//TODO auto-generated method Stubhouse houses = seller.sellhoues (money);//intermediaries get the house from the buyer. Start the transfer process System.out.println ("paid to the seller, intermediary to get the house"), House.sethousehost ("buyer");//Transfer, the name of the house to the buyer's System.out.println (" has succeeded "); return house;}}

Finally, test it to see if the entire buying process is successful.

public class Test {public static void main (string[] args) {Sell proxy =new proxy (); Buyer Buyer = new Buyer (); Buyer.givemoney (8888); House House = proxy.sellhoues (8888), Buyer.gethouse (house); SYSTEM.OUT.PRINTLN ("Buyer surplus money:" +buyer.getmoney ()); System.out.println ("Homeowner's name:" +buyer.gethouse (). Gethousehost ());}}
Output Result:



Okay, it's the most annoying outfit. B Summary moment: The most notable feature of proxy mode is that the object is already held in the proxy class, so in the external view, you do not need to care about the target class, the target class has never appeared, the biggest advantage is that let others see you want him to see, it is so simple. Agent mode or adhering to the principle of design, internal closure of the changes, opening up the idea of expansion.


In fact, when I finished writing these, I was suddenly confused with the proxy mode, the adorner mode, and the adapter mode again. So, I am going to write an article to analyze the similarities and differences of the 3 models, so that they can understand the patterns.

Novice Java design mode-from buying a house to see proxy mode

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.