JAVA设计模式-装饰器模式(Decorator)

装饰器模式(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
注重的是覆盖、扩展注重的是覆盖、扩展
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值