1. 靜態(tài)代理
步驟1
以12306售票為例,先定義一個(gè)售票接口
public interface Ticket {
public void sellTicket();//售票
}
步驟2
鐵道部自己售票
public class Railway implements Ticket {
@Override
public void sellTicket() {
System.out.println("銷售了一張火車票");
}
}
步驟3
代理點(diǎn)售票
public class PointProxy implements Ticket {
private Railway railway;
public PointProxy(Railway railway) {
super();
this.railway = railway;
}
@Override
public void sellTicket() {
System.out.println("火車票代理點(diǎn)準(zhǔn)備購(gòu)買火車票");
railway.sellTicket();// 票都是鐵道部出的票
System.out.println("購(gòu)票成功");
}
}
步驟4
測(cè)試
Ticket ticket = new PointProxy(new Railway());
ticket.sellTicket();
輸出
火車票代理點(diǎn)準(zhǔn)備購(gòu)買火車票
銷售了一張火車票
購(gòu)票成功