理解:提取接口的意思是,多于一个类共同使用某个类中的方法或属性,那么我们可以把这些方法和属性提出来,作为一个单独的接口。这样的好处是解除代码间的依赖,降低耦合性。

详解

先看重构前的代码:

  public class ClassRegistration
{
public void Create()
{
// create registration code
} public void Transfer()
{
// class transfer code
} public decimal Total { get; private set; }
} public class RegistrationProcessor
{
public decimal ProcessRegistration(ClassRegistration registration)
{
registration.Create();
return registration.Total;
}
}

RegistrationProcessor 类只使用到了ClassRegistration 类中的Create方法和Total 字段,所以可以考虑把他们做成接口给RegistrationProcessor 调用。

重构后的代码:

  public interface IClassRegistration
{
void Create();
decimal Total { get; }
} public class ClassRegistration : IClassRegistration
{
public void Create()
{
// create registration code
} public void Transfer()
{
// class transfer code
} public decimal Total { get; private set; }
} public class RegistrationProcessor
{
public decimal ProcessRegistration(IClassRegistration registration)
{
registration.Create();
return registration.Total;
}
}

我们提取了一个IClassRegistration 接口,同时让ClassRegistration 继承此接口,然后调用端RegistrationProcessor就可以直接通过IClassRegistration 接口进行调用。

重构第9天:提取接口(Extract Interface)的更多相关文章

  1. 重构第17天提取父类(Extract SuperClass)

    今天的重构来自 Martin Fowler的http://refactoring.com/catalog/extractSuperclass.html. 理解:本文中的“提取父类”是指类中有一些字段或 ...

  2. 重构9-Extract Interface(提取接口)

    我们来介绍一个常常被忽视的重构:提取接口.如果你发现多于一个类使用另外一个类的某些方法,引入接口解除这种依赖往往十分有用.该重构实现起来非常简单,并且能够享受到松耦合带来的好处. public cla ...

  3. 抽象类(abstract class)和接口(Interface)的区别

    前言 抽象类(abstract class)和接口(Interface)是Java语言中对于抽象类定义进行支持的两种机制,赋予了Java强大的面向对象能力. 二者具有很大的相似性,甚至可以相互替换,因 ...

  4. 组件接口(API)设计指南[2]-类接口(class interface)

    *返回文件夹阅读其它章节: http://blog.csdn.net/cuibo1123/article/details/39894477 类接口(class interface) 你能够參考MGTi ...

  5. 探索typescript的必经之路-----接口(interface)

    TypeScript定义接口 熟悉编程语言的同学都知道,接口(interface)的重要性不言而喻. 很多内容都会运用到接口.typescrip中的接口类似于java,同时还增加了更灵活的接口类型,包 ...

  6. TypeScript之路----探索接口(interface)的奥秘

    TypeScript定义接口 要想掌握typescript的知识,接口是其必经之路.很多东西都需要接触到接口,接口除了对类的一部分行为进行抽象以外,也常用于对对象的形状进行描述.接下来我们就一起来学习 ...

  7. 面向对象编程语言中的接口(Interface)

    在大多面向对象的编程语言中都提供了Interface(接口)的概念.如果你事先学过这个概念,那么在谈到“接口测试”时,会不会想起这个概念来!?本篇文章简单介绍一下面向对象编程语言中的Interface ...

  8. 【PHP面向对象(OOP)编程入门教程】20.PHP5接口技术(interface)

    PHP与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父类.为了解决这个问题,PHP引入了接口,接口的思想是指定了一个实现了该接口的类必须实现的一系列方法.接口是一种特殊的抽象 ...

  9. 环回接口(loopback interface)的新认识

    背景 前些日子在IDC实验docker的时候,为了避免与公司网络冲突,将bridge设置为127.x网段的IP,原以为这样就OK,后来发现在访问container内部的服务的时候无法访问.开始以为ip ...

随机推荐

  1. libevent

    libevent doc example #include <event2/event.h> void cb_func(evutil_socket_t fd, short what, vo ...

  2. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  3. WPF快速精通版

    命名空间: xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:U ...

  4. Quartz 2D绘制简单图形

    在Quartz 2D中,绘图是通过图形上下文进行绘制的,以下绘制几个简单的图形 首先先创建一个QuartzView.swift文件继承自UIView,然后实现drawRect方法: import UI ...

  5. TypeError: Cannot read property 'root' of null

    解决办法: brew upgrade watchman

  6. 【AI】蒙特卡洛搜索树

    http://jeffbradberry.com/posts/2015/09/intro-to-monte-carlo-tree-search/ 蒙特卡洛方法与随机优化: http://iacs-co ...

  7. LoadRunner 12试用

    LoadRunner 12试用 http://blog.csdn.net/testing_is_believing/article/details/23611845

  8. npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案

    项目中一直使用NPOI与memcached,一直相安无事,但是最近升级了npoi到最新版本,发生了ICSharpCode.SharpZipLib的版本冲突问题. 因为此前一直使用的是NPOI的1.x的 ...

  9. iis 访问网站需要进行身份验证

    今天网站输入域名访问的时候提示需要输入账号密码,这是权限出了问题,百度了一下,解决了,分享一下: 1.登陆远程,右键我的电脑->管理->本地用户和组->用户,里面有一个IUSR_WD ...

  10. java判断乱码

    开发需要,判断乱码,baidu了一下,基本都是同一份代码 if (!Character.isLetterOrDigit(c)) {        ->  这个有问题,中文文字被识别成字母及数字 ...