Важно

  •  

Saturday, September 01, 2012

Mixin and treats (English)

Formatting is mine.

Mixins are synonymous with abstract base classes. Inheriting from a mixin is not a form of specialization but is rather a means of collecting functionality. A class or object may "inherit" most or all of its functionality from one or more mixins, therefore mixins can be thought of as a mechanism of multiple inheritance...

...Mixins encourage code reuse and avoid well-known pathologies associated with multiple inheritance. However, mixins introduce their own set of compromises...

...When a class includes a mixin, the class...includes, rather than inherits, all the mixin's attributes (fields, properties) and methods. They become part of the class during compilation.
http://en.wikipedia.org/wiki/Mixin

There is more below.
Ниже есть продолжение.


With mixins the class definition defines only the attributes and parameters associated with that class; methods are left to be defined elsewhere, as in Flavors and CLOS, and are organized in "generic functions". These generic functions are functions that are defined in multiple cases (methods) by type dispatch and method combinations.

CLOS and Flavors allows mixin methods to add behavior to existing methods: :before and :after daemons, whoppers and wrappers in Flavors. CLOS added :around methods and the ability to call shadowed methods via CALL-NEXT-METHOD. So, for example, a stream-lock-mixin can add locking around existing methods of a stream class. In Flavors one would write a wrapper or a whopper and in CLOS one would use an :around method. Both CLOS and Flavors allow the computed reuse via method combinations. :before, :after and :around methods are a feature of the standard method combination. Other method combinations are provided.

...Some languages like ECMAScript (commonly referred to as JavaScript) do not support mixins on the language level, but can easily mimic them by copying methods from one object to another at runtime, thereby "borrowing" the mixin's methods. Note that this is also possible with statically typed languages, but it requires constructing a new object with the extended set of methods...

...Some of the functionality of mixins is provided by interfaces in popular languages like Java and C#. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for refactoring common behavior into a single place.

Interfaces combined with aspect-oriented programming can produce full fledged mixins in languages that support such features, such as C# or Java. Additionally, through the use of the marker interface pattern, generic programming, and extension methods, C# 3.0 has the ability to mimic mixins.
http://en.wikipedia.org/wiki/Mixin


A little sample in Scala Language:


class Point2D(xc: Int, yc: Int) {
   val x = xc;
   val y = yc;
   override def toString() =
  "x = " + x + ", y = " + y;
 }
 class ColoredPoint2D(u: Int, v: Int, c: String)
   extends Point2D(u, v) {
   var color = c;
   def setColor(newCol: String): Unit = color = newCol;
   override def toString() =
  super.toString() + ", col = " + color;
 }
 class Point3D(xc: Int, yc: Int, zc: Int)
   extends Point2D(xc, yc) {
   val z = zc;
   override def toString() =
  super.toString() + ", z = " + z;
 }
 class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
  extends Point3D(xc, yc, zc)
  with ColoredPoint2D(xc, yc, col);

And
new ColoredPoint3D(1, 2, 3, "blue").toString() 
would return
"x = 1, y = 2, z = 3, col = blue". 
http://c2.com/cgi/wiki?MixIn

Mixin can be viewed as partial realization of multiple inheritance. In the programming languages that supports multiple inheritance, mixin can be easily emulated. For example, in C++ template can be used for adding operator given operator «==»:

template  struct AddNoEq {
    virtual bool operator==(const T &cmp) const = 0;
    bool operator!=(const T &cmp) const {
        return !static_cast(this)->operator== (cmp);
    }
 };

#include 
 
 struct Complex : public AddNoEq {
    Complex(int re, int im): re_(re), im_(im) { }
    
    virtual bool operator==(const Complex& cmp) const {
        return cmp.re_ == this->re_ && cmp.im_ == this->im_;
    }
    // ...
 private:
    int re_, im_;
 };

 int main()
 {
    Complex a(1, 2), b(2, 3);
 
     if (a != b)
        std::cout << "That's ok" << std::endl;
     
     return 0;
 }
http://ru.wikipedia.org/wiki/Примесь_(программирование)

No comments:

Post a Comment