首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

狂言设计模式_代理模式

2013-03-12 
大话设计模式_代理模式卓贾易让戴励帮助自己追娇娇为场景,卓贾易不好意思直接送物给娇娇,让戴励代替自己送

大话设计模式_代理模式

卓贾易让戴励帮助自己追娇娇为场景,卓贾易不好意思直接送礼物给娇娇,让戴励代替自己送.

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();}}


1楼adam_zs昨天 11:54
[code=java]n输出结果:n卓贾易 送给 李娇娇 洋娃娃.n卓贾易 送给 李娇娇 鲜花.n卓贾易 送给 李娇娇 巧克力.n[/code]

热点排行