The first milk tea shop
Milk tea is now so popular with the public that you can't miss such a good way to make money.
If you want to open a milk tea shop, what steps do you need? After some observation, you may need to buy equipment, raw materials, shops, labor and so on in the early stage.
Then we can use code to realize our first milk tea shop
- Define a milk tea shop interface
public interface IBubbleShop { // shop void shop(); // raw material void ingredient(); // staff void staff(); // equipment void facility(); // Normal business void normalBusiness(); }
- Milk tea shop implementation class
public class BubbleShop implements IBubbleShop { // Name of milk tea shop private String name; public BubbleShop(String name){ this.name = name; } @Override public void shop() { System.out.println("Rent a shop!"); } @Override public void ingredient() { System.out.println("Buy raw materials!"); } @Override public void staff() { System.out.println("Hire employees!"); } @Override public void facility() { System.out.println("Buy equipment!"); } @Override public void normalBusiness() { System.out.println("The second lever milk tea point is open"); System.out.println("Sell a cup of milk tea!"); } }
- Opening
public class BubbleShopMain { public static void main(String[] args) { // Define a milk tea shop called two levers IBubbleShop bubbleShop = new BubbleShop("Two bars"); // preparation in advance bubbleShop.shop(); bubbleShop.facility(); bubbleShop.ingredient(); bubbleShop.staff(); // Start business bubbleShop.normalBusiness(); } } /** * Operation results: * * Rent a shop! * Buy equipment! * Buy raw materials! * Hire employees! * The second lever milk tea point is open * Sell a cup of milk tea! */
Through the above steps, our "two bars" milk tea shop has finally opened!
After a year's hard work, our milk tea shop is on fire. Then we are definitely not satisfied with the benefits brought by a store. If we want to be bigger, we should open the brand franchise mode.
Brand joining
Due to the reputation of "two bars" milk tea shop, many businesses want to join our brand. In the early stage, in order to promote it, we don't set the joining conditions and directly let everyone use fame. After all, leeks have to be raised first before they can be cut.
- Define an agent merchant
public class BubbleShopProxy implements IBubbleShop { private IBubbleShop iBubbleShop; public BubbleShopProxy(IBubbleShop iBubbleShop){ this.iBubbleShop = iBubbleShop; } @Override public void shop() { this.iBubbleShop.shop(); } @Override public void ingredient() { this.iBubbleShop.ingredient(); } @Override public void staff() { this.iBubbleShop.staff(); } @Override public void facility() { this.iBubbleShop.facility(); } @Override public void normalBusiness() { System.out.println("Franchise store ready"); this.iBubbleShop.normalBusiness(); } }
- "Two bars" franchise store opens
public class BubbleShopMain { public static void main(String[] args) { IBubbleShop erganggang = new BubbleShop("Two bars"); IBubbleShop proxy = new BubbleShopProxy(erganggang); // Preliminary preparation of franchise stores proxy.shop(); proxy.facility(); proxy.ingredient(); proxy.staff(); // Franchise stores open proxy.normalBusiness(); } } /** * Operation results: * * Rent a shop! * Buy equipment! * Buy raw materials! * Hire employees! * Franchise store ready * The second lever milk tea point is open * Sell a cup of milk tea! */
The above code enables us to join the business.
After another year, "two bars" milk tea shop became more popular. More merchants have joined in. Then we began to put forward our joining conditions. We need to pay a 10w joining fee. When each merchant sells a cup of milk tea, we will draw 2 yuan (is it very capitalist).
- For joining conditions, we add an interface
public interface ProxyCondition { // Franchise fee void initalFee(); // take a percentage void cut(); }
- After the release of the new joining conditions, new businesses have joined.
Let's implement a class of new franchisees
public class BubbleShopProxy implements IBubbleShop, ProxyCondition { private IBubbleShop iBubbleShop; public BubbleShopProxy(IBubbleShop iBubbleShop){ this.iBubbleShop = iBubbleShop; } @Override public void shop() { this.initalFee(); this.iBubbleShop.shop(); } @Override public void ingredient() { this.iBubbleShop.ingredient(); } @Override public void staff() { this.iBubbleShop.staff(); } @Override public void facility() { this.iBubbleShop.facility(); } @Override public void normalBusiness() { System.out.println("Franchise store ready"); this.iBubbleShop.normalBusiness(); this.cut(); } @Override public void initalFee() { System.out.println("Pay 10 w Franchise fee"); } @Override public void cut() { System.out.println("A cup of milk tea costs 2 yuan"); } } /** * Main Method does not need to be changed. * Operation results: * * Pay 10w franchise fee * Rent a shop! * Buy equipment! * Buy raw materials! * Hire employees! * Franchise store ready * The second lever milk tea point is open * Sell a cup of milk tea! * A cup of milk tea costs 2 yuan */
After the new joining conditions are released. Our earnings have increased again.
Two bars milk tea shop company
Because the "two bars" milk tea shop is too hot, many people join in. So we need to set up a company that specializes in franchise affairs.
Then we introduce a concept, dynamic agent. Dynamic proxy refers to the dynamic generation of proxy classes through the interface of the proxy.
At present, there are two ways for JDK to implement dynamic agent. JDK based dynamic agent and CGILB based dynamic agent.
The following takes the dynamic agent of JDK as an example. The implementation of JDK dynamic agent mainly includes two steps
- Create a class that implements the InvocationHandler interface and must implement the invoke method.
- Via proxy Newproxyinstance initializes a proxy, and finally calls specific methods through the proxy.
Setting up a company is like realizing dynamic agency. Next, go back to our milk tea shop.
- Create our "two lever" company
public class BubbleShopIH implements InvocationHandler { // Proxy instance Object obj; public BubbleShopIH(Object obj) { this.obj = obj; } // Call the proxied method @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("shop")) { System.out.println("Pay 10 w Franchise fee"); } if (method.getName().equals("normalBusiness")) { System.out.println("Franchise store ready"); } Object result = method.invoke(this.obj, args); if (method.getName().equals("normalBusiness")) { System.out.println("A cup of milk tea costs 2 yuan"); } return result; } }
- New Merchants join through the company
public class BubbleShopIHMain { public static void main(String[] args) { IBubbleShop bubbleShop = new BubbleShop("Two bars"); InvocationHandler handler = new BubbleShopIH(bubbleShop); // Dynamically generate an agent IBubbleShop proxy = (IBubbleShop) Proxy.newProxyInstance(bubbleShop.getClass().getClassLoader(), new Class[]{IBubbleShop.class}, handler); proxy.shop(); proxy.facility(); proxy.ingredient(); proxy.staff(); proxy.normalBusiness(); } } /** * Operation results: * * Pay 10w franchise fee * Rent a shop! * Buy equipment! * Buy raw materials! * Hire employees! * Franchise store ready * The second bar milk tea shop is open * Sell a cup of milk tea! * A cup of milk tea costs 2 yuan */
So far, we have established our own company with a special process (invoke method of agent class), and our milk tea Dynasty has been established.
summary
1. There are two ways of agency: static agency and dynamic Agency (static agency before the establishment of the company)
2. Agent mode is a widely used mode. The implementation principle of Spring AOP is the proxy mode.
I hope the way of milk tea shop can give you an understanding idea. If you think the article is good, you can praise it. Your support is my driving force.