Schizophrenia (object-oriented programming)
Object schizophrenia or self schizophrenia is a complication arising from delegation and related techniques in object-oriented programming, where
self/this can refer to more than one object. By way of metaphor with the public confusion of dissociative identity disorder with the psychiatric diagnosis of schizophrenia, the former being associated with "split personalities," this configuration is called object schizophrenia or self schizophrenia in object-oriented programming.Overview
An object can be defined as a computing concept combining data and behavior, and having an identity. In class-based programming, objects are built on class systems, where an object is an instance of a class. Classes can in turn be related by inheritance: a base class provides the fundamental or default behavior of an object, and acts as a template for creating objects, while a derived class can be used to override behaviors of a base class, and can be used as a template for objects whose behaviors refine those of the base class.An alternative to classes and inheritance is prototypes and delegation, which is used in prototype-based programming, and is more dynamic. Instead of using one class to refine another, delegation allows one object to override the behavior of another. The original object a can delegate some of its methods to another object b. If a delegates its foo method to the bar method of b, then any invocation of foo on a will cause b's bar method to execute. However, bar executes in the context of the a object, for example, its
self identifier refers to a rather than to b.When delegation is used, the question arises: What is the value of
self when evaluating a method on object a, which is delegated to a method on object b? The identity is split: it can refer to a or b. Note that while the two objects are separate and have separate physical identity, self is ambiguous.