See alsoLightening Talk: 简单设计
经常碰到的一段对话是:“这里为什么要引入一个类?字符串就好了,简单设计嘛” 或者 “一个switch/case就搞定的,弄个子类干嘛?”
事实通常是反过来的,引入新的类型或子类比使用字符串或switch/case更简单. 我来解释一下
设计是否简单的一个判断标准是它是否更容易理解. 而我们对事物的理解建立在概念或模型之上. 比如做一个最简单的超市存包系统,能存包能取包,没有进一步的需求.牵扯到的概念可能有包裹,储物柜,存完包有个凭证用于取包. 这时我的设计中引入一个空类public class Package {}来代表包裹,另外一个空类public class Ticket {}来代表凭证. 你跟我说应该用字符串因为它更简单,反正也不会有新的需求, 我是不同意的. 我们来看采用这两种设计的代码,比如存包:
- public Ticket Put(Package package){…}
- public string Put(string package) {…}
用起来:
- new Cabinet().Put(new Package());
- new Cabinet().Put(“some package”);
即使不再有扩展需求,即使第一种设计比第二种设计多两个没有任何行为的空类,第一种设计也要比第二种简单. 因为它更易于理解. 它跟我脑袋里关于存包这件事概念和词汇是一致的,不需要做任何翻译的. 而采用字符串,我看到代码后还得把它翻译成包裹,翻译成凭证,甚至没人跟我解释这段代码我还不知道应该做个翻译,它阻碍了我的理解,因此它是更复杂的. 这是第一个角度
Ubiquitous Language
第二个角度是沟通交流的问题. 我们在描述存包系统的需求的时候,不可避免的会提到包裹和凭证这类概念,Tester来写测试用例肯定也会用到这类词汇,可到代码中突然没有了,只剩字符串了, 你告诉我这是简单设计,你这是令人困惑的设计.
分享到:
相关推荐
Dutson shows how to design sites that are responsive “from the start,” while keeping development simple and flexible. Next, he delivers complete technical know-how for transforming responsive ...
C++ Programming: From Problem Analysis to Program Design By 作者: D. S. Malik ISBN-10 书号: 1337102083 ISBN-13 书号: 9781337102087 Edition 版本: 8 出版日期: 2017-02-13 pages 页数: 1491 Contents ...
Part II OSPF Routing and Network Design 161 Chapter 4 Design Fundamentals 163 Chapter 5 Routing Concepts and Configuration 225 Chapter 6 Redistribution 339 Chapter 7 Summarization 405 Part III OSPF ...
8 Design Space Exploration of On-Chip Networks: A Case Study 223 Bishnupriya Bhattacharya, Luciano Lavagno, and Laura Vanzago 8.1 Introduction 223 8.2 Background 225 8.2.1 Function/Architecture Co-...
Data Modeling Made Simple with Embarcadero ER/Studio Data Architect: Adapting to Agile Data Modeling in a Big Data World Build a working knowledge of data modeling concepts and best practices, along ...
Title: C++ Programming: From Problem Analysis to Program Design, 7th Edition Author: D. S. Malik Length: 1488 pages Edition: 7 Language: English Publisher: Course Technology Publication Date: 2014-02-...
Chapter 4: Simple Knapsack Problems Chapter 5: Assignment Problems Chapter 6: The Traveling Salesman Problem Chapter 7: Vehicle Routing Problems Chapter 8: Resource-Constrained Scheduling Chapter 9: ...
JavaScript Applications with Node.js, React, React Native and MongoDB: Design, code, test, deploy and manage in Amazon AWS By 作者: Eric Bush ISBN-10 书号: 0997196661 ISBN-13 书号: 9780997196665 出版...
The Programming Process Setting Up Your Work Area The Specification Code Design The Prototype The Makefile Testing Debugging Maintenance Revisions Electronic Archaeology Mark Up the Program Use the ...
Design of the homework In this homework, we will practice some important knowledge of C++ in the chapters on classes, before the inheritance part (chapter 13). A sequence of numbered tasks are ...
Chapter 11: Python Design Patterns II Chapter 12: Testing Object-oriented Programs Chapter 13: Concurrency Module 3: Mastering Python Chapter 1: Getting Started – One Environment per Project Chapter...
- **Overview of Eclipse and Its Design**: This section explains the architecture of Eclipse, focusing on its modular design, which allows for easy extension and customization. Key components include ...
10.4 Inapproximability results for problems in class II 10.4.1 SETCOVER 10.5 Inapproximability results lor problems in class 111 10.5.1 LABELCOVER maximization version ,. 10.5.2 LABELCOVER mtn version...
Abstract—Based on demands in proxy pattern, this article proposes informatization solution of proxy pattern which includes enterprise management, proxy management, franchisee management and supply ...
Faculty of Visual Communication Design, Kyushu Institute of Design, Fukuoka, Japan 815-8540 SUMMARY only by the context information even if there is no pattern input. Then, the response of the low-...