设计模式之Programming to an Interface, not anImplementation 程序指向接口,而不是实现
Class inheritance is basically just a mechanism for extending an application's functionality by reusing functionality in parent classes. It lets you define a new kind of object rapidly in terms of an old one. It lets you get new implementations almost for free, inheriting most of what you need from existing classes.
类的继承只是通过重用父类里的函数为延长应用函数提供了基础的机制。它会让你更快速的利用旧有的对象来快速定义新的对象。它让你几乎是很轻的得到新的实现。继承在多数情况下是从存在的类中得到你想要的。
However, implementation reuse is only half the story. Inheritance's ability to define families of objects with identical interfaces (usually by inheriting from an abstract class) is also important. Why?Because polymorphism depends on it.
然而,实现的重用只是这个故事的一半。用相同的接口定义家族式的对象的继承能力很重要。(通常通过抽象类来实现继承)为什么?因为多态依赖与它。
When inheritance is used carefully (some will say properly), all classes derived from an abstract class will share its interface. This implies that a subclass merely adds or overrides operations and does not hide operations of the parent class. All subclasses can then respond tothe requests in the interface of this abstract class, making them all subtypes of the abstract class.
当继承被谨慎的使用时(有的地方会说“恰当”),所有从抽象类衍生出来的类会共享这些接口。这纯粹是子类的增加或覆盖运算,而不是隐藏父类的运算。
There are two benefits to manipulating objects solely in terms of the interface defined by abstract classes:
1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect.
2. Clients remain unaware of the classesthat implement these objects. Clients only know about the abstract class(es)defining the interface.
根据抽象类定义的接口来操作对象有两个独一无二的好处
(1) 客户不会意识到他们所用的特殊类型的对象,像客户期待的那样只要对象遵循接口即可。
(2) 客户不会意识到实现对象的类。客户仅仅只是知道定义这个接口的抽象类。
This so greatly reduces implementation dependencies between subsystems that it leads to the following principle of reusable object-oriented design: Program to an interface, not an implementation.
这也大量减少了子系统之间实现的依赖,它导致了面向对象设计中的一下原则:程序指向接口,而不是实现。
Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class. You will find this to be a common theme of the design patterns in this book.
不声明变量是特定的具体类的实现。取而代之的是,通过抽象类定义的接口来实现调配。在这本书里,你会发现这是普遍的主题。
You have to instantiate concrete classes(that is, specify a particular implementation) somewhere in your system,of course, and the creational patterns(Abstract Factory (99), Builder (110),Factory Method (121), Prototype (133),and Singleton (144) let you do just that.By abstracting the process of object creation, these patterns give you differentways to associate an interface with
its implementation transparently at instantiation. Creational patterns ensure that your system is written in terms of interfaces, not implementations.
在系统的某些地方,你不得不去实例化具体的类(这些是特殊的实现)。
当然,创建模式和单体会让你做这些。通过抽象对象创建的进程,在实例化的过程中,这些模式会给你提供不同的易于觉察的方法来联系这些接口。创建模式
会保证在你的系统中的接口的安全,而不是实现。
设计模式之Programming to an Interface, not anImplementation 程序指向接口,而不是实现的更多相关文章
- API(Application Programming Interface,应用程序编程接口)
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...
- JNDI(Java Naming and Directory Interface,Java命名和目录接口)
JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访 ...
- 安卓操作系统版本(Version)与应用程序编程接口等级(Application Programming Interface Level)对照表
Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑. 使用Android API,可以在Java环境开发App,编译.打包后可在Android系统 ...
- 开始慢慢学习这本书了。。Python编程实战:运用设计模式、并发和程序库创建高质量程序
没办法,不到设计模式,算法组合这些,在写大一点程序的时候,总是力不从心...:( 一开始可能要花很多时间来慢慢理解吧,,这毕竟和<大话设计模式>用的C#语言有点不太一样... 书上代码是3 ...
- [UE4]Get All Widgets Of Class、Get All Widgets with Interface,根据类名或者接口UI实例对象
Get All Widgets Of Class.Get All Widgets with Interface,是系统蓝图函数库提供的方法,可以在任何蓝图中使用. 可以方便地获得UI实例对象,进而使用 ...
- C#中泛型方法与泛型接口 C#泛型接口 List<IAll> arssr = new List<IAll>(); interface IPerson<T> c# List<接口>小技巧 泛型接口协变逆变的几个问题
http://blog.csdn.net/aladdinty/article/details/3486532 using System; using System.Collections.Generi ...
- python经典书籍:Python编程实战 运用设计模式、并发和程序库创建高质量程序
Python编程实战主要关注了四个方面 即:优雅编码设计模式.通过并发和编译后的Python(Cython)使处理速度更快.高层联网和图像.书中展示了在Python中已经过验证有用的设计模式,用专家级 ...
- 设计模式 第一天 UML图,设计模式原则:开闭原则、依赖倒转原则、接口隔离原则、合成复用原则、迪米特法则,简单工厂模式
1 课程大纲 2 UML的概述 总结: UML unified model language 统一建模语言 一共有十种图: 类图 用例图 时序图 * 对象图 包图 组件图 部署图 协作图 状态图 (最 ...
- 匿名字段 内嵌结构体 interface作为struct field 匿名接口
interface作为struct field,谈谈golang结构体中的匿名接口 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/19 ...
随机推荐
- EventBus机制 handler 区别
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 事件巴士 是 采用 观察者模式 实现 事件订阅总线, 可以用在 应用程序中, 组件之间, ...
- Codeforces 388 D. Fox and Perfect Sets
$ >Codeforces \space 388 D. Fox and Perfect Sets<$ 题目大意 : 定义一个完美的集合 \(S\) ,当且仅当 \(S\) 非负非空,且 ...
- luoguP4457 [BJOI2018]治疗之雨 概率期望 + 高斯消元
应该是最后一道紫色的概率了....然而颜色啥也代表不了.... 首先看懂题意: 你现在有$p$点体力,你的体力上限为$n$ 在一轮中, 1.如果你的体力没有满,你有$\frac{1}{m + 1}$的 ...
- 【8.23校内测试】【贪心】【线段树优化DP】
$m$的数据范围看起来非常有问题??仔细多列几个例子可以发现,在$m<=5$的时候,只要找到有两行状态按位$&$起来等于$0$,就是可行方案,如果没有就不行. #include<i ...
- LCA POJ 1330 Nearest Common Ancestors
POJ 1330 Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24209 ...
- SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...
- 无法读取Excel中的数据单元格。有数据,但是读出来全是空值
C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...
- Linux下使用SSH远程执行命令方法收集
说明:可以做SSH免密登录之后执行,这样可以省去每次执行输入密码的提示. 对于简单的命令: 如果是简单执行几个命令,则: ssh user@remoteNode "cd /home ; ls ...
- 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法
今天发现电脑的系统时间不对,因此将时钟进行了改动,回头编译Linux kernel的时候,提演示样例如以下的warning: warning: Clock skew detected. Your ...
- loading(正在加载特效)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...