When to Use Delegates Instead of Interfaces
2013-02-01 17:10
337 查看
Both delegates and interfaces enable 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 be 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 that has 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
it use an interface?
Use a delegate in the following circumstances:
An eventing design pattern is used.
It is desirable to encapsulate a static method.
The caller has no need to 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 in the following circumstances:
There is 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.
be inherited and implemented by any class or struct. A delegate can be 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 that has 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
it use an interface?
Use a delegate in the following circumstances:
An eventing design pattern is used.
It is desirable to encapsulate a static method.
The caller has no need to 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 in the following circumstances:
There is 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.
相关文章推荐
- 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 Bar Charts Instead of Pie Charts
- 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
- 导入android sdk samples工程报错"did you mean to use @+id instead of @+android:id?"
- DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command f
- Words to Use Instead of "Very"
- When to use Class.isInstance() & when to use instanceof operator
- Use jQuery to hide a DIV when the user clicks outside of it
- Describe 3 kernel functions and when to use which of them
- When to use comparable and comparator interfaces in java
- How to get a notification from Linux when the set of network interfaces changes
- attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using
- Is it possible to use a checkbox in a split button list instead of the gear icon?
- Use Exception.ToString() instead of Exception.Message.
- 5 Reasons to Use Protocol Buffers Instead of JSON For Your Next Service
- An effective way to use pattern to instead of multiple if else statements