1. You may run across a class whose instances come in two or more flavors and contain a tag field indicating the flavor of the instance. (i.e. An Operator class with one field to indicate what operation it does.)
2. Such tagged classes have numerous shortcomings. They are cluttered with boilerplate, including enum declarations, tag fields, and switch statements. Readability is further harmed because multiple implementations are jumbled together in a single class. Memory footprint is increased because instances are burdened with irrelevant fields belonging to other flavors. You can’t add a flavor to a tagged class unless you can modify its source file. In short, tagged classes are verbose, error-prone, and inefficient. The tag could be eliminated and the class replaced by a hierarchy.
3. To transform a tagged class into a class hierarchy, first define an abstract class containing an abstract method for each method in the tagged class whose behavior depends on the tag value. If there are any methods whose behavior does not depend on the value of the tag, put them in this class. Similarly, if there are any data fields used by all the flavors, put them in this class. Next, define a concrete subclass of the root class for each flavor of the original tagged class. Include in each subclass the data fields particular to its flavor. Also include in each subclass the appropriate implementation of each abstract method in the root class.
相关推荐
Item 23: Prefer class hierarchies to tagged classes Item 24: Favor static member classes over nonstatic Item 25: Limit source files to a single top-level class 5 Generics Item 26: Don’t use raw types...
Item 20: Prefer Immutable Atomic Value Types 114 Chapter 3 Expressing Designs in C# 125 Item 21: Limit Visibility of Your Types 126 Item 22: Prefer Defining and Implementing Interfaces to Inheritance ...
专享:prefer的用法__(全).pdf
I get asked this question sometimes from seasoned programmers who are new to C++. There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet ...
Therefore, we prefer to minimize includes, particularly includes of header files in other header files. You can significantly minimize the number of header files you need to include in your own ...
ESLint是针对javascript代码的静态检查工具。资源是本人配置的ESLint 规则文件。官方文档:http://eslint.org/docs/rules/。
npm i --save-dev eslint-plugin-class-prefer-methods 用法 在您的.eslintrc : { "plugins" : [ "class-prefer-methods" ] , "rules" : { "class-prefer-methods/prefer-methods" : 2 } } 为什么 箭头函数...
public.actor.first_name: error prefer-text-to-varchar : Prefer text to varchar types 用法 安装方式: $ npm i -g schemalint 要运行,请确保您位于具有.schemalintrc.js配置文件的文件夹中,然后键入: $ ...
条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-by-reference-to-const to pass-by-value. 条款21:必须返回对象时,别妄想返回其reference don't try to return a reference when you must ...
Prefer recent versions of the above packages, in order to avoid compatibility issues, notably for Matplotlib. Note that this latter package is not necessary for the program to run, although you might ...
Minikube and Docker for Mac or Windows should undoubtedly be there for those who prefer to "play" with Docker locally. AWS is the biggest hosting provider so Kubernetes Operations (kops) must be ...
var prefer = req.prefer; if (prefer.handling === 'strict') { // strict handling } else if (prefer.handling === 'lenient') { // lenient handling } if (prefer.respondAsync) { // client prefers ...
eslint-plugin-classnames 在 JSX 中警告和格式化长类名的用法安装您首先需要安装... { " rules " : { " classnames/prefer-classnames-function " : 2 }}支持的规则 :check_mark: :在配置中启用。 :wrench: : 可以用
4.1.3. Prefer non-final classes (optional) 4.1.4. Declare accessors and mutators for persistent fields (optional) 4.2. Implementing inheritance 4.3. Implementing equals() and hashCode() 4.4. Dynamic ...
Yii2框架的队列邮件装饰器使用Yii2队列在后台发送您的电子邮件。... 'mailer' => [ 'class' => \YarCode\Yii2\QueueMailer\Mailer::class, 'syncMailer' => [ 'class' => \yii\swiftmailer\Mailer::class
SI3US-721119-13700 psh create the following serial number : prefer number 6 SI3US-000006-39407
--tee : mirror output to a filename specified after '--tee' -4 : prefer ipv4 -6 : prefer ipv6 -c : only show an output line on changed state --file : treat the "server-address" as a filename ...
启用首选:安全的HTTP标头 Chrome扩展程序启用prefer: safe HTTP标头。 更多信息: :
传单首选画布使用meteorhacks:inject-initial软件包注入L_PREFER_CANVAS = true; 选项,然后加载您喜欢的传单包。 这被添加到您的文档头中: <head> [removed]L_PREFER_CANVAS = true;[removed] ... style...
Berger_Goulding_Rice-Do small businesses still prefer community banks-JBF2014