我嘗試在JDK、Android SDK和一些出名的庫中,尋找靜態(tài)代理的源碼,沒能找到。如果有讀者發(fā)現(xiàn),歡迎評論或者私信我。
靜態(tài)代理的實(shí)例
1. 售票代理
售票服務(wù)
public interface TicketService {
//售票
public void sellTicket();
//問詢
public void inquire();
//退票
public void withdraw();
}
站點(diǎn)售票文章來源:http://www.zghlxwxcb.cn/news/detail-668663.html
public class Station implements TicketService {
@Override
public void sellTicket() {
System.out.println("\n\t售票.....\n");
}
@Override
public void inquire() {
System.out.println("\n\t問詢。。。。\n");
}
@Override
public void withdraw() {
System.out.println("\n\t退票......\n");
}
}
代理網(wǎng)點(diǎn)售票文章來源地址http://www.zghlxwxcb.cn/news/detail-668663.html
public class StationProxy implements TicketService {
private Station station;
public StationProxy(Station station){
this.station = station;
}
@Override
public void sellTicket() {
// 1.做真正業(yè)務(wù)前,提示信息
this.showAlertInfo("××××您正在使用車票代售點(diǎn)進(jìn)行購票,每張票將會收取5元手續(xù)費(fèi)!××××");
// 2.調(diào)用真實(shí)業(yè)務(wù)邏輯
station.sellTicket();
// 3.后處理
this.takeHandlingFee();
this.showAlertInfo("××××歡迎您的光臨,再見!××××\n");
}
@Override
public void inquire() {
// 1.做真正業(yè)務(wù)前,提示信息
this.showAlertInfo("××××歡迎光臨本代售點(diǎn),問詢服務(wù)不會收取任何費(fèi)用,本問詢信息僅供參考,具體信息以車站真實(shí)數(shù)據(jù)為準(zhǔn)!××××");
// 2.調(diào)用真實(shí)邏輯
station.inquire();
// 3。后處理
this.showAlertInfo("××××歡迎您的光臨,再見!××××\n");
}
@Override
public void withdraw() {
// 1.真正業(yè)務(wù)前處理
this.showAlertInfo("××××歡迎光臨本代售點(diǎn),退票除了扣除票額的20%外,本代理處額外加收2元手續(xù)費(fèi)!××××");
// 2.調(diào)用真正業(yè)務(wù)邏輯
station.withdraw();
// 3.后處理
this.takeHandlingFee();
}
/*
* 展示額外信息
*/
private void showAlertInfo(String info) {
System.out.println(info);
}
/*
* 收取手續(xù)費(fèi)
*/
private void takeHandlingFee() {
System.out.println("收取手續(xù)費(fèi),打印發(fā)票。。。。。\n");
}
}
2. 明星代理
public interface IStar {
public abstract void sing(double money);
}
public class StarImpl implements IStar {
public void sing(double money) {
System.out.println("唱歌,收入" + money + "元");
}
}
//經(jīng)紀(jì)人
public class StarProxy implements IStar {
private StarImpl star = new StarImpl();
public void sing(double money) {
System.out.println("請先預(yù)約時間");
System.out.println("溝通出場費(fèi)用");
if (money < 100000) {
System.out.println("對不起,出場費(fèi)10w萬以內(nèi)不受理");
return;
}
System.out.println("經(jīng)紀(jì)人抽取了" + money * 0.2 + "元代理費(fèi)用");
star.sing(money * 0.8);
}
}
//測試
public class ProxyDemo {
public static void main(String[] args) {
StarProxy sg = new StarProxy();
sg.sing(200000);
}
}
到了這里,關(guān)于設(shè)計(jì)模式8:代理模式-靜態(tài)代理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!