设计模式之委派模式

委派模式

 

总体来说设计模式分为三大类:

创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。

虽然说委派模式不属于Gof23中设计模式,但这并不影响它成为一种经典的设计模式。

“委派”字面意思就是指派一件事情给某人。类比到生活中的场景,比如项目leader指派开发任务给下面的猿猿们。这听起来有点像静态代理,不过还是不一样的,你品,你细品!代理强调的是过程,主要是要在代理过程中加入一些动作的,而委派主要是分配和分发。

代码实现

我们先新建一个业务处理接口BusinessService

public interface BusinessService {
    void doService();
}

新建两个实现类LoginService和OrderService

public class LoginService implements BusinessService{
    public void doService() {
        System.out.println("处理登录相关业务");
    }
}

public class OrderService implements BusinessService{
    public void doService() {
        System.out.println("订单业务模块");
    }
}

新建一个枚举类,表示不同的业务类型,这里假设就两个,login和order

public enum ServerType {
    LOGIN,ORDER;
}

新建一个业务查找类,主要是用于根据不用的业务类型选择不同的业务组件提供服务。

public class BussinessLookup {
    private OrderService orderService;

    private LoginService loginService;

    /**
     * 查找对应的服务
     * @param serverType
     * @return
     */
    public BusinessService getBusinessService(ServerType serverType){
        if(serverType.equals(ServerType.LOGIN)){
            return loginService;
        }else{
            return orderService;
        }
    }

    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
}

接下来,关键的类来了,委派类。做的工作主要是分发。

public class BusinessDelegate {
    private BussinessLookup bussinessLookup;

    private BusinessService businessService;

    private ServerType serverType;

    public void setBussinessLookup(BussinessLookup bussinessLookup) {
        this.bussinessLookup = bussinessLookup;
    }

    public void setServerType(ServerType serverType) {
        this.serverType = serverType;
    }

    /**
     * 委派方法,其实最终调用的是业务类的方法
     */
    public void doTask(){
        businessService = bussinessLookup.getBusinessService(serverType);
        businessService.doService();
    }
}

我们再来个客户端类,也就是请求类,它通过委派类完成工作。就是说我不需要知道在幕后到底是哪个业务组件在处理,我只需要给你请求,你帮我完成好任务就OK.。说明白点,委派类就是做了一层封装和抽象,不将业务处理的大量组件暴露给请求层或者说是视图层。

public class Client {
    private BusinessDelegate businessDelegate;

    public Client(BusinessDelegate businessDelegate) {
        this.businessDelegate = businessDelegate;
    }

    public void doTask(){
        businessDelegate.doTask();
    }
}

好了,我们写个Main测试一下。

public class AppMain {
    public static void main(String[] args) {
        BusinessDelegate businessDelegate = new BusinessDelegate();
        BussinessLookup bussinessLookup = new BussinessLookup();
        bussinessLookup.setLoginService(new LoginService());
        bussinessLookup.setOrderService(new OrderService());

        businessDelegate.setBussinessLookup(bussinessLookup);
        businessDelegate.setServerType(ServerType.LOGIN);
        Client client = new Client(businessDelegate);
        client.doTask();

//        businessDelegate.setServerType(ServerType.ORDER);
//        client.doTask();
    }
}

结果:

处理登录相关业务

在这里,非常有必要看下类图

在这里插入图片描述

应用场景

  1. 当你要实现表现层和业务层之间的松耦合的时候。
  2. 当你想要编排多个服务之间的调用的时候。
  3. 当你想要再封装一层服务查找和调用时候

转载于:https://www.cnblogs.com/happyone/p/12496880.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值