在传统的代码库中,我们常常会看到一些违反了SRP原则的类。这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类。这种God类还有一个特征,使用语句或注释将代码分隔为多个不同角色的分组,而这些角色正是这一个类所扮演的。 
久而久之,这些类成为了那些没有时间放置到恰当类中的方法的垃圾桶。这时的重构需要将方法分解成多个负责单一职责的类。
  1. public class CustomerService {
    public Double CalculateOrderDiscount(List<Product> products, Customer customer) {
    // do work
    }
    public Boolean CustomerIsValid(Customer customer, Order order) {
    // do work
    }
    public List<String> GatherOrderErrors(List<Product> products, Customer customer) {
    // do work
    }
    public void Register(Customer customer) {
    // do work
    }
    public void ForgotPassword(Customer customer) {
    // do work
    }
    }
使用该重构是非常简单明了的,只需把相关方法提取出来并放置到负责相应职责的类中即可。这使得类的粒度更细、职责更分明、日后的维护更方便。上例的代码最终被分解为两个类:
  1. public class CustomerOrderService {
    public Double CalculateOrderDiscount(List<Product> products, Customer customer) {
    // do work
    }
    public Boolean CustomerIsValid(Customer customer, Order order) {
    // do work
    }
    public List<String> GatherOrderErrors(List<Product> products, Customer customer) {
    // do work
    }
    }

    public class CustomerRegistrationService {
    public void Register(Customer customer) {
    // do work
    }
    public void ForgotPassword(Customer customer) {
    // do work
    }
    }



重构27-Remove God Classes(去掉神类)的更多相关文章

  1. 重构第27天 去除上帝类(Remove God Classes)

    理解:本文中的”去除上帝类”是指把一个看似功能很强且很难维护的类,按照职责把自己的属性或方法分派到各自的类中或分解成功能明确的类,从而去掉上帝类. 详解:我们经常可以在一些原来的代码中见到一些类明确违 ...

  2. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  3. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. eclipse使用maven打包时去掉测试类

    eclipse使用maven打包时去掉测试类 在pom.xml文件中增加如下配置: <plugin> <groupId>org.apache.maven.plugins< ...

  6. 编写高质量代码改善C#程序的157个建议——建议147:重构多个相关属性为一个类

    建议147:重构多个相关属性为一个类 若存在多个相关属性,就应该考虑是否将其重构为一个类.查看如下类: class Person { public string Address { get; set; ...

  7. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  8. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  9. LeetCode 27 Remove Element

    Problem: Given an array and a value, remove all instances of that value in place and return the new ...

随机推荐

  1. JAVA 流程控制之选择语句

    在程序设计时,有三种基本技术可以改变程序的流程控制: 调用方法: 选择: 循环. 在这里,我们主要来讲讲选择语句. JAVA中的选择语句与C语言中的基本相同,包括: if 语句: if/else 语句 ...

  2. windows 7 文件加密设置

    方法/步骤1 加密文件 1 右击需加密的文件,选择“属性”命令. 2 在属性对话框的“常规‘选项卡中单击”高级“按钮. 3 在弹出的对话框中选中”加密内容以便保护数据“复选框,单击”确定“按钮. 4 ...

  3. Loadrunner&Jemeter进行手机APP压力测试

    一.loadrunner通过代理录制app脚本 随着手机APP的广泛应用,手机应用的使用已占据了大量的市场份额,尤其是优秀的手机APP,动辄用户过千万过亿,对于如此庞大的用户量,我们在开发APP时,也 ...

  4. ssh 远程登陆指定port

    ssh 到指定port  ssh -p xx user@ip      xx 为 port号    user为username   ip为要登陆的ip

  5. 《转》 Ceilometer项目源代码分析----ceilometer项目源代码结构分析

    感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/gaoxingnengjisua ...

  6. storm与hadoop的对照

       hadoop 是实现了 mapreduce 的思想,将数据切片计算来处理大量的离线数据. hadoop处理的数据必须是已经存放在 hdfs 上或者类似 hbase 的数据库中.所以 hadoop ...

  7. JavaScript基础 -- BOM

    JavaScript三大核心: 1)核心(ECMAScript):描述了JS的语法和基本对象 2)文档对象模型(DOM):处理网页内容的方法和接口 3)浏览器对象模型(BOM):与浏览器交互的方法和接 ...

  8. 基本数据类型操作的补充和set的基本操作

    1,补充基本数据类型的知识点:    "  " .join(内容)...........什么加到什么里边,内容可以是:int,str,dic,tup. lst = ["周 ...

  9. go7---map

    package main /* map 类似其它语言中的哈希表或者字典,以key-value形式存储数据 Key必须是支持==或!=比较运算的类型,不可以是函数.map或slice, 这3中类型都不能 ...

  10. oracle type类型

    转载 http://blog.sina.com.cn/s/blog_6cfb6b090100ve92.html 转自网络,具体用法我会再细化 1.概念    方法:是在对象类型说明中用关键字  MEM ...