Proxy design mode:
For example, if you want to watch a movie, you need to buy a ticket ). In this way, we can find a proxy to buy tickets. 1. First define a Protocol @ protocol ticketdelegate <nsobject>
// Return the fare-(double) ticketprice; // The number of remaining tickets-(INT) leftticketsnumber;
@ End 2. The proxy complies with this Protocol @ interface agent: nsobject <ticketdelegate> @ end 3. In the person class, declare @ property to comply with the Protocol variables, then declare a method to buy a movie ticket @ interface person: nsobject
@ Property (nonatomic, strong)
ID <ticketdelegate>Delegate;
// Do not enter the proxy class here. The ID is used.// Buy movie tickets-(void) buyticket; @ end
4. Implementation of the person class @ implementation person
-(Void) buyticket {double price = [_ delegate ticketprice]; int number = [_ delegate leftticketsnumber]; nslog (@ "via proxy help, fare = % F, % d tickets remaining ", price, number) ;}@ end
Proxy design mode: to complete a task and define a protocol, as long as the class complies with this Protocol, it can be done. When a proxy is required, we use the universal pointer ID when declaring it, you do not need to determine which class to complete. You only need to assign the object of the class complying with the Protocol to the declared member variable.
Objective-C: 11_proxy Design Mode