在传统的代码库中,我们常常会看到一些违反了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. 学习swift从青铜到王者之Swift集合数据类型03

    1 数组的定义 var array1 = [,,,] var array2: Array = [,,,] var array3: Array<Int> = [,,,] var array4 ...

  2. 踩坑录-IDEA编辑器:找不到TomcatService或ApplicationServers----TomcatService使用指南

    一.找不到TomcatService或ApplicationServers Setp1. 检查IDEA版本 检查IDEA版本是否为Ultimate(终极版需要激活),Community(社区版免费无需 ...

  3. [python]pycharm画图插件matplotlib、numpy、scipy的下载与安装

    最近在用pycharm学习python语言,不得不感叹python语言的强大与人性化! 但对于使用pycharm画图(较复杂的图)就要用到几个插件了,即matplotlib.numpy和scipy!但 ...

  4. Smart Home DIY 计划

    工作了这么长时间了,感觉自己眼下的工作内容非常不利于技术水平的提升,对此状况,我心里深感不踏实.因此,我决定利用下班时间.边学习边做,做一套真正可用的智能家居系统,首先部署到自己居住的房间. 对此智能 ...

  5. C# .NET如何清空stringbuilder

    就红色的代码可以:   System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("hello&qu ...

  6. Kinect驱动的人脸实时动画

    近期几年.realtime的人脸动画開始风声水起.不少图形图像的研究者開始在这个领域不断的在顶级会议siggraph和期刊tog上面发文章. 随着kinect等便宜的三维数据採集设备的运用.以及其功能 ...

  7. netty4与protocol buffer结合简易教程

    各项目之间通常使用二进制进行通讯,占用带宽小.处理速度快~ 感谢netty作者Trustin Lee.让netty天生支持protocol buffer. 本实例使用netty4+protobuf-2 ...

  8. C++入门学习——模板

    为什么须要模板? 我们已经学过重载(Overloading),对重载函数而言,C++ 通过函数參数(參数个数.參数类型)的正确匹配来调用重载函数.比如.为求两个数的最大值,我们定义 max () 函数 ...

  9. Spring+Quartz实现定时任务的配置方法(插曲)

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46376093 1.Scheduler的配置 <bean class=&quo ...

  10. uva 10765 Doves and Bombs(割顶)

     题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标 ...