大话设计模式_代理模式
卓贾易让戴励帮助自己追娇娇为场景,卓贾易不好意思直接送礼物给娇娇,让戴励代替自己送.
package com.wzs.design;/** * 大话设计模式--page62 代理模式 * * @author Administrator * */public class ProxyPattern {public static void main(String[] args) {SchoolGirl jiaojiao = new SchoolGirl("李娇娇");Pursuit pursuit = new Pursuit(jiaojiao, "卓贾易");Proxy proxy = new Proxy(pursuit);proxy.giveDolls();proxy.giveFlowers();proxy.giveChocolate();}}/* * 女孩 */class SchoolGirl {private String name;public SchoolGirl(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}/* * 送礼物接口 */interface IGiveGift {public abstract void giveDolls();public abstract void giveFlowers();public abstract void giveChocolate();}/* * 追求者 */class Pursuit implements IGiveGift {private SchoolGirl schoolGirl;private String PursuitName;public Pursuit(SchoolGirl schoolGirl, String pursuitName) {this.schoolGirl = schoolGirl;PursuitName = pursuitName;}@Overridepublic void giveChocolate() {System.out.println(PursuitName + " 送给 " + schoolGirl.getName() + " 巧克力.");}@Overridepublic void giveDolls() {System.out.println(PursuitName + " 送给 " + schoolGirl.getName() + " 洋娃娃.");}@Overridepublic void giveFlowers() {System.out.println(PursuitName + " 送给 " + schoolGirl.getName() + " 鲜花.");}}/* * 送礼物的代理 */class Proxy implements IGiveGift {Pursuit pursuit;public Proxy(Pursuit pursuit) {this.pursuit = pursuit;}@Overridepublic void giveChocolate() {pursuit.giveChocolate();}@Overridepublic void giveDolls() {pursuit.giveDolls();}@Overridepublic void giveFlowers() {pursuit.giveFlowers();}}