什么是Mixins(作为一个概念) [英] What are Mixins (as a concept)

查看:625
本文介绍了什么是Mixins(作为一个概念)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的头围绕着Mixin的概念,但我似乎不明白它是什么。
我看到的方式是,它是一种通过使用继承来扩展类的功能的方法。
我读过,人们把它们称为抽象子类。谁能解释为什么?

I'm trying to get my head around the Mixin concept but I can't seem to understand what it is. The way I see it is that it's a way to expand the capabilities of a class by using inheritance. I've read that people refer to them as "abstract subclasses". Can anyone explain why?

如果你能根据下面的例子解释你的答案(从我的演讲幻灯片之一):
< img src =http://i.stack.imgur.com/uxgOo.pngalt =A C ++ Mixin示例>

I'd appreciate if you'd explain your answer based on the following example (From one of my lecture slideshows):

推荐答案

在介绍一个混合是什么之前,描述它想要解决的问题是有用的。假设你有一堆想要或想要建模的概念。它们可能以某种方式相关,但它们大部分是正交的 - 这意味着它们可以彼此独立地彼此支持。现在你可以通过继承来建模这些概念,并且每个概念都来自一些通用的接口类。然后在实现该接口的派生类中提供具体的方法。

Before going into what a mix-in is, it's useful to describe the problems it's trying to solve. Say you have a bunch of ideas or concepts you are trying to model. They may be related in some way but they are orthogonal for the most part -- meaning they can stand by themselves independently of each other. Now you might model this through inheritance and have each of those concepts derive from some common interface class. Then you provide concrete methods in the derived class that implements that interface.

这种方法的问题是,这种设计不提供任何明确的直观方式具体类并将它们组合在一起。

The problem with this approach is that this design does not offer any clear intuitive way to take each of those concrete classes and combine them together.

使用混合的想法是提供一堆原始类,其中每个类都建立一个基本的正交概念,并且能够将它们粘在一起组成更多复杂的类只有你想要的功能 - 像legos。原语类本身被用作构建块。这是可扩展的,因为稍后你可以添加其他原始类到集合而不影响现有的。

The idea with mix-ins is to provide a bunch of primitive classes, where each of them models a basic orthogonal concept, and be able to stick them together to compose more complex classes with just the functionality you want -- sort of like legos. The primitive classes themselves are meant to be used as building blocks. This is extensible since later on you can add other primitive classes to the collection without affecting the existing ones.

回到C ++,一个技术是使用模板和遗产。这里的基本想法是通过模板参数提供它们将这些构建块连接在一起。然后你把它们链接在一起,例如。通过 typedef ,形成包含所需功能的新类型。

Getting back to C++, a technique for doing this is using templates and inheritance. The basic idea here is you connect these building blocks together by providing them via the template parameter. You then chain them together, eg. via typedef, to form a new type containing the functionality you want.

以你的例子,在上面添加一个重做功能。以下是它的外观:

Taking your example, let say we want to add a redo functionality on top. Here's how it might look like:

#include <iostream>
using namespace std;

struct Number
{
  typedef int value_type;
  int n;
  void set(int v) { n = v; }
  int get() const { return n; }
};

template <typename BASE, typename T = typename BASE::value_type>
struct Undoable : public BASE
{
  typedef T value_type;
  T before;
  void set(T v) { before = BASE::get(); BASE::set(v); }
  void undo() { BASE::set(before); }
};

template <typename BASE, typename T = typename BASE::value_type>
struct Redoable : public BASE
{
  typedef T value_type;
  T after;
  void set(T v) { after = v; BASE::set(v); }
  void redo() { BASE::set(after); }
};

typedef Redoable< Undoable<Number> > ReUndoableNumber;

int main()
{
  ReUndoableNumber mynum;
  mynum.set(42); mynum.set(84);
  cout << mynum.get() << '\n';  // 84
  mynum.undo();
  cout << mynum.get() << '\n';  // 42
  mynum.redo();
  cout << mynum.get() << '\n';  // back to 84
}

您会注意到原始:


  • 虚拟函数在这里不是必需的,因为我们知道我们编译时的类型是什么。 li>
  • 我为第二个模板参数添加了一个默认值 value_type ,使其使用更麻烦。这样,每次你粘在一起时,你不必再键入< foobar,int>

  • 一个从类继承的新类,使用一个简单的 typedef

  • The virtual functions really aren't necessary here because we know exactly what our composed class type is at compile-time.
  • I've added a default value_type for the second template param to make its usage less cumbersome. This way you don't have to keep typing <foobar, int> everytime you stick a piece together.
  • Instead of creating a new class that inherits from the pieces, a simple typedef is used.

注意,这只是一个简单的例子来说明混合的想法。所以它不考虑角落的情况和有趣的用法。例如,在不设置数字的情况下执行 undo 可能不会像您预期的那样正常。

Note that this is meant to be a simple example to illustrate the mix-in idea. So it doesn't take into account corner cases and funny usages. For example, performing an undo without ever setting a number probably won't behave as you might expect.

您可能还会发现 本文

As a sidenote, you might also find this article helpful.

这篇关于什么是Mixins(作为一个概念)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆