接口隔离原则(Interface Segregation Principle)ISP
using System;
using System.Collections.Generic;
using System.Text; namespace InterfaceSegregationPrinciple
{
//接口隔离原则(Interface Segregation Principle)ISP
//Clients should not be forced to depend upon interfaces that they don's use.(客户端不应该依赖它不需要的接口)
//The dependency of one class to another one should depend on the smallest possible interface.(类间的依赖关系应该建立在最小的接口上)
//单一指责要求的是类和接口的指责单一,注重的是指责,这是业务逻辑的划分。而接口隔离原则要求接口方法尽量少。提供给几个模块就应该有几个接口,而不是建立一个庞大臃肿的接口,容纳所有的客户访问。
class Program
{
static void Main(string[] args)
{
BluetoothMouse mouse = new BluetoothMouse();
USBMouse usbmouse = new USBMouse();
}
} //蓝牙鼠标类,继承了鼠标接口,蓝牙接口
public class BluetoothMouse : IMouse, IBluetooth
{ public void LeftClick()
{
throw new NotImplementedException();
} public void RightClick()
{
throw new NotImplementedException();
} public void Move()
{
throw new NotImplementedException();
} public void BluetoothConnect()
{
throw new NotImplementedException();
}
} //USB标类,继承了鼠标接口,USB接口
public class USBMouse : IMouse, IUSB
{ public void LeftClick()
{
throw new NotImplementedException();
} public void RightClick()
{
throw new NotImplementedException();
} public void Move()
{
throw new NotImplementedException();
} public void USBConnect()
{
throw new NotImplementedException();
}
} //鼠标的接口,对于鼠标本身来说,是肯定包含这左右点击,移动三个方法的
//至于蓝牙连接,还是USB连接,这是外部的连接方式,应该隔离开来(这样其他设备也可以继承这些接口,防止代码冗余复杂),而不是全都写在IMouse这个接口里面
//有人可能会想,move的方式有红外和滑轮这两种方式,是不是应该move也提取出接口隔离出来?
//这需要具体分析,在这个案例里是没有必要的,从鼠标类应用的层面看,move是鼠标内部的方法,并没有外部对接,就算提取出来接口,在这个应用层面也没有用处,只会增加复杂度。
//如果我们业务要更细化鼠标类(例如模拟生产制造鼠标),深入各个鼠标的模块组件,可能就有必要将move的接口隔离。
public interface IMouse
{
void LeftClick();
void RightClick();
void Move(); //应该将下面两个接口隔离,因为他们是外部接口
//如果不隔离,有些鼠标明明只支持USB连接,你还必须实现一个空的蓝牙连接方法?
//void BluetoothConnect();
//void USBConnect(); } public interface IBluetooth
{
void BluetoothConnect();
} public interface IUSB
{
void USBConnect();
}
}
接口隔离原则(Interface Segregation Principle)ISP的更多相关文章
- 接口隔离原则(Interface Segregation Principle, ISP)
使用多个专门的接口,而不使用单一的总接口 接口隔离有两种定义: Clients should not be forced to depend upon interfaces that they don ...
- 设计模式六大原则(四):接口隔离原则(Interface Segregation Principle)
接口隔离原则(ISP)定义: 客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来: 类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不 ...
- 面象对象设计原则之四:接口隔离原则(The Interface Segregation Principle,ISP)
接口隔离原则定义如下: 接口隔离原则(Interface Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 根 ...
- 设计模式原则(4)--Interface Segregation Principle(ISP)--接口隔离原则
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.定义: 使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 2.使用场景: 类A ...
- 【面向对象设计原则】之接口隔离原则(ISP)
接口隔离原则(Interface Segregation Principle, ISP):使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 从接口隔离原则的定义可以看 ...
- 设计模式之六大原则——接口隔离原则(ISP)
设计模式之六大原则——接口隔离原则(ISP) 转载于:http://www.cnblogs.com/muzongyan/archive/2010/08/04/1792528.html 接口隔离原则 ...
- 设计模式值六大原则——接口隔离原则 (ISP)
接口隔离原则 Interface Segregation Principle 定义: 客户端不应该依赖它不需要的接口 类间的依赖关系应该建立在最小的接口上 我们可以把这两个定义概括为一句话:建立 ...
- Java设计原则—接口隔离原则(转)
接口隔离原则 Interface Segregation Principle 定义: 客户端不应该依赖它不需要的接口 类间的依赖关系应该建立在最小的接口上 我们可以把这两个定义概括为一句话:建立 ...
- 接口分离原则(Interface Segregation Principle)
接口分离原则(Interface Segregation Principle)用于处理胖接口(fat interface)所带来的问题.如果类的接口定义暴露了过多的行为,则说明这个类的接口定义内聚程度 ...
- 接口隔离原则(ISP)
设计应用程序的时候,如果一个模块包含多个子模块,那么我们应该小心对模块做出抽象.设想该模块由一个类实现,我们可以把系统抽象成一个接口.但是要添加一个新的模块扩展程序时,如果要添加的模块只包含原系统中的 ...
随机推荐
- 编写高质量代码改善C#程序的157个建议——建议15: 使用dynamic来简化反射实现
建议15: 使用dynamic来简化反射实现 dynamic是Framework 4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译器默认dy ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- springcloud 实现微服务间调用
package com.idoipo.ibt.config; import org.apache.http.HttpException; import org.apache.http.HttpRequ ...
- 2014-4-2解决无法访问github和google的问题
github是个好地方,但是上不去就蛋疼了. 今天github上不去,果断f12下,看下network,发现里面好多请求都是指向 github.global.ssl.fastly.net这个域名的,然 ...
- TSQL--时间类型和毫秒数转换
项目中使用BIGINT来存放时间,以下代码用来转换时间类型和BIGINT类型 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ========= ...
- C# Path类常用方法
Path 类 对包含文件或目录路径信息的 String 实例执行操作. 1.Path.GetExtension 方法 —— 返回指定的路径字符串的扩展名. public static string G ...
- 趣图:后端工程师是怎样调试CSS的
一大波趣图:CSS的力量 趣图:前端 VS 后端
- MySQL索引的索引长度问题
转自:http://samyubw.blog.51cto.com/978243/223773 MySQL的每个单表中所创建的索引长度是有限制的,且对不同存储引擎下的表有不同的限制. 在MyISAM表中 ...
- LOSKI,我
2019年入驻github以及博客园 在发现用github的issue写博客稍微有些奇怪后决定开辟这个更适合写博的空间 2019/4/1 目前大一,计算机专业,尚未分流 更多的时间花在了数据结构与算法 ...
- how to use windows azure market
here is the sample. namespace USCrime2006and2007 { class Program { static void Main(string[] args) { ...