【Example of proxy Mode Application Scenario]
For example, if the player's money reaches a certain amount, he can go to the car store to buy a racing car with higher performance, then the "car store" that sells cars is a typical "agent" of a typical "car manufacturer". He provides the "service for car manufacturers" to people in need. From the aspect of object-oriented consideration, the "agent for selling cars" is also an object, so this object also has a certain state, in software projects, this object also has the basic function of managing financial invoicing. In the design, we should consider the class structure of software with the idea of OOP programming, this car sales agent is also a class.
[Proxy mode explanation]
Type: Structure Mode
Provides proxies for some objects to restrict those objects to access other objects.
【Proxy mode-JAVA code implementation]
Create an API to buy a car:
public interface buy_car { public void buy_mycar();}
Create a new people human with the behavior of buying a car, so the implementation interface buy_car
Class people implements buy_car {private int cash; private String vip; private String username; @ Override public void buy_mycar () {// TODO Auto-generated method stub System. out. print (username + "is a vip customer. You can buy a new car directly! ");} Public int getCash () {return cash;} public void setCash (int cash) {this. cash = cash;} public String getUsername () {return username;} public void setUsername (String username) {this. username = username;} public String getVip () {return vip;} public void setVip (String vip) {this. vip = vip ;}}
The people class cannot own a car. It must be authenticated by the proxy class. Only after the condition is met can the car be owned. A new proxy is created to check whether the current people is eligible to buy a car:
Class proxyclass implements buy_car {private people; public people getPeople () {return people;} public void setPeople (people) {this. people = people;} @ Override public void buy_mycar () {// TODO Auto-generated method stub if (people. getVip () = "vip") {people. buy_mycar (); return;} if (people. getCash () >=50000) {System. out. println (people. getUsername () + "bought a new car and the transaction is over! ");} Else {System. out. println (people. getUsername () +" there is not enough money to buy a car. Continue! ");}}}
Finally, create a client to simulate the behavior of buying a car:
public class run_main { public static void main(String[] args) { // TODO Auto-generated method stub people people_1 =new people(); people_1.setCash(60000); people_1.setUsername("jeck"); people people_2 =new people(); people_2.setCash(40000); people_2.setUsername("rose"); people people_3 =new people(); people_3.setCash(0); people_3.setUsername("tom"); people_3.setVip("vip"); proxyclass proxy_buy = new proxyclass(); proxy_buy.setPeople(people_1); proxy_buy.buy_mycar(); proxy_buy.setPeople(people_2); proxy_buy.buy_mycar(); proxy_buy.setPeople(people_3); proxy_buy.buy_mycar(); }}
The program running result is as follows:
Jeck bought a new car and the transaction is over! Rose has not enough money to buy a car. Continue the competition! Tom is a vip customer and can buy a new car directly!
The proxy mode mainly uses java polymorphism, working on the proxy class, and the proxy class is mainly engaged. You let me work, well, I will give it to the class behind the scenes, you will be satisfied with it, how can I know whether the agent can do this? The same root is the same interface.
Author: jason0539
Weibo: http://weibo.com/2553717707
Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)