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

DECORATOR设计方式【为了某某而写】

2013-09-05 
DECORATOR设计模式【为了某某而写】喜欢听故事吗?我想很多人都是喜欢听故事的,尽管有很多故事都一样,但是大

DECORATOR设计模式【为了某某而写】

喜欢听故事吗?我想很多人都是喜欢听故事的,尽管有很多故事都一样,但是大家都不约而同地去追求同样的故事结尾:美好的永远长存。

其实,我也一样。

这里的代码,故事的五分之四是真的,还有五分之一,我不知道。

package patterns.structures.decorator;/** * 在大学里面,我默默的喜欢过一个女生,她和其她那些女生比较起来,唯一的不同就是我只喜欢她一个人。 * 毕业那天,我喝多了,对她说:"等我,三年内我会回重庆来找你的!" * 她没有拒绝,也没有答应。我知道我这三年内要做的事情很多。 * 毕业了,我也知道,以后可能很难见到她,但是我不会放弃。 * ... * 经过努力,小有成果,我回去找她,用积蓄买了一间不大的房子,房子需要装饰一下。  *   * @author one * */public interface House {//我只想让房子更美void makeRoomBeautiful();}


 

package patterns.structures.decorator;/** * 家,其实很简单。 * 就拿这窗户来说吧。 *  * @author one * */public class Windows implements House {//窗已经有了框架和玻璃,都很干净。@Overridepublic void makeRoomBeautiful() {System.out.println("Windows has installed framework and glasses.");} }


 

package patterns.structures.decorator;/** * 女生天生爱美,当然男生也一样。 * 不管是什么,都想到去装饰一番。 * 她和我想的一样,也想装饰一下这个家。 * @author one * */public class Decorator implements House {//家,是我和她私有的,不允许有第三者进入我们的小世界。private House house;Decorator(House house){this.house=house;}@Overridepublic void makeRoomBeautiful() {house.makeRoomBeautiful();//我们装饰家的目标都一样:"让家,更漂亮。"} }
package patterns.structures.decorator;/** * 她喜欢花,就想在窗台上摆了一盆百合 * @author one * */public class FlowerDecorator extends Decorator {FlowerDecorator(House house) {super(house);}@Overridepublic void makeRoomBeautiful() {super.makeRoomBeautiful();this.putFlower();} private void putFlower(){System.out.println("put a basin of lily");} }


 

package patterns.structures.decorator;/** * 我想按上青绿色的窗帘 * @author one * */public class CurtainDecorator extends Decorator {CurtainDecorator(House house) {super(house);} @Overridepublic void makeRoomBeautiful() {super.makeRoomBeautiful();this.installedCurtain();} private void installedCurtain(){System.out.println("Installed a dark green curtain");}}


好了看看:

package patterns.structures.decorator;public class Test {/** * @param args */public static void main(String[] args) {House house=new FlowerDecorator(new CurtainDecorator(new Windows()));house.makeRoomBeautiful();}}


得出的效果是:

Windows has installed framework and glasses.Installed a dark green curtainput a basin of lily



 

热点排行