java design patterns ---- Decorator Pattern, the wording of the second third-party login

Decorator Pattern (Decorator Pattern)

It refers without changing the original object, with the additional functionality to an object, providing more flexible than inherited alternatives (expansion of the original object function)

Structure pattern belongs

Applicable scene:

1. extended function for a class or a class to add additional functions.

2. Dynamic of an object to add features that dynamically revoked.

Reality scene:

We often eat fried not have flavor, plus meat, and eggs, but also all I want, we immediately think of the code.

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 原味炒粉
 * @E-Mail : [email protected]
 * @Date : Created in 15:55 2020-3-14
 */
public class Noodles {

    protected String getMsg(){
        return "炒粉";
    }

    protected int getPrice(){
        return 6;
    }

}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 15:59 2020-3-14
 */
public class NoddlesWithEggs extends Noodles {
    @Override
    protected String getMsg() {
        return super.getMsg()+",加一个鸡蛋";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+1;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 16:01 2020-3-14
 */
public class NoodlesWithEggsAndMeat extends NoddlesWithEggs{
    @Override
    protected String getMsg() {
        return super.getMsg()+",加点猪肉";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+2;
    }
}

Test run:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 16:02 2020-3-14
 */
public class client {
    public static void main(String[] args) {
        //来份原味炒粉
        Noodles noodles = new Noodles();
        System.out.println(noodles.getMsg() + "价格:"+noodles.getPrice());
        //来份鸡蛋炒粉
        NoddlesWithEggs noddlesWithEggs = new NoddlesWithEggs();
        System.out.println(noddlesWithEggs.getMsg() + "价格:"+noddlesWithEggs.getPrice());
        //我全都要,来份加肉加蛋的
        NoodlesWithEggsAndMeat noodlesWithEggsAndMeat = new NoodlesWithEggsAndMeat();
        System.out.println(noodlesWithEggsAndMeat.getMsg() + "价格:"+noodlesWithEggsAndMeat.getPrice());
    }
}

 

But some people would say, I want to add two eggs, how to do it, obviously, it is impossible to classes noddlesWithDoubleEgg, right, so we continue to optimize. This time the decorator pattern on debut.

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 此时炒粉就变成了一个抽象的炒粉了
 * @E-Mail : [email protected]
 * @Date : Created in 16:13 2020-3-14
 */
public abstract class Noodles {

    //信息
    protected abstract String getMsg();

    //价格
    protected abstract int getPrice();
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  炒粉装饰器
 * @E-Mail : [email protected]
 * @Date : Created in 16:16 2020-3-14
 */
public abstract class NoodlesDecorator extends Noodles{

    //又像静态代理
    private Noodles noodles;

    public NoodlesDecorator(Noodles noodles){
        this.noodles = noodles;
    }

    @Override
    protected String getMsg() {
        return this.noodles.getMsg();
    }

    @Override
    protected int getPrice() {
        return this.noodles.getPrice();
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 基础套餐,原味炒粉
 * @E-Mail : [email protected]
 * @Date : Created in 16:15 2020-3-14
 */
public class BaseNoodles extends Noodles{
    @Override
    protected String getMsg() {
       return  "原味炒粉";
    }

    @Override
    protected int getPrice() {
        return 6;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 加鸡蛋
 * @E-Mail : [email protected]
 * @Date : Created in 16:21 2020-3-14
 */
public class EggsDecorator extends NoodlesDecorator{

    public EggsDecorator(Noodles noodles) {
        super(noodles);
    }

    @Override
    protected String getMsg() {
        return super.getMsg()+ "+1个鸡蛋";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+1;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 加肉
 * @E-Mail : [email protected]
 * @Date : Created in 16:23 2020-3-14
 */
public class MeatDecorator extends NoodlesDecorator{

    public MeatDecorator(Noodles noodles) {
        super(noodles);
    }

    @Override
    protected String getMsg() {
        return super.getMsg() + "+1份肉";
    }

    @Override
    protected int getPrice() {
        return super.getPrice()+2;
    }
}

Test run it:

 

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 16:25 2020-3-14
 */
public class DecoratorTest {
    public static void main(String[] args) {

        Noodles noodles;
        //路边买了个原味炒粉
        noodles = new BaseNoodles();
        //不爽啊,来个蛋
        noodles = new EggsDecorator(noodles);
        //还是不行啊,我全都要,再加份肉
        noodles = new MeatDecorator(noodles);
        //还是不行,真男人,我要双黄蛋,再来一个蛋
        noodles = new EggsDecorator(noodles);
        System.out.println(noodles.getMsg() + "价格:"+noodles.getPrice());
    }

 This is the function of the Decorator Pattern, and would like to add a few to add a few eggs, you can continue to add sausage, no problem, you do not need to go to change the original code.

Well, the above is an example of that reality, and now we have to give an example of the code used in third-party login form II.

 The initial landing approach:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 登陆接口
 * @E-Mail : [email protected]
 * @Date : Created in 16:44 2020-3-14
 */
public interface LoginService {

    public MsgResult login(String username, String password);

    public MsgResult register(String username,String password);
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  相当于系统已经写好的代码,不去动它
 * @E-Mail : [email protected]
 * @Date : Created in 13:38 2020-3-14
 */
public class LoginServiceImpl implements LoginService{

    //用户名,密码登陆方法
    @Override
    public MsgResult login(String username, String password){
        System.out.println("用户pc登陆");
        return null;
    }

    //注册方法
    @Override
    public MsgResult register(String username,String password){
        return login(username,password);
    }
}

/**
 * @Author Darker
 * @Descrption
 * @Date : Created in 16:07 2020-3-12
 */
@Data
@AllArgsConstructor
public class MsgResult {

    private int code;
    private Object data;
    private String msg;

    public static MsgResult success(Object data){
        return new MsgResult(200,data,"成功");
    }

    public static MsgResult fail(Object data){
        return new MsgResult(404,data,"失败");
    }

}

/**
 * @Author Darker
 * @Descrption
 * @Date : Created in 10:57 2020-3-13
 */
@Data
public class User {
    private String id;

    private String usename;

    private String password;

    private int age;

    private String addr;
}

Coupled with third-party landed, the decorator strengthened to achieve, do not move the original code:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 第三方登陆接口,直接继承登陆接口
 * @E-Mail : [email protected]
 * @Date : Created in 16:48 2020-3-14
 */
public abstract class ThirdLoginServiceDecorator implements LoginService {

    //qq登陆留给子类实现
    protected MsgResult loginQQ(String id){
        return null;
    }

    //微信登陆留给子类实现
    protected MsgResult loginWechat(String id){
        return null;
    }
}

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption 实现原有的基础套餐
 * @E-Mail : [email protected]
 * @Date : Created in 16:50 2020-3-14
 */
public class ThirdLoginServiceImpl extends ThirdLoginServiceDecorator {

    private LoginService loginService;

    public ThirdLoginServiceImpl(LoginService loginService){
        this.loginService = loginService;
    }

    @Override
    public MsgResult login(String username, String password) {
        return loginService.login(username,password);
    }

    @Override
    public MsgResult register(String username, String password) {
        return loginService.register(username,password);
    }
}


/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption  qq登陆装饰器
 * @E-Mail : [email protected]
 * @Date : Created in 16:57 2020-3-14
 */
public class ThirdLoginQQDecorator extends ThirdLoginServiceDecorator {

    private ThirdLoginServiceDecorator thirdLoginServiceDecorator;

    public ThirdLoginQQDecorator(ThirdLoginServiceDecorator thirdLoginServiceDecorator){
        this.thirdLoginServiceDecorator = thirdLoginServiceDecorator;
    }

    @Override
    public MsgResult loginQQ(String id){
        System.out.println("qq登陆");
        return null;
    }

    @Override
    public MsgResult login(String username, String password) {
        return thirdLoginServiceDecorator.login(username,password);
    }

    @Override
    public MsgResult register(String username, String password) {
        return thirdLoginServiceDecorator.register(username,password);
    }
}

 have a test:

/**
 * @Author Darker
 * @Note 我心净处,何处不是西天。
 * @Descrption
 * @E-Mail : [email protected]
 * @Date : Created in 17:00 2020-3-14
 */
public class updateLoginTest {
    public static void main(String[] args) {

        ThirdLoginServiceDecorator thirdLoginService = new ThirdLoginServiceImpl(new LoginServiceImpl());
        thirdLoginService = new ThirdLoginQQDecorator(thirdLoginService);
        thirdLoginService.loginQQ("4444");
        thirdLoginService.login("xiaoming","123456");

    }
}

I feel like adapter model is not ah, in fact, the code looks like, but it is important that the two modes of thought, the difference between what we do here.

We take a look at the jdk use the most classic decorator pattern, io, io found no new stream is to deliver a level of (io which has a classic summary sets vats kegs).

 

 

The above injection subclass abstract class, parent class to get some of the things that sets vat full compliance with kegs.

to sum up:

advantage:

1. decorator inherited beneficial supplement flexible than inheritance, does not change the dynamic of the original object to an object under extension, plug and play.

2. The different effects may be achieved by using different combinations and permutations of these decorative and the decorative.

3. decorator fully comply with the principle of opening and closing.

Disadvantages:

1. Add more classes, increasing the complexity of the program.

2. dynamic decoration, more complex multilayer decorative.

 

Published 27 original articles · won praise 1 · views 3638

Guess you like

Origin blog.csdn.net/qq_40111437/article/details/104861677