Building Maintainable Software-java篇之Separate Concerns in Modules

Building Maintainable Software-java篇之Separate Concerns in Modules

  In a system that is both complex and tightly coupled, accidents are inevitable.

—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.

读书笔记:

Building Maintainable Software: Ten Guidelines for Future-Proof Code

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 [email protected]

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

时间: 2024-10-12 16:51:36

Building Maintainable Software-java篇之Separate Concerns in Modules的相关文章

Building Maintainable Software-java篇之Keep Architecture Components Balanced

Building encapsulation boundaries is a crucial skill in software architecture. -George H. Fairbanks in Just Enough Architecture Guideline: ? Balance the number and relative size of top-level components in your code. ? Do this by organizing source cod

Building Maintainable Software-java篇之Write Short Units of Code

Building Maintainable Software-java篇之Write Short Units of Code Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -Martin Fowler Guideline: ? Limit the length of code units to 15 lines of c

Building Maintainable Software-java篇之 Write Clean Code

Building Maintainable Software-java篇之 Write Clean Code Writing clean code is what you must do in order to call yourself a professional. -Robert C. Martin Guideline: ? Write clean code. ? Do this by not leaving code smells behind after development wor

[转]有哪些值得关注的技术博客(Java篇)

有哪些值得关注的技术博客(Java篇) 大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1.总结知识点相对比较全面 2.一般来说讲解知识点通俗易懂 3.路线比较清晰,不会有太多的冗余内容. 这样一来,对于自学的朋友来说,一些专业的博客不但大量的缩减了你得学习时间.提高了学习效率.更重要的是这些博客能培养你对编程的兴趣. 于是,这几期文章会主要推荐一些技术博客,目前计划打算主要分为: (已完) 

关于多线程的几个问题(面试小问题,Java篇)

面试官想考考我多线程方面的问题. 1.请说下线程与进程的关系. 答:<操作系统>中是这样说的 (1)一个线程只能属于一个进程,而一个进程可以有多个线程,但至少有一个线程.线程是操作系统可识别的最小执行和调度单位. (2)资源分配给进程,同一进程的所有线程共享该进程的所有资源.同一进程中的多个线程共享代码段(代码和常量),数据段(全局变量和静态变量),扩展段(堆存储).但是每个线程拥有自己的栈段,栈段又叫运行时段,用来存放所有局部变量和临时变量. (3)处理机分给线程,即真正在处理机上运行的是线

关于一些设计模式(面试小问题,Java篇)

今天在某公司(不透露了)写了一套Java面试题.有个题大概是这样的. Which design pattern used in Java.lang.Runtime? 就是说java.lang.Runtime里,用了什么设计模式 还有java.utils.collection里用了什么设计模式. 当时由于对这冷门知识了解的不够多,而且api也读的少,所以一时答不上来. 回到家搜了下. 答: Runtiome里 单例模式. 1)  每一个Java程序都有一个Runtime类的单一实例. 2)  通过

Tools:Installing and using the Required Tools for downloading and Building EDK II工具篇:安装/使用EDKII源码获取/

Tools:Installing and using the Required Tools for downloading and Building EDK II工具篇:安装/使用EDKII源码获取/编译工具[2.3] 2015-07   北京海淀区  张俊浩 2. Setting Up EDKII Development Environment(EDKII开发环境的搭建) ->2.1 The General Procedure Of Setting Up EDKII Development E

优秀的编程风格(Java篇)——高薪必看

今天突发奇想,对编码习惯和编程风格很感兴趣,于是乎,找了一下关于编程风格(Java篇)的资料,希望对爱好编码或者开始学习编码的同学有帮助! 来自<The Elements of Java Style>-<Java编程风格>一书,值得一读的书籍,会让你在细节上节省很多时间,合作之间更加愉快! 好处不多说了,但是有几个原则如下: 1.保持原有风格 2.坚持最小惊奇原则 3.第一次就做对 4.记录所有非规范行为 格式规范: 1.缩进嵌套的代码:在每个代码块和嵌套中加入缩进,缩进代码,加强

事件驱动模型实例详解(Java篇)

或许每个软件从业者都有从学习控制台应用程序到学习可视化编程的转变过程,控制台应用程序的优点在于可以方便的练习某个语言的语法和开发习惯(如.net和java),而可视化编程的学习又可以非常方便开发出各类人机对话界面(HMI).可视化编程或许是一个初学者开始对软件感兴趣的开始,也可能是一个软件学习的里程碑点,因为我们可以使用各类软件集成开发环境(IDE)方便的在现成的界面窗口上拖放各类组件(Component),这类组件包括我们常见的按钮(Button),单选按钮(Radio Button),复选框