设计模式之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 ...
随机推荐
- Hibernate 双向一对多的关联映射
双向的一对多的关联关系是单项的一对多和单项的多对一的情况下产生的. 1.设计表结构 虽然关联关系变为双向的一对多,但是我们表结构不会发生改变,只是指向变了. 2.创建student对象 3.创建Gra ...
- 【动态规划/多重背包问题】POJ2392-Space Elevator
方法同POJ1014-Dividing,唯一不同点在于每一种block有最大限定高度a,故要以a为关键字进行排序,使得最大高度小的在前,否则最大高度小的再后可能放不上去. #include<io ...
- Spring Mvc 前台数据的获取、SpringMvc 表单数据的获取
首先在web.xml 里面配置一个编码过滤器 <!-- springmvc框架本身没有处理请求编码,我们自己配置一个请求编码过滤器 --> <filter> <filte ...
- 51NOD 1179 最大的最大公约数 筛法
1179 最大的最大公约数 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1179 Description ...
- hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序
DZY Loves Topological Sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...
- Linux下同一网段内的IP中两台主机通信不经过路由器(ARP)(转)
答案一:同一网段A与B通信,不需要路由器介入. A直接广播ARP request 到广播域,B处于同一广播域,可以接收到ARP request,B用单播方式直接告诉A自己的MAC B 地址.A收到B的 ...
- OC 导入类 #import和@class 区别复习
objective-c中#import和@class的区别 在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forwar ...
- Android Material Design-Working with Drawables(使用Drawable)-(五)
转载请注明出处:http://blog.csdn.net/bbld_/article/details/40584331 翻译自:http://developer.android.com/trainin ...
- How to implement *All-Digital* analog-to-digital converters in FPGAs and ASICs
When we engineers look at the complexity of system design these days, we are challenged with crammin ...
- NHibernate使用无状态Sessions
NHibernate 3.0 Cookbook第三章,Using stateless sessions的翻译. 当要处理大量的数据,你通常可能会使用更"底层"的API来改善性能,在 ...