在传统的代码库中,我们常常会看到一些违反了SRP原则的类。这些类通常以Utils或Manager结尾,有时也没有这么明显的特征而仅仅是普通的包含多个功能的类。这种God类还有一个特征,使用语句或注释将代码分隔为多个不同角色的分组,而这些角色正是这一个类所扮演的。 
久而久之,这些类成为了那些没有时间放置到恰当类中的方法的垃圾桶。这时的重构需要将方法分解成多个负责单一职责的类。
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
}
}
使用该重构是非常简单明了的,只需把相关方法提取出来并放置到负责相应职责的类中即可。这使得类的粒度更细、职责更分明、日后的维护更方便。上例的代码最终被分解为两个类:
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. jQuery源代码解析(1)—— jq基础、data缓存系统

    闲话 jquery 的源代码已经到了1.12.0版本号.据官网说1版本号和2版本号若无意外将不再更新,3版本号将做一个架构上大的调整.但预计能兼容IE6-8的.或许这已经是最后的样子了. 我学习jq的 ...

  2. 【CV论文阅读】An elegant solution for subspace learning

    Pre: It is MY first time to see quite elegant a solution to seek a subspace for a group of local fea ...

  3. android 深入浅出 群内“每日一问” 问答总结

    永远不变的就是变. 俗话说的好,环境改变人生. 常常面对的是一群积极奋进的人,那么你的心态和生活也会变的充满斗志.青春在于折腾,趁我们还年轻,拿出你的激情.踏着泪水载着梦,才干拥有自己的一片天空. 上 ...

  4. SVN的配置与使用方法

    1.所选服务器安装包:VisualSVN-Server-2.1.3.msi. 2.客户端安装包:TortoiseSVN-1.6.2.16344-win32-svn-1.6.2.msi 一.服务器的安装 ...

  5. python爬虫实践

    模拟登陆与文件下载 爬取http://moodle.tipdm.com上面的视频并下载 模拟登陆 由于泰迪杯网站问题,测试之后发现无法用正常的账号密码登陆,这里会使用访客账号登陆. 我们先打开泰迪杯的 ...

  6. Jenkins 使用

    Jenkins 安装 Jenkins是用Java语言开发的系统,首先要确定服务器上已经安装JDK或者JRE. 安装方式一 直接运行java –jar Jenkins.war,在浏览器中输入 http: ...

  7. oracle 11g GRID 中 关于 OLR 须要知道的一些内容

     oracle 11g GRID 中 关于 OLR 须要知道的一些内容 1.检查olr 的状态: [root@vmrac1 ~]# ocrcheck -local Status of Oracle ...

  8. sdut 4-1 复数类的运算符重载

    4-1 复数类的运算符重载 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 通过本题目的练习能够掌握成员运算符重载及友元运算符重载 要求定义一个复数类.重 ...

  9. hbase 增删改查 api 简单操作

    package com.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; im ...

  10. [mac]WireShark检測不到网卡怎么办?

    wireshark是一个很好用的抓包工具,有windows版本号和mac版本号,在mac下安装wireshark,启动后发现提示"There are no interfaces on whi ...