设计模式之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 ...
随机推荐
- Knockout.js(四):自定义绑定
除了内嵌的绑定,还可以创建一些自定义绑定,封装复杂的逻辑或行为. 注册绑定 添加子属性到ko.bindingHandlers来注册绑定: ko.bindingHandlers.yourBindingN ...
- NOIP 2018 提高组初赛解题报告
单项选择题: D 进制转换题,送分: D 计算机常识题,Python是解释运行的: B 常识题,1984年小平爷爷曰:“娃娃抓起”: A 数据结构常识题,带进去两个数据就可以选出来: D 历年真题没有 ...
- [BZOJ3203][SDOI2013]保护出题人(凸包+三分)
https://www.cnblogs.com/Skyminer/p/6435544.html 先不要急于转化成几何模型,先把式子化到底再对应到几何图形中去. #include<cstdio&g ...
- 入门cout输出的格式(补位和小数精度)
http://blog.csdn.net/gentle_guan/article/details/52071415 mark一下,妈妈再也不用担心我高精度不会补位了
- Tagging Problems & Hidden Markov Models---NLP学习笔记(原创)
本栏目来源于对Coursera 在线课程 NLP(by Michael Collins)的理解.课程链接为:https://class.coursera.org/nlangp-001 1. Taggi ...
- 【原创】Eclipse中Android项目引用
1.选择名为SMSSDK的项目,右键--->Properties--->Android--->将Is Library勾上--->OK. 2.选中另一个名为FragmentDem ...
- Android学习笔记(三)之带有侧边索引的快速查找(跟带字母索引查找的通讯录差不多)
喜欢另辟蹊径的我,在这里废话不多说了,直接上代码和图片了. 效果图如下: 第一步:MainActivity的代码如下: package net.loonggg.test; import java.ut ...
- 在w7下的wamp中配置memcache
php版本是5.4.16 ,我的电脑是w7 64位的. 一. memcache和memcached的区别 在自己的新程序中打算全面应用memcached技术,这个很容易理解这是memcached是内 ...
- TSL / SSL
参考: http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html http://www.tuicool.com/articles/IJ3 ...
- nodejs 服务器 崩溃 2种解决办法
用node启动server后,发现服务器不稳定,经常crash.我是用ssh远程登录的,ssh远程通道中断,或者Ctrl+C,都会使nodejs server崩溃掉. 一,node server 崩溃 ...