Are .net delegating real objects?

I have been working in C # for several years, and one thing always scares me. Delegates They have no state, they are pointers to methods of objects, and the only way to go when working with threads or any other parallel programming method that .NET can offer.

My main problem is that when programming in an object-oriented style, it is a fact that delegates break the paradigm, violating the notion that if you go Object Oriented, the design phase using UML and CRC should be displayed in the language you use.

Thus, as the name asks, real objects or an anomaly of programming are delegated, what needs to be done outside of the object-oriented design.

Is it possible for an object to be an object if it has no state and can a delegate be modeled in UML?

A famous person once gave a public lecture on astronomy. He described how the earth revolves around the sun and how the sun, in turn, revolves around the center of a huge collection of stars called our galaxy. At the end of the lecture, a little old woman in the back of the room stood up and said: “What you told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise.” The scientist gave an excellent smile before answering: "What is the tortoise?" “You are very smart, young man, very smart,” the old woman said. "But it's turtles all the way down! ---- Stephen Hawkins

I think this applies very well to the objects and the basic types on which they sit. Can you really say that his objects are completely omitted?

+3
source share
5 answers

These are real objects.

When you model UML, it is common to use interfaces. Do they have a condition? No. Are they less object oriented? Of course not.

Of course, a delegate can be modeled in UML.

I think they represent a more functional approach, because these are functions that are first class objects. It may concern you, but it does not detract from the fact that they really are objects.

+16
source

, , - . , " " .

Action @ICanHazState()
{
    int callcount = 0;
    Action retval = delegate {
       System.MessageBox("I haz bin calld " + callcount.ToString() + " tims b4");
       if (++callcount > 5) throw new InvalidOperationException("Call limit exceeded.  Kaboom!");
    };
    return retval;
}

( # 4, Visual Studio 2010)

+7

wikipedia ( (): - ( ) ( ), . Delegate .NET. Delegate, , (.. ): Method Target. (): , CreateDelegate .

- , 'this', . , , ( ), .

, .

Q.E.D.

+3

. - . ;)

+2

"".

, , "" .

By this definition, delegates are not objects. However, I believe that the definition is extremely, extremely limited. Entire categories of “objects” in real applications do not meet this definition. Functors, for starters. Many types of decorators will also fail by this definition, as well as things like filter objects.

If you go back to the Smalltalk, for example, objects true, and falseare objects. By your definition, they will not.

+2
source

Source: https://habr.com/ru/post/1770218/


All Articles