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. java项目中读取src目录下的文件

    private void getUser(String tmpfile){ Properties props = new Properties(); props.load(DbTask.class.g ...

  2. ThinkPHP中initialize和construct调用父类的区别

    http://blog.topok.net/archives/142 需要加parent::_initialize();

  3. MyEclipse反编译Class文件

    对于需要查看Java Class文件源码的筒子们来说,必须在项目中导入Java源码才能查看Class文件的具体实现,这不仅十分的麻烦,因为有时我们并不可以获得Class文件对应的Java源码.今天就给 ...

  4. TTL值的含义以及与域名DNS TTL值的区别

    TTL值的含义以及与域名TTL值的区别 本文来源于时光漂流瓶 http://www.9usb.net , 原文地址: http://www.9usb.net/201004/ttl-yuyuming-t ...

  5. Java中如何防止内存泄漏的发生

    在Java开发中我们常常会遇到内存泄漏的情况发生.那么为什么会发生内存泄漏,以及怎样去防止! 内存泄漏的定义:对象已经没有被应用程序使用,但是垃圾回收器没办法移除它们,因为还在被引用着. 为什么会发生 ...

  6. Java学习第四篇:数组,排序,查找

    一.数组 1.一维数组 (1).数组的定义 数据类型 数组名[]=new 数据类型[大小] public class Demo1 { public static void main(String[] ...

  7. PhotoshopCS4轻松将PSD分层导出为Png分层

    大家在网上都下载过PSD分层素材,有时候想把素材分层导出,以往做法是一层一层导出,可是这样效率太低,其实利用PS自带功能可以轻松导出PNG分层.     我们先打开一个PSD文件. 文件有7个图层,分 ...

  8. Unity3D脚本中文系列教程(二)

    原地址:http://dong2008hong.blog.163.com/blog/static/469688272014030347910/ Unity3D脚本中文系列教程(一) .根据名称或标签定 ...

  9. CentOS下使用cmake编译安装mysql

    一.下载安装所必需的依赖包 1.因为高版本mysql都用cmake安装,所以下载cmake wget http://www.cmake.org/files/v3.0/cmake-3.0.1.tar.g ...

  10. shop++ 安装

    1.安装tomcat后 ,G:\apache-tomcat-6.0.35\conf\server.xml 中设置tomcat 编码为utf-8. 增加URIEncoding = "UTF-8 ...