An object, in its simplest form, is simply a collection of states and functions that work in this state. Closing is also a collection of states and a function that works in this state.
Let's say I call a function that performs a callback. In this callback, I need to operate on some state known before the function call. I can create an object that embodies this state ("fields") and contains a member function ("method") that performs a callback function. Or, I could take a quick and easy ("poor man") route and create a closure.
Like an object:
class CallbackState{ object state; public CallbackState(object state){this.state = state;} public void Callback(){
This is pseudo-C #, but in a different way similar to other OO languages. As you can see, there are enough templates associated with the callback class to manage the state. This would be a lot easier with closure:
void Foo(){ object state = GenerateState(); PerformOperation(()=>{}); }
This is a lambda (again, in C # syntax, but the concept is similar to other languages ββthat support closure), which gives us all the features of a class without the need to write, use and maintain a separate class.
You will also hear the consequence: "objects are poor people." If I cannot or will not use closure, then I am forced to do my work using objects, as in my first example. Although objects provide more functionality, closing is often the best choice when closing will work for reasons already stated.
Therefore, a poor person without objects can often do work with closure, and a poor person without closure can do his job using objects. A rich person has both, and uses the right one for each task.
P Daddy Mar 23 '10 at 6:12 2010-03-23 ββ06:12
source share