Clean Code 读书笔记八
来源:互联网 发布:什么是旋转变换矩阵 编辑:程序博客网 时间:2024/04/28 20:32
简单设计四规则:
• Runs all the tests
• Contains no duplication
• Expresses the intent of the programmer
• Minimizes the number of classes and methods
(作者认为的金钥匙)
运行所有测试
低耦合 高内聚 造成的结果就是会有更多的类与方法。我们就更易编写独立的单元测试。
不可重复
重复不仅仅指重复的代码,当然还有其他形式的存在。比如
int size() {}boolean isEmpty() {}
可以分别实现,也可以:
boolean isEmpty() {return 0 == size();}
再看看这个,不要放过少量的重复:
public void scaleToOneDimension(float desiredDimension, float imageDimension) { if (Math.abs(desiredDimension - imageDimension) < errorThreshold) return; float scalingFactor = desiredDimension / imageDimension; scalingFactor = (float)(Math.floor(scalingFactor * 100) * 0.01f); RenderedOp newImage = ImageUtilities.getScaledImage( image, scalingFactor, scalingFactor); image.dispose(); System.gc(); image = newImage;}public synchronized void rotate(int degrees) { RenderedOp newImage = ImageUtilities.getRotatedImage( image, degrees); image.dispose(); System.gc(); image = newImage;}
重构后:
public void scaleToOneDimension(float desiredDimension, float imageDimension) { if (Math.abs(desiredDimension - imageDimension) < errorThreshold) return; float scalingFactor = desiredDimension / imageDimension; scalingFactor = (float) (Math.floor(scalingFactor * 100) * 0.01f); replaceImage(ImageUtilities.getScaledImage(image, scalingFactor, scalingFactor)); } public synchronized void rotate(int degrees) { replaceImage(ImageUtilities.getRotatedImage(image, degrees)); } private void replaceImage(RenderedOp newImage) { image.dispose(); System.gc(); image = newImage; }
实现大规模复用,必须从理解小规模复用开始。
- 使用模版模式TEMPLATE METHOD pattern 消除重复:
public class VacationPolicy { public void accrueUSDivisionVacation() { // code to calculate vacation based on hours worked to date // ... // code to ensure vacation meets US minimums // ... // code to apply vaction to payroll record // ... } public void accrueEUDivisionVacation() { // code to calculate vacation based on hours worked to date // ... // code to ensure vacation meets EU minimums // ... // code to apply vaction to payroll record // ... }}
用一个抽象方法去实现公用的代码,并再各自的继承类中实现各自不同的代码:
abstract public class VacationPolicy { public void accrueVacation() { calculateBaseVacationHours(); alterForLegalMinimums(); applyToPayroll(); } private void calculateBaseVacationHours() { /* ... */ }; abstract protected void alterForLegalMinimums(); private void applyToPayroll() { /* ... */ };}public class USVacationPolicy extends VacationPolicy { @Override protected void alterForLegalMinimums() { // US specific logic }}public class EUVacationPolicy extends VacationPolicy { @Override protected void alterForLegalMinimums() { // EU specific logic }}
0 0
- Clean Code 读书笔记八
- 《Writing clean code》读书笔记
- 《clean code》读书笔记
- Clean Code 读书笔记
- Clean Code读书笔记
- clean code读书笔记
- 《clean code》读书笔记
- Clean Code 读书笔记一
- Clean Code 读书笔记二
- Clean Code 读书笔记三
- Clean Code 读书笔记四
- Clean Code 读书笔记五
- Clean Code 读书笔记六
- Clean Code 读书笔记七
- Clean Code 读书笔记九
- clean code 读书笔记
- Writing clean code 读书笔记
- 《clean code》读书笔记
- declare-styleable (自定义控件的编写)
- (15.1.10)FTP服务器配置以及访问
- OSPF(五):链路状态数据库
- RecycleView 明明有数据却显示不出来?
- iOS开发之Xcode6之后不再自动创建Pch预编译文件
- Clean Code 读书笔记八
- 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect
- adb shell 命令详解
- 二叉查找树(二叉排序树)的详细实现
- 没有对比没有差距1
- OC的三大特性--封装
- (15.1.10.1)在Win7的IIS上搭建FTP服务及用户授权
- 面向对象原则
- Android listview addHeaderView 和 addFooterView 详解