装饰器模式(Decorator)
为了某个实现类在不修改原始类的基础上进行动态地覆盖或者增加方法
采用--------装饰器模式
实现类要保持与原有类的层级关系
装饰器模式是一种特殊的适配器模式
拿适配器模式来改进
http://debuggg.com/article/255
示例代码:
ISigninService
package com.debuggg.test1.decorator;
import com.debuggg.test1.main5.ResultMsg;
public interface ISigninService {
    /**
     * 作者 ZYL
     * 功能描述 : 注册
     * 日期 2020-04-13 17:32
     * 参数 username
     * 参数 password
     * 返回值 com.debuggg.test1.main5.ResultMsg
     */
    public ResultMsg regist(String username, String password);
    /**
     * 作者 ZYL
     * 功能描述 : 登录
     * 日期 2020-04-13 17:32
     * 参数 username
     * 参数 password
     * 返回值 com.debuggg.test1.main5.ResultMsg
     */
    public ResultMsg login(String username, String password);
}
SigninService
package com.debuggg.test1.decorator;
import com.debuggg.test1.main5.ResultMsg;
public class SigninService implements ISigninService {
    @Override
    public ResultMsg regist(String username, String password) {
        return null;
    }
    @Override
    public ResultMsg login(String username, String password) {
        return null;
    }
}
ISigninForThirdService
package com.debuggg.test1.decorator;
import com.debuggg.test1.main5.ResultMsg;
public interface ISigninForThirdService extends ISigninService{
    public ResultMsg loginForQQ(String openId);
    public ResultMsg loginForWechat(String openId);
    public ResultMsg loginForToken(String token);
    public ResultMsg loginForTelephone(String phone, String code);
    public ResultMsg loginForRegist(String username, String password);
}
SigninForThirdService
package com.debuggg.test1.decorator;
import com.debuggg.test1.main5.ResultMsg;
public class SigninForThirdService implements ISigninForThirdService {
    ISigninService service;
    public SigninForThirdService(SigninService service){
        this.service = service;
    }
    @Override
    public ResultMsg loginForQQ(String openId) {
        return null;
    }
    @Override
    public ResultMsg loginForWechat(String openId) {
        return null;
    }
    @Override
    public ResultMsg loginForToken(String token) {
        return null;
    }
    @Override
    public ResultMsg loginForTelephone(String phone, String code) {
        return null;
    }
    @Override
    public ResultMsg loginForRegist(String username, String password) {
        return null;
    }
    @Override
    public ResultMsg regist(String username, String password) {
        return this.service.regist(username, password);
    }
    @Override
    public ResultMsg login(String username, String password) {
        return this.service.login(username, password);
    }
}
与适配器模式的对比
| 装饰器模式 | 适配器模式 | 
|---|---|
| 是一种特殊的适配器模式 | 可以不保留层级关系 | 
| 装饰者和被装饰者都要实现同一个接口主要目的是为了扩展,依旧保留OOP关系 | 适配者和被适配没有必然的层级关系 | 
| 满足is a | 满足has a | 
| 注重的是覆盖、扩展 | 注重的是覆盖、扩展 | 
更多内容请访问:IT源点
注意:本文归作者所有,未经作者允许,不得转载