设计模式-委派模式

委派模式不属于23种设计模式,在Spring中有许多运用,如Spring的子项目Spring MVC中的前端控制器DispatcherServlet就是委派模式的运用。

下面就以老板(Boss),领导(Leader),员工(Employee)为例。

/**
 * @description:老板
 */
public class Boss {

    public void command(String command,Leader leader) {
        leader.doing(command);
    }
}
/**
 * @description:leader
 */
public class Leader {

    private Map<String,IEmployee> feature = new HashMap<>();

    public Leader(){
        feature.put("TaskA",new EmployeeA());
        feature.put("TaskB",new EmployeeB());
    }

    public void doing(String command) {
        feature.get(command).doing(command);
    }
}
public interface IEmployee {

    void doing(String command);
}
``

```java
/**
 * @description:EmployeeA
 */
public class EmployeeA implements IEmployee{
    @Override
    public void doing(String command) {
        System.out.println("EmployeeA doing " + command);
    }
}

```java
/**
 * @description:EmployeeB
 */
public class EmployeeB implements IEmployee {
    @Override
    public void doing(String command) {
        System.out.println("EmployeeB doing " + command);
    }
}
/**
 * @author :fenghua.gu
 * @date :Created in 2019/10/9 9:38
 * @description:DelegateTest
 */
public class DelegateTest {

    public static void main(String[] args) {
        Boss boss = new Boss();
        boss.command("TaskA",new Leader());
        boss.command("TaskB",new Leader());
    }
}

测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值