When to Use Delegates Instead of Interfaces
2008-08-23 16:07
417 查看
reference from MSDN:
When to Use Delegates Instead of Interfaces (C# Programming Guide)
Both delegates and interfaces allow a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct; a delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?
Use a delegate when:
An eventing design pattern is used.
It is desirable to encapsulate a static method.
The caller has no need access other properties, methods, or interfaces on the object implementing the method.
Easy composition is desired.
A class may need more than one implementation of the method.
Use an interface when:
There are a group of related methods that may be called.
A class only needs one implementation of the method.
The class using the interface will want to cast that interface to other interface or class types.
The method being implemented is linked to the type or identity of the class: for example, comparison methods.
One good example of using a single-method interface instead of a delegate is IComparable or IComparable. IComparable declares the CompareTo method, which returns an integer specifying a less than, equal to, or greater than relationship between two objects of the same type. IComparable can be used as the basis of a sort algorithm, and while using a delegate comparison method as the basis of a sort algorithm would be valid, it is not ideal. Because the ability to compare belongs to the class, and the comparison algorithm doesn’t change at run-time, a single-method interface is ideal.
When to Use Delegates Instead of Interfaces (C# Programming Guide)
Both delegates and interfaces allow a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct; a delegate can created for a method on any class, as long as the method fits the method signature for the delegate. An interface reference or a delegate can be used by an object with no knowledge of the class that implements the interface or delegate method. Given these similarities, when should a class designer use a delegate and when should they use an interface?
Use a delegate when:
An eventing design pattern is used.
It is desirable to encapsulate a static method.
The caller has no need access other properties, methods, or interfaces on the object implementing the method.
Easy composition is desired.
A class may need more than one implementation of the method.
Use an interface when:
There are a group of related methods that may be called.
A class only needs one implementation of the method.
The class using the interface will want to cast that interface to other interface or class types.
The method being implemented is linked to the type or identity of the class: for example, comparison methods.
One good example of using a single-method interface instead of a delegate is IComparable or IComparable. IComparable declares the CompareTo method, which returns an integer specifying a less than, equal to, or greater than relationship between two objects of the same type. IComparable can be used as the basis of a sort algorithm, and while using a delegate comparison method as the basis of a sort algorithm would be valid, it is not ideal. Because the ability to compare belongs to the class, and the comparison algorithm doesn’t change at run-time, a single-method interface is ideal.
相关文章推荐
- When to Use Delegates Instead of Interfaces (C# Programming Guide)
- When to Use Delegates Instead of Interfaces
- When to Use Delegates Instead of Interfaces
- When to use Tomcat CATALINA_OPTS instead of JAVA_OPTS - See more at: http://www.tikalk.com/java/when
- Know When to Use an Active Object Instead of a Mutex
- 干活来袭:Effective Concurrency: Know When to Use an Active Object Instead of a Mutex
- When to Use Bar Charts Instead of Pie Charts
- When to Use Object-oriented Programming(Chapter 5 of Python 3 Object Oriented Programming)
- How to use USB to do charger detection instead of PMIC?
- How to get a notification from Linux when the set of network interfaces changes
- When to use Class.isInstance() & when to use instanceof operator?
- When to use comparable and comparator interfaces in java
- When would someone use Apache Tez instead of Apache Spark, or vice versa?
- [ES6] When should use Map instead of Object
- An effective way to use pattern to instead of multiple if else statements
- 5 Reasons to Use Protocol Buffers Instead of JSON For Your Next Service
- how to use Decimal by default instead of float in python
- Describe 3 kernel functions and when to use which of them
- Words to Use Instead of "Very"
- How to Use Homebrew Zsh Instead of Mac OS X Default