[29] Learning C # (01) interface isolation, reflection

Interface Segregation Principle

Interface segregation principle: Interface Segregation Principle, ISP
definition:

  • The client should not rely on it does not interface
  • Dependencies between classes should be based on the smallest Interface

In other words: Do not put a lot of ways in which an interface, it would seem this class bloated. We should try to refine the interface, an interface module corresponds to a function, while the interface which method should be as small as possible to make the interface more lightweight and flexible.

But pay attention to distinguish Interface Segregation Principle [] and [] Single Responsibility Principle, has a very distinct difference between the two principles that the new perspective is different, single responsibility principle requires a single class interfaces and responsibilities, focusing on the responsibility, is divided on the business logic; and the interface isolation to principle requires as little as possible, it is considered in the interface design.

For example a duty interface contains methods 10, 10 which are placed in a method of interfaces, and access to a plurality of modules, each module in accordance with the authority to access, and provides for a "method can not be accessed without using the" this design is not consistent with the principles of the isolation interfaces, interfaces principle requires isolation "to make use of a plurality of special interface" herein refers to an interface dedicated to each module should be a single interface (i.e. the interface of each module corresponds to a ), instead of creating a bloated interface to accommodate all client access.

The consequences of violating the principle of isolating interfaces

1. class lead to the realization of the interface, while a single violation of the principle of duty

2. lead to a qualified provider in the door function

Example 1: On tanks girls

Girls driving on the road rear-end, and comforted her boyfriend, she said, do not cry, Gan Minger buy you a tank stand you on your way out!
(1) design mistakes, put too much functionality in one interface, the caller simply less than; and will feature qualified provider in the door
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
(2) in accordance with the principle of isolating interfaces, interfaces to large split into small Interface
Here Insert Picture Description
Here Insert Picture Description

Example 2: summing a set of integers

(1) to a summation of integers
Here Insert Picture Description

(2)ICollection,IEnumerable
Here Insert Picture Description
Here Insert Picture Description
只需要功能的提供者——传进求和函数的数组,具有能被迭代的功能即可,也就是说,只要求它实现【IEnumerable接口】,并不需要实现【ICollection接口】

(3)声明一个【ReadOnlyCollection】
声明【ReadOnlyCollection类】只读集合,它只实现【IEnumerable接口】,只能被迭代
Here Insert Picture Description
Here Insert Picture Description

(4)具体实现
Here Insert Picture Description
Here Insert Picture Description
(5)传入一个大接口
Here Insert Picture Description

显式地实现接口

显式地实现接口是C#所独有的功能,C#在接口隔离方面做得比其他任何一种语言都要出色;它不但能让我们做到接口隔离,还能把一些隔离出的接口隐藏起来,直到显式地用接口类型的变量,去引用一个实现了该接口的类的具体实例时,这个接口中的方法才能被我们所看到,所调用

例3:这个杀手不太冷

杀手走在街上,不能轻易地让别人看出他是一个杀手
Here Insert Picture Description
Here Insert Picture Description

反射

什么是反射?

反射机制指的是程序在运行时能够获取自身的信息,只要给定类的名字,就可以通过反射机制来获得类的所有信息
也就是说:给我一个对象,我在不知道它具体是什么静态类型,且不用new操作符的情况下,能创建出一个同类型的对象,还能访问对象的成员

为什么需要反射?

为什么要用反射机制?直接创建对象不就可以了吗,这就涉及到了动态与静态的概念

  • 静态编译:在编译时确定类型,绑定对象,即通过。

  • 动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多态的应用,以降低类之间的藕合性。

  • 反射机制的优点就是可以实现动态创建对象和编译,体现出很大的灵活性

    For example, a large-scale software, it is impossible to put a design is perfect, when the program is compiled, published, found that when certain functions need to be updated, it is impossible for the user to the previous uninstall, then reinstall the new version, if so, this software is certainly not many people use. Static, then, need to recompile the entire program once only update function can be achieved, while the use of reflection, then it can not uninstall only need to dynamically create and compile when run, you can achieve this functionality.

  • The disadvantage is the performance impact.

Principles reflected

Here Insert Picture Description

Reference material

Interface Segregation Principle
Java reflection Comments

Published 32 original articles · won praise 3 · Views 1115

Guess you like

Origin blog.csdn.net/weixin_44813932/article/details/104089219