设计模式之Inheritance versus Parameterized Types 继承和参数化类型
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 继承和参数化类型的更多相关文章
- 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 ...
- Head First 设计模式--1策略模式 组合优于继承
策略模式:第一了算法族,分别封装起来,让他们之间可以互相替换,次模式让算法的变化独立于使用算法的客户. 首先看个错误的面向对象. 假如我们需要写一个关于鸭子的程序,各种类型的鸭子.第一想到的就是建一个 ...
- Chapter 17. Objects and Inheritance(对象与继承)
javascript面向对象编程有几个层面: 1: 单一对象 (covered in Layer 1: Single Objects) 2: 对象之间的 prototype (described i ...
- C# 泛型约束
一.泛型简介1.1泛型通过使用泛型,可以创建这样的类.接口和方法,它们以一种类型安全的工作方式操作各种数据.本质上,术语“泛型”指的是“参数化类型”(parameterized types).参数化类 ...
- Java面试16|设计模式
1.单例模式: 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式有以下几个要素: 私有的构造方法 指向自己实例的私有静态引用 以自己实例为返回值的静态的公有的方法 单例模式根 ...
- Java核心技术卷一基础知识-第5章-继承-读书笔记
第5章 继承 本章内容: * 类.超类和子类 * Object:所有类的超类 * 泛型数组列表 * 对象包装器和自动装箱 * 参数数量可变的方法 * 枚举类 * 反射 * 继承设计的技巧 利用继承,人 ...
- (C/C++学习笔记) 十八. 继承和多态
十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...
- 28-React state提升、组件组合或继承
Lifting State Up state提升 对于在React应用程序中更改的任何数据,应该有一个单一的数据源.通常,都是将state添加到需要渲染的组件.如果其他组件也需要它,您可以将其提升到最 ...
- Java设计模式--模板方法模式
定义: 模板模式是一种行为设计模式,使用了JAVA的继承机制,在抽象类中定义一个模板方法,该方法引用了若干个抽象方法(由子类实现)或具体方法(子类可以覆盖重写).它的实现思路是,创建一个桩方法,并且定 ...
随机推荐
- 部署keepalived
下载 keepalived-1.1.20.tar.gz tar -xvf keepalived-1.1.20.tar.gz [root@yoon export]# cd keepalived-1. ...
- 在后台直接调用sql
表:FmCashStatistics EFContext db = new EFContext(); string strsql = "SELECT org_info_id,customer ...
- LintCode-Search 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of ...
- ajax正确返回数据,却进入了error分支
.net 开发: $.ajax({ type: "POST", //post没有数据量限制 url: "ashx/PostHandle.ashx", data: ...
- 在MAC上安装虚拟机搭建Ubuntu开发环境
由于工作需要,需要在LINUX环境搭建服务器,但是工作中使用的是MAC系统,只好用虚拟机来搭建LINUX服务器环境.下面记录介绍一下搭建步骤以供需要的人参考使用. 下载准备 虚拟机使用VMWare ...
- 【转】使用JIRA搭建企业问题跟踪系统【个人推荐】
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:Judy Shen的专栏 原文地址:使用JIRA搭建企业问题跟踪系统 ...
- oracle——外连接查询
一.问题描述 有时我们为了保留某个表中的数据,而该表中的数据在另外一个关联表中未必都存在对应,此时就应该试用外连接查询. 比如:两个表,产品表和子产品表 注:子产品的parent_product_id ...
- Aizu 2325 Mysterious Maze
走迷宫 ~ 不同的是题目给了你转向的方向序列 dis[x][y]表示到(x,y) 使用了最少的转向次数 #include<cstdio> #include<cstring> # ...
- uva 10205 模拟
模拟题 题目描述挺长的.... #include <cstdio> #include <cstdlib> #include <cmath> #include < ...
- 国内最大的 Node.js 社区将 New Relic 的监控产品换成了 OneAPM
国内最知名的 CNode 社区把 New Relic 的监控产品换成了 OneAPM .难道 APM 的老大 New Relic 已经被 OneAPM 超越? 毋庸置疑,在全球应用性能管理 SaaS ...