Building Maintainable Software-java篇之Separate Concerns in Modules
—Charles Perrow’s Normal Accidents
theory in one sentence
Guideline:
• Avoid large modules in order to
achieve loose coupling between them.
• Do this by assigning responsibilities to separate modules and hiding implementation details behind interfaces
• This improves maintainability because changes in a loosely coupled codebase are much easier to oversee and execute than changes in a tightly coupled codebase.
Remember that the concept of a module translates to a class in object-oriented languages such as Java.
Motivation
The biggest advantage of keeping classes small is that it provides a direct path toward loose coupling between classes. Loose coupling means that your class-level design will be much more flexible to facilitate future changes. By “flexibility” we mean that
you can make changes while limiting unexpected effects of those changes. Thus, loose coupling allows developers to work on isolated parts of the codebase without creating change ripples that affect the rest of the codebase. A third advantage, which cannot
be underestimated, is that the codebase as a whole will be much more open to less experienced developers.
Small, Loosely Coupled Modules Allow Developers to Work on Isolated Parts of the Codebase
When a class is tightly coupled with other classes, changes to the implementation of the class tend to create ripple effects through the codebase. For example, changing the interface of a public method
leads to code changes everywhere the method is called. Besides the increased development effort, this also increases the risk that class modifications lead to bugs or inconsistencies in remote parts of the codebase.
Small, Loosely Coupled Modules Ease Navigation Through the Codebase
Not only does a good separation of concerns keep the codebase flexible to facilitate future changes, it also improves the analyzability of the codebase since classes encapsulate data and implement logic
to perform a single task. Just as it is easier to name methods that only do one thing, classes also become easier to name and understand when they have one responsibility. Making sure classes have only one responsibility is also known as the single
responsibility principle.
Small, Loosely Coupled Modules Prevent No-Go Areas for New Developers
Classes that violate the single responsibility principle become tightly coupled and accumulate a lot of code over time. As with the UserService example in the introduction of this chapter, these classes
become intimidating to less experienced developers, and
even experienced developers are hesitant to make changes to their implementation. A codebase that has a large number of classes that lack a good separation of concerns is very difficult to adapt to new requirements.
How to Apply the Guideline
In general, this guideline prescribes
keeping your classes small (by addressing only one concern) and
limiting the number of places where a class is called by code outside the class itself. Following are three development best practices that help to prevent tight coupling between classes in a codebase.
Split Classes to Separate Concerns
Designing classes that collectively implement functionality of a software system is the most essential step in modeling and designing object-oriented systems. In typical software projects we see that
classes start out as logical entities that implement a single functionality but over time gain more responsibilities. To prevent classes from getting a large class smell, it is crucial that developers take action if a class has more than one responsibility
by splitting up the class.
Hide Specialized Implementations Behind Interfaces
We can also achieve loose coupling by
hiding specific and detailed implementations behind a high-level interface.
Replace Custom Code with Third-Party Libraries/Frameworks
A third situation that typically leads to tight module coupling are classes that provide generic or utility functionality. Classic examples are classes called StringUtils and FileUtils. Since these classes
provide generic functionality, they are obviously called
from many places in the codebase. In many cases this is an occurrence of tight coupling that is hard to avoid. A best practice, though, is to keep the class sizes limited and to periodically review (open source) libraries and frameworks to check if they
can replace the custom implementation. Apache Commons and Google Guava are widespread libraries with frequently used utility functionality. In some cases, utility code can be replaced with new Java language features or a company-wide shared
library.
读书笔记:
by Joost Visser
Copyright © 2016 Software Improvement Group, B.V. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Acquisitions Editor: Rachel Roumeliotis
Editor: Nan Barber
Production Editor: Matthew Hacker
Copyeditor: Rachel Monaghan
Proofreader: Marta Justak
Indexer: WordCo Indexing Services, Inc.
Interior Designer: David Futato
Cover Designer: Randy Comer
Illustrator: Rebecca Demarest
February 2016: First Edition
Revision History for the First Edition
2016-01-25: First Release
See http://shop.oreilly.com/product/0636920049159.do
Building Maintainable Software-java篇之Separate Concerns in Modules的更多相关文章
- Building Maintainable Software-java篇之Couple Architecture Components Loosely
Building Maintainable Software-java篇之Couple Architecture Components Loosely There are two ways of co ...
- JSON总结(java篇)
JSON总结(java篇一) JSON简介 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于 ...
- [转]有哪些值得关注的技术博客(Java篇)
有哪些值得关注的技术博客(Java篇) 大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1. ...
- 面试总结——Java篇
前言:前期对Java基础的相关知识点进行了总结,具体参看:Java基础和面试知识点.近期由于笔者正在换工作(ing),因此下面将笔者在面试过程中或笔者朋友面试过程中反馈的题目进行总结,相信弄清楚下面题 ...
- 事件驱动模型实例详解(Java篇)
或许每个软件从业者都有从学习控制台应用程序到学习可视化编程的转变过程,控制台应用程序的优点在于可以方便的练习某个语言的语法和开发习惯(如.net和java),而可视化编程的学习又可以非常方便开发出各类 ...
- Tools:downloading and Building EDK II工具篇:安装/使用EDKII源代码获取/编译工具[2.3]
Tools:Installing and using the Required Tools for downloading and Building EDK II工具篇:安装/使用EDKII源代码获取 ...
- 管中窥豹——框架下的SQL注入 Java篇
管中窥豹--框架下的SQL注入 Java篇 背景 SQL注入漏洞应该算是很有年代感的漏洞了,但是现在依然活跃在各大漏洞榜单中,究其原因还是数据和代码的问题. SQL 语句在DBMS系统中作为表达式被解 ...
- Eclipse Tomcat 7.0 添加WEB项目报错:Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
前言 我叫梅乾花,我误闯了“从零开始的程序世界”,遭受到了前所未有的的困难,为了活下去,为了看见美好的明天,我开始学习之旅. 问题篇我打开了"Eclipse",将项目导入其中,开启 ...
- tomcat部署项目时 报错Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules
Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 and 6 Web modules 解决方法: 找到文件 .set ...
随机推荐
- python基础--常用模块与面向对象基础
1常用模块 1.1 xml xml是实现不同语言或程序之间进行数据交换的协议 xml的格式如下: <?xml version="1.0"?> <data> ...
- C#Windows服务:一些方法(启动、停止等)
前面讲述了如何创建和安装服务(创建windows服务),下面把启动.停止.判断是否启动的方法也写一下. /// <summary> /// 判断是否安装了某个服务 /// </sum ...
- duplicate symbol _gestureMinimumTranslation in:
在真机上编译无误,在模拟器上编译报错:duplicate symbol _gestureMinimumTranslation in: /Users/Sam/Library/Developer/Xcod ...
- 大湿教我写程序(2)之走向AV之路
一.大摆庆功宴 上一篇博文<大湿教我写程序(1)之菜单导航篇>中讲到了我撸码到晚上两点多,整出了一个还算是高端大气上档次的demo.半夜回到家里打算着可以好好睡上一个懒觉,到时候直接到客户 ...
- 电脑(台式机||笔记本)开机password忘记通用解决方法
方法:直接制作一个老毛桃装机版u盘启动盘 网址:老毛桃官网 步骤:依照网址的解说,将制作好的U盘插入到电脑的usb插口.执行Windows 登入password破解菜单,搜索password所在的盘符 ...
- VS2010中使用命令行參数
在Linux下编程习惯了使用命令行參数,故使用VS2010时也尝试了一下. 新建项目,c++编敲代码例如以下: #include<iostream> #include<fstream ...
- Zabbix的SNMPTrap监控配置
SNMPTrap监控主要用于设备发生故障时的主动通知的监控.以下简单记录下Zabbix的SNMPTrap的配置方法. 一.SNMPTrap监控的处理流程说明 1.监控对象发送SNMPTrap信息到sn ...
- 纯C实现面向对象
#include <stdio.h> #include <stdlib.h> //接口 #ifndef Interface #define Interface struct # ...
- Mybatis c3p0 整合
1.下载c3p0 2.实现 UnpooledDataSourceFactory package com.joinhealth.esb.db; import org.apache.ibatis.data ...
- Spark下载与入门(Spark自学二)
2.1 下载Spark 略 2.2 Spark中Python和Scala的shell Spark shell可用来与分布式存储在许多机器的内存或者硬盘上的数据进行交互,并且处理过程的分发由Spark自 ...