Design pattern proxy - buy a ticket (original)

xiaoxiao2021-03-06  83

Design mode proxy - buy a ticket

This year, the New Year is good, playing cards to win 100 pieces, I am proudly laugh, I have a smirk, in short, one word "cool". Because I have never won in previous years! I am happy to return home, I haven't waited for me to report the battle, my wife gave me a sentence. "I have to go back to Shanghai, you give a ticket." Although I am not happy, my face is very happy. The look, immediately talked with a firm tone: "Please reassure, and ensure the completion of the task!". Guarantee is guaranteed, but the ticket of the New Year is really difficult to buy. After some setbacks (呜)), I have to find a ticket (Proxy).

Speaking of the word agent, everyone may not be unfamiliar, in fact, a lot of intermediary in society can also understand the agency, it is to help you do things, take the intermediate fee.

1. Here, first define this activity into an interface (Buyticket) class:

Public interface buyticket {

Public void Buyticket ();

}

2, below we have to achieve these two interfaces

A: Normal BuyTicket Class: Normalbuyticket

Public class normalbuyticket imports Buyticket {

Public void Buyticket () {

System.out.println ("Buy train ticket!");

}

}

B: BuxyBuyticket in the proxy situation:

// You must use a proxy object when you cannot directly access the NORMALBUYTICKET object.

Public class proxyBuyticket Implements Buyticket {

Private normalbuyticket normalbuyticket;

Public void Buyticket () {

IF (Normalbuyticket == NULL) {

NORMALBUYTICKET = New Normalbuyticket ();

}

NORMALBUYTICKET.BUYTICKET ();

GetMoney ();

}

Public void getMoney () {

System.out.println ("Get a proxy fee!");

}

}

3, write test classes:

Public class test {

Public static void main (string args []) {

Buyticket Buyticket = new proxybuyticket ();

Buyticket.buyticket ();

}

}

4, explanation:

A: Definition: Provide a proxy for other objects to control access to this object. That is to say, when we can't access the objects we want to access, you must access it through a proxy object.

B: In this example, I want to buy a ticket, but when I can't buy the ticket, I have to buy it through the ticket division. This truth should know.

5, postscript:

In the end, I got the ticket, I got the ticket to Shanghai, my wife was very happy, but I said that I was very capable, but I paid a 100 yuan agency fee, I should cry or laugh, but I want to: "Flower 100 Yuan let his wife praise me, saying that I can do it, not bad, you said! Hey.

转载请注明原文地址:https://www.9cbs.com/read-107875.html

New Post(0)