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. ASP.NET MVC NonActionAttribute使用说明

    默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法. 如果您的 controller 类包含公共方法,并且您不希望它成为操作方法,则必须用 NonActionAttrib ...

  2. 处理一则MySQL Slave环境出现ERROR 1201 (HY000): Could not initialize master info structure的案例

    mysql> start slave; ERROR (HY000): Slave failed to initialize relay log info structure from the r ...

  3. 常用git 命令

    1.取消跟踪某些文件或文件夹: 删除文件: $git rm --cached FILENAME 删除文件夹: $git rm -r --cached Path 2.忽略某些文件或文件夹 $vi .gi ...

  4. SqlServer 系统存储过程

    exec sp_databases; --查看数据库exec sp_tables; --查看表exec sp_columns Categories;--查看列exec sp_helpIndex Cat ...

  5. Asp.Net原理Version1.0

    Asp.Net原理Version2.0 Asp.Net原理Version3.0_页面声明周期

  6. js异步加载 defer和async 比较

    网上说法很多,很少一句话能总结清楚的,终于找到两句一针见血的描述,很到位: 相同点:都不阻塞DOM解析 defer  :顺序:保证先后顺序.解析:HTML 解析器遇到它们时,不阻塞(脚本将被异步下载) ...

  7. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  8. 几款实用的 JavaScript 图形图表库

    一款好的图表插件不是那么容易找到的.最近项目里需要实现统计图表功能,所以在网上搜罗了一圈,找到一些不错的图表插件,分享大家.众多周知,图形和图表要比文本更具表现力和说服力.这里给大家精心推荐几款实用的 ...

  9. XEE介绍

    摘要: XMl Entity Expansion(攻击)某种程度上类似于 XML Entity Expansion,但是它主要试图通过消耗目标程序的服务器环境来进行DOS攻击的.这种攻击基于XML E ...

  10. Mutex vs Semaphore

    What are the differences between Mutex vs Semaphore? When to use mutex and when to use semaphore? Co ...