zoukankan      html  css  js  c++  java
  • 重构29-Remove Middle Man(去掉中间人)

    有时你的代码里可能会存在一些“Phantom”或“Ghost”类,Fowler称之为“中间人(Middle Man)”。这些中间人类仅仅简单地将调用委托给其他组件,除此之外没有任何功能。 这一层是完全没有必要的,我们可以不费吹灰之力将其完全移除。
    public class Consumer {
    public AccountManager AccountManager;//getter setter
    public Consumer(AccountManager accountManager) {
    AccountManager = accountManager;

    }
    public void Get(int id) {
    Account account = AccountManager.GetAccount(id);
    }
    }

    public class AccountManager {
    public AccountDataProvider DataProvider;//getter setter
    public AccountManager(AccountDataProvider dataProvider) {
    DataProvider = dataProvider;

    }
    public Account GetAccount(int id) {
    return DataProvider.GetAccount(id);
    }
    }
    public class AccountDataProvider {
    public Account GetAccount(int id) {
    // get account
    }
    }
    最终结果已经足够简单了。我们只需要移除中间人对象,将原始调用指向实际的接收者。
    public class Consumer {
    public AccountDataProvider AccountDataProvider;//getter settter

    public Consumer(AccountDataProvider dataProvider) {
    AccountDataProvider = dataProvider;
    }

    public void Get(int id) {
    Account account = AccountDataProvider.GetAccount(id);
    }
    }

    public class AccountDataProvider {
    public Account GetAccount(int id) { // get account
    }
    }





  • 相关阅读:
    C++之STL总结精华笔记
    [转]asp.net使用uploadify上传出现的IO Error问题
    $.post()参数及返回值
    用CSS3.0画圆
    C# 用代码返回上一页
    用js获取cookie
    html页面的局部刷新
    在Asp.Net中使用amChart统计图
    内存中DataTable去除重复行
    .net中对HTTP请求的两种请求:Get和Post的操作
  • 原文地址:https://www.cnblogs.com/jgig11/p/5786500.html
Copyright © 2011-2022 走看看