在传统的代码库中,我们常常会看到一些违反了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. Django学习系列之CSRF

    Django CSRF 什么是CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到 ...

  2. Logstash学习系列之插件介绍

    Logstash插件获取方式 插件获取地址: https://github.com/logstash-plugins  在线安装: /plugin install logstash-input-jdb ...

  3. POJ 1849 Two(遍历树)

    POJ 1849 Two(遍历树) http://poj.org/problem?id=1849 题意: 有一颗n个结点的带权的无向树, 在s结点放两个机器人, 这两个机器人会把树的每条边都走一遍, ...

  4. java实现floyd统计天津地铁的网站距离

    一:说明 (1)使用floyd实现各个网站的计算记录和路径 (2)网站获取和初始距离依据外部文件得到 (3)结果以外部文件的形式存储 (4)网站间转乘,觉得初始值也为1 (5)代码凝视比較具体,如有疑 ...

  5. ZOJ 3819 Average Score 水

    水 Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Univer ...

  6. javascript返回顶端源代码

    <div style="display:none" id="goTopBtn"><img src="http://www.unity ...

  7. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  8. 华为云分布式数据库中间件DDM和开源MyCAT对比

    前言 华为云分布式数据库中间件(Distributed Database Middleware)是解决数据库容量.性能瓶颈和分布式扩展问题的中间件服务,提供分库分表.读写分离.弹性扩容等能力,应对海量 ...

  9. ionic/cordova 真机调试

    android下简单,连接手机后,直接: myProjectPath>ionic run android ios下比较麻烦点,要先装ios-deploy: ​sudo npm install - ...

  10. luence全文检索(简介)

    刚开始做全文检索也是找了很多资料但是网上的都不是很齐全luence是个很不多的工具 Lucene4.0的官网文档:http://lucene.apache.org/core/4_0_0/core/ov ...