delegation mode

1. Definitions

It is not one of the 23 design patterns and is a commonly used pattern in object-oriented design patterns.
The principle of this pattern is that class B and class A are two classes that have nothing to do with each other, and B has exactly the same methods and property; and calling the method in B, the property is calling the method and property of the same name in A. B seems to be an intermediary authorized by A. The third-party code does not need to know the existence of A, nor does it need to have a direct connection with A. It can directly use the functions of A through B, so that it can not only use the various public functions of A, but also effectively integrate A. protected.

2. Examples

Let's take the customer request (Boss) project manager (leader) developer (Developer) as an example. Boss tells the project manager to make a system. After the project manager gets the order, he assigns the task to the developer to do it. Project managers do not do development work. Since each developer is good at different modules, the project manager will assign tasks to different developers.

IDeveloper

/**
 * 开发者都有收到命令做事情的能力
 * @author lijun
 * @since 2018-03-19 17:51
 */
public interface IDeveloper {
    /**
     *
     * @param command
     */
    public void doing(String command);
}

DeveloperA

/**
 * 开发者A
 * @author lijun
 * @since 2018-03-19 17:51
 */
public class DeveloperA implements IDeveloper {
    /**
     * @param command
     */
    @Override
    public void doing(String command) {
        System.out.println("我开发者A,我开始做:"+command);
    }
}

DeveloperB

/**
 *开发者B
 * @author lijun
 * @since 2018-03-19 17:51
 */
public class DeveloperB implements IDeveloper {
    /**
     * @param command
     */
    @Override
    public void doing(String command) {
        System.out.println("我开发者B,我开始做:"+command);
    }
}

Leader

/**
 * 项目经理
 * @author lijun
 * @since 2018-03-19 17:51
 */
public class Leader {

    /**
     * 员工集合
     */
    private Map<String,IDeveloper> targets = new HashMap<String,IDeveloper>();

    public Leader() {
        targets.put("login",new DeveloperA());
        targets.put("encrypt",new DeveloperB());
    }

    /**
     *项目经理不干活 委派给开发者
     * @param command 命令
     */
    public void  doing(String command){
        targets.get(command).doing(command);
    }
}

Boss

/**
 * Boss
 * @author lijun
 * @since 2018-03-19 17:51
 */
public class Boss {
    public static void main(String[] args) {
        new  Leader().doing("login");
        new  Leader().doing("encrypt");
    }
}

output

我开发者A,我开始做:login
我开发者B,我开始做:encrypt

Summary:
1. The core of delegation: distribution, scheduling and dispatch.
2. Delegation mode: It is a special combination of static proxy and strategy mode
3. The proxy mode focuses on the process, the delegation mode focuses on the result
4. The strategy mode focuses on extensibility (external expansion), and the delegation mode focuses on internal flexibility and reuse

Source address

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325641805&siteId=291194637