ProxyMain.java 1.2 KB
Newer Older
C
chenjianqiang 已提交
1 2 3 4 5 6 7 8
package com.pattern.ProxyPattern;

import com.pattern.ProxyPattern.materials.po.BeijingStationImpl;
import com.pattern.ProxyPattern.materials.po.TicketService;
import com.pattern.ProxyPattern.materials.po.YellowBullHandler;
import com.pattern.ProxyPattern.materials.po.YellowBullShopImpl;

/**
C
OUT  
chenjianqiang 已提交
9
 * 代理模式
C
chenjianqiang 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 * @author lx
 * @date 2021/11/8 13:22
 **/
public class ProxyMain {

    /**
     * 静态代理
     */
    public void staticMain() {
        // 我要买票 --- 去北京站售票处
        BeijingStationImpl beijingStation = new BeijingStationImpl();
        beijingStation.consultation("1+1=?");

        // 我要买票 --- 去黄牛处
        YellowBullShopImpl yellowBullShop = new YellowBullShopImpl(beijingStation);
        yellowBullShop.consultation("2 + 2 = ?");
    }


    /**
     * 动态代理
     */
    public void reflectMain() {
        // 我要买票 --- 去北京站售票处
        BeijingStationImpl beijingStation = new BeijingStationImpl();


        YellowBullHandler handler = new YellowBullHandler();
        TicketService subject = (TicketService) handler.newProxyInstance(beijingStation);
        subject.consultation("你们卖票的么?");
C
chenjianqiang 已提交
40
        subject.sellT();
C
chenjianqiang 已提交
41 42
    }
}