装饰者模式
Posted flgb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了装饰者模式相关的知识,希望对你有一定的参考价值。
一、煎饼案例
public class Battercake { protected String getMsg(){ return "煎饼"; } public int getPrice(){ return 5; } }
public class BattercakeWithEgg extends Battercake{ @Override protected String getMsg() { return super.getMsg() + "+1个鸡蛋"; } @Override //加一个鸡蛋加1块钱 public int getPrice() { return super.getPrice() + 1; } }
public class BattercakeWithEggAndSausage extends BattercakeWithEgg{ @Override protected String getMsg() { return super.getMsg() + "+1根香肠"; } @Override //加一个香肠加2块钱 public int getPrice() { return super.getPrice() + 2; } }
test
public class BattercakeTest { public static void main(String[] args) { Battercake battercake = new Battercake(); System.out.println(battercake.getMsg() + ",总价格:" + battercake.getPrice()); Battercake battercakeWithEgg = new BattercakeWithEgg(); System.out.println(battercakeWithEgg.getMsg() + ",总价格:" + battercakeWithEgg.getPrice()); Battercake battercakeWithEggAndSausage = new BattercakeWithEggAndSausage(); System.out.println(battercakeWithEggAndSausage.getMsg() + ",总价格:" + battercakeWithEggAndSausage.getPrice()); } }
二、改进
public class BaseBattercake extends Battercake { protected String getMsg(){ return "煎饼"; } public int getPrice(){ return 5; } }
public abstract class Battercake { protected abstract String getMsg(); protected abstract int getPrice(); }
public abstract class BattercakeDecorator extends Battercake { //静态代理,委派 private Battercake battercake; public BattercakeDecorator(Battercake battercake) { this.battercake = battercake; } protected abstract void doSomething(); @Override protected String getMsg() { return this.battercake.getMsg(); } @Override protected int getPrice() { return this.battercake.getPrice(); } }
public class EggDecorator extends BattercakeDecorator { public EggDecorator(Battercake battercake) { super(battercake); } protected void doSomething() { } @Override protected String getMsg() { return super.getMsg() + "+1个鸡蛋"; } @Override protected int getPrice() { return super.getPrice() + 1; } }
public class SausageDecorator extends BattercakeDecorator { public SausageDecorator(Battercake battercake) { super(battercake); } protected void doSomething() { } @Override protected String getMsg() { return super.getMsg() + "+1根香肠"; } @Override protected int getPrice() { return super.getPrice() + 2; } }
public class BattercakeTest { public static void main(String[] args) { Battercake battercake; //路边摊买一个煎饼 battercake = new BaseBattercake(); //煎饼有点小,想再加一个鸡蛋 battercake = new EggDecorator(battercake); //再加一个鸡蛋 // battercake = new EggDecorator(battercake); //很饿,再加根香肠 battercake = new SausageDecorator(battercake); battercake = new SausageDecorator(battercake); battercake = new SausageDecorator(battercake); battercake = new SausageDecorator(battercake); battercake = new SausageDecorator(battercake); //跟静态代理最大区别就是职责不同 //静态代理不一定要满足is-a的关系 //静态代理会做功能增强,同一个职责变得不一样 //装饰器更多考虑是扩展 System.out.println(battercake.getMsg() + ",总价:" + battercake.getPrice()); } }
三、登陆案例
public interface ISigninService { ResultMsg regist(String username, String password); /** * 登录的方法 * @param username * @param password * @return */ ResultMsg login(String username, String password); }
public class Member { private String username; private String password; private String mid; private String info; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMid() { return mid; } public void setMid(String mid) { this.mid = mid; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } }
public class ResultMsg { private int code; private String msg; private Object data; public ResultMsg(int code, String msg, Object data) { this.code = code; this.msg = msg; this.data = data; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
public class SigninService implements ISigninService { public ResultMsg regist(String username,String password){ return new ResultMsg(200,"注册成功",new Member()); } /** * 登录的方法 * @param username * @param password * @return */ public ResultMsg login(String username,String password){ return null; } }
改进
public interface ISiginForThirdService extends ISigninService { /** * QQ登录 * @param id * @return */ ResultMsg loginForQQ(String id); /** * 微信登录 * @param id * @return */ ResultMsg loginForWechat(String id); /** * 记住登录状态后自动登录 * @param token * @return */ ResultMsg loginForToken(String token); /** * 手机号登录 * @param telphone * @param code * @return */ ResultMsg loginForTelphone(String telphone, String code); /** * 注册后自动登录 * @param username * @param passport * @return */ ResultMsg loginForRegist(String username, String passport); }
public class SiginForThirdService implements ISiginForThirdService { private ISigninService signinService; public SiginForThirdService(ISigninService signinService) { this.signinService = signinService; } public ResultMsg regist(String username, String password) { return signinService.regist(username,password); } public ResultMsg login(String username, String password) { return signinService.login(username,password); } public ResultMsg loginForQQ(String id) { return null; } public ResultMsg loginForWechat(String id) { return null; } public ResultMsg loginForToken(String token) { return null; } public ResultMsg loginForTelphone(String telphone, String code) { return null; } public ResultMsg loginForRegist(String username, String passport) { return null; } }
test
public class DecoratorTest { public static void main(String[] args) { //满足一个is-a ISiginForThirdService siginForThirdService = new SiginForThirdService(new SigninService()); siginForThirdService.loginForQQ("sdfasfdasfsf"); } }
以上是关于装饰者模式的主要内容,如果未能解决你的问题,请参考以下文章