In GoF's book "Java and design patterns", we will not talk about the importance of the Delegate in. Net.
Compared with Java, a better improvement of. Net is to provide a better model for the event. Among them, the first hero is Delegate. So what is the role of Delegate?
In short, Delegate is a function interface + a function list.
Function interfaces can be said to be the idea in function programming (hereinafter referred to as FP) (I recently learned FP ). The core idea of FP is that a function is a first-class citizen and can be used as a value or variable. Let's look back. net is mainly oriented to the OOP framework, but it does also absorb the core idea of FP, in. net, the function cannot be used as a parameter, but the function can be converted into a Delegate, and then the Delegate is used as a parameter. Therefore, high-order functions in FP (namely, input or output functions) and Lambda expressions are all possible. How to define an interface in a world where functions are king? The function interface is its return value and its parameters. Think about how similar a Delegate is to it. To define a Delegate, you only need to return values and parameters.
Because of the existence of the Delegate function interface, the observer mode in. Net is not necessary and an interface is no longer needed because there is a function interface.
At the same time, the existence of the Delegate function interface can completely simplify many logics. The main example is void List <T>. sort (Comparison <T> comparison), which encapsulates a fast sorting method. However, this method allows injection of a relatively large function. Therefore, when a user-defined hour is required, you only need to write a method to compare the size of the data, instead of writing a sorting method from the beginning, because the quick sorting method encapsulated in List <T> can be shared completely.
Back to the topic, with Delegate, we can develop a new. Net mode that is popular in Java to simplify some code, but we can continue to maintain the open and closed principles of the mode.
The observer mode is omitted. I believe not many people use the observer mode in. Net.
One of the following advantages and disadvantages is omitted: The execution efficiency of virtual methods is higher than that of Delegate.
-------------------- The following is purely original and the names are all random ----------------------
1. Simple factory mode --> Delegate simple factory mode:
When there is only one method in IProduct of a simple factory product, it can be replaced by a factory that returns a Delegate.
Advantages: 1. You do not need to write an IProduct interface, instead of a Delegate interface.
Disadvantages: 1. You can add other functions to IProduct, but not to Delegate.
2. There is only one visitor mode-> simplified visitor mode:
When the visitor mode only needs to process one class, the visitor mode can be simplified as follows: Interface IVisitable <T>
{
Void Visit (Action <T> action );
}
Advantage: The IVisitor interface is not required
Disadvantage: it can only be used for a single class
...
----------------------------------- Summary ---------------------------------
In short, when an interface only exists for a method, it can be replaced by a Delegate, because the Delegate itself can be understood as a function interface.