`
chelsea
  • 浏览: 112479 次
  • 来自: ...
社区版块
存档分类
最新评论

On Simple Design II: 简单 != 少

 
阅读更多


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来写测试用例肯定也会用到这类词汇,可到代码中突然没有了,只剩字符串了, 你告诉我这是简单设计,你这是令人困惑的设计.

分享到:
评论

相关推荐

    .Responsive.Mobile.Design.Designing.for.Every.Device

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

    Fundamentals of Digital Logic with Verilog Design

    By Stephen Brown, Zvonko Vranesic, * Publisher: McGraw-Hill Science/Engineering/Math ... Altera's advanced Max plus II CAD systmem (on CD-ROM) and a series of step-by-step tutorials are included.

    Game design: theory & practice

    Ten Years On, Who Would Publish Tetris? .....................157 Chapter 9 Artificial Intelligence..................158 Goals of Game AI...................................160 Challenge the Player........

    C++ Programming: From Problem Analysis to Program Design, 8th Edition

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

    Multiprocessor+Systems-on-Chips

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

    Cisco Press - OSPF Network Design Solutions, 2nd Edition

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

    C++.Programming.From.Problem.Analysis.to.Program.Design.7th.Edition

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

    Business.Analytics.for.Decision.Making.14822217

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

    Data.Modeling.Made.Simple.with.Embarcadero.ERStudio

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

    Good.Habits.for.Great.Coding.1484234588.pdf

    Good Habits for Great Coding distills Stueben's three decades of analyzing his own mistakes, analyzing student mistakes, searching for problems that teach lessons, and searching for simple examples to...

    ARM system-on-chip architecture

    13.1 The VLSI Ruby II Advanced Communication Processor 13.2 The VLSI ISDN Subscriber Processor 13.3 The OneC™ VWS22100 GSM chip 13.4 The Ericsson-VLSI Bluetooth Baseband Controller 13.5 The ARM7500 ...

    Processing.for.Android

    This book goes through a comprehensive series of hand-on projects, ranging from simple sketches to more complex projects involving sensors and integration with larger apps. It also covers important ...

    How to Create Selling E-Commerce Websites.pdf

    15 Common Mistakes in E-Commerce Design (Part II) 97 E-Commerce: Fundamentals of a Successful Re-Design 114 Improve Your E-Commerce Design With Brilliant Product Photos 126 How To Use Photos To ...

    JavaScript Applications with Node.js, React, React Native and MongoDB

    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 出版...

    PWM ON FPGA

    This report describes the design of a simple Pulse Width Modulator. The design is consists of data path and control path. The data path is consists of counter, a d-register, a comparator, a 2:1 ...

    Practical C++ Programming C++编程实践

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

    Homework 7_Knowledge items of C++_(part 2).zip

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

    Packt.Python.Journey.from.Novice.to.Expert.2016

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

    np难问题近似算法(绝版好书)

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

    Proxy Pattern Informatization Research Based On SaaS

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

Global site tag (gtag.js) - Google Analytics