Another (not strictly object-oriented)technique for reusing functionality is through parameterized types, also known asgenerics (Ada, Eiffel) and templates (C++). This technique lets you define atype without specifying all the other types it uses. The unspecified types aresupplied as parameters at the point of use. For example, a List class can beparameterized by the type of elements it

contains. To declare a list of integers,you supply the type "integer" as a parameter to the List parameterized type. To declarea list of String objects, you supply the "String" type as a parameter.The language implementation will create a customized version of the List classtemplate for each type of element.

重用功能体的其他技术是参数化类型,也被称作泛型或模板。这种技术让你定义了一种指定的不是现在在用的类型。这个没有指定的类型在使用的时候通过提供参数

来弥补。例如,链表类可以通过它包含的元素类型被参数化。为了声明一个整形的链表,你指定了整形作为链表参数化的类型。为了声明一个字符串类型的链表对象,你指定了链表作为参数化的类型。语言的实现将会为每一个类型的元素创造客户指定的链表类模板。

Parameterized types give us a third way (inaddition to class inheritance and object composition) to compose behavior inobject-oriented systems. Many designs can be implemented using any of these threetechniques. To parameterize a sorting routine by the operation it uses to compareelements, we could make the comparison

1. an operation implemented by subclasses(an application of Template Method

(360),

2. the responsibility of an object that'spassed to the sorting routine

(Strategy (349), or

3. an argument of a C++ template or Adageneric that specifies the name of

the function to call to compare theelements.

在面向对象系统里,参数化类型给了我们第三种方式来构建行为(除了类的继承和对象的组合)。很多设计可以用这三种技术实现。我们可以做一个比较:

(1)通过子类来实现运算。(360页有一个应用的模板方法)

(2)对象的职责是保留了其储存路线。

(3)c++模板参数或ada 的泛型指定了被函数调用的元素的名字。

There are important differences betweenthese techniques. Object composition lets you change the behavior being composed atrun-time, but it also requires indirection and can be less efficient.Inheritance lets you provide default implementations for operations and letssubclasses override them. Parameterized types let you change the types that a classcan use. But neither inheritance nor

parameterized types can change at run-time.Which approach is best depends on your design and implementation constraints.

下面是这三种技术重要的不同。对象的组合会让你在运行时改变被组合的对象的行为。他也让命令变得间接和低效。继承提供了默认的运算的实现或让子类覆盖的实现。参数化类型会让你用可用的类型来改变类的类型。但是继承和参数化类型不能在运行时被改变。到底用那种方法来处理依靠你设计和实现的约束。

None of the patterns in this book concerns parameterized types, though we use them on occasion to customize a pattern's C++ implementation. Parameterized types aren't needed at all in a language like Smalltalk that doesn't have compile-time type checking

在这本书里没有一种模式关心参数化类型,尽管我们会用它在客户指定用c++模式实现的时候。像Smalltalk这些语言在一点也不需要在编译时不做类型检查的参数化类型的。

设计模式之Inheritance versus Parameterized Types 继承和参数化类型的更多相关文章

  1. Syntax error, parameterized types are only available if source level is 1.5 解决方案

    在网上找了一个K-means算法的程序,打开,运行,出现了Syntax error,parameterized types are only available if source level is ...

  2. Head First 设计模式--1策略模式 组合优于继承

    策略模式:第一了算法族,分别封装起来,让他们之间可以互相替换,次模式让算法的变化独立于使用算法的客户. 首先看个错误的面向对象. 假如我们需要写一个关于鸭子的程序,各种类型的鸭子.第一想到的就是建一个 ...

  3. Chapter 17. Objects and Inheritance(对象与继承)

    javascript面向对象编程有几个层面: 1: 单一对象 (covered in Layer 1: Single Objects) 2: 对象之间的 prototype  (described i ...

  4. C# 泛型约束

    一.泛型简介1.1泛型通过使用泛型,可以创建这样的类.接口和方法,它们以一种类型安全的工作方式操作各种数据.本质上,术语“泛型”指的是“参数化类型”(parameterized types).参数化类 ...

  5. Java面试16|设计模式

    1.单例模式: 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下几个要素: 私有的构造方法 指向自己实例的私有静态引用 以自己实例为返回值的静态的公有的方法 单例模式根 ...

  6. Java核心技术卷一基础知识-第5章-继承-读书笔记

    第5章 继承 本章内容: * 类.超类和子类 * Object:所有类的超类 * 泛型数组列表 * 对象包装器和自动装箱 * 参数数量可变的方法 * 枚举类 * 反射 * 继承设计的技巧 利用继承,人 ...

  7. (C/C++学习笔记) 十八. 继承和多态

    十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...

  8. 28-React state提升、组件组合或继承

    Lifting State Up state提升 对于在React应用程序中更改的任何数据,应该有一个单一的数据源.通常,都是将state添加到需要渲染的组件.如果其他组件也需要它,您可以将其提升到最 ...

  9. Java设计模式--模板方法模式

    定义: 模板模式是一种行为设计模式,使用了JAVA的继承机制,在抽象类中定义一个模板方法,该方法引用了若干个抽象方法(由子类实现)或具体方法(子类可以覆盖重写).它的实现思路是,创建一个桩方法,并且定 ...

随机推荐

  1. Win7下配置nginx和php5

    本文链接:http://www.cnblogs.com/cnscoo/archive/2012/09/03/2668577.html 一.准备工作: OS:Windows7 SP1 Nginx: ng ...

  2. (菜鸟要飞系列)四,基于Asp.Net MVC5的后台管理系统(zTree绑定Json数据生成树)

    上一次老师让我们用递归将中国城市镇县四级联动 显示在树上,那个时候就知道可以显示在zTree上,可是苦于对Json的不了解,对zTree的Api的不了解,一直没有做出来,只好将递归算法显示在了窗体上, ...

  3. c++程序开发利器

    c++程序开发利器 vc6visual stdio系列都很好,个人最喜欢vc6,主要原因是快捷,classview和wizardbar功能强大,其他vs秒杀其他的vs windbgWinDbg是在wi ...

  4. Linux - 升级+编译kernel

    For upgrading present kernel to linux-next kernel, we need to follow below steps. 1. Check present k ...

  5. Team Homework #3: The feedback of predecessors

    此次对学长的采访主要在QQ上进行,感谢陈宇宁学长的热情配合. 采访学长的问题及学长的答复如下: 1. 平均每周花在这门课上的时间 (包括上课/作业/上机) -大约15-20小时吧(学长个人花费时间) ...

  6. BZOJ 3436: 小K的农场 差分约束

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c  ->  b<=a-c ...

  7. 框架优化系列文档:SVN中非版本控制文件忽略上传的设置

    对于SVN代码库,只应该上传源代码.资源文件等内容进行版本管理,通常编译后的二进制文件.程序包等生成产物是不应该放到SVN上做版本管理的.因此在svn的客户端工具中设置svn的属性:svn:ignor ...

  8. 【树形DP/搜索】BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会

    1827: [Usaco2010 Mar]gather 奶牛大集会 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 793  Solved: 354[Sub ...

  9. 关于iOS中的文本操作-管理text fields 和 text views

    Managing Text Fields and Text Views 管理UITextField和UITextView实例 UITextField和UITextView的实例拥有两个最主要的功能:展 ...

  10. POJ3764 The xor-longest path Trie树

    代码写了不到30分钟,改它用了几个小时.先说题意,给你一颗树,边上有权,两点间的路径上的路径的边权抑或起来就是路径的xor值,要求的是最大的这样的路径是多少.讲到树上的两点的xor,一个常用的手段就是 ...