久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

什么是 Mixin(作為一個(gè)概念)

What are Mixins (as a concept)(什么是 Mixin(作為一個(gè)概念))
本文介紹了什么是 Mixin(作為一個(gè)概念)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我試圖理解 Mixin 的概念,但我似乎無法理解它是什么.我認(rèn)為它是一種通過使用繼承來擴(kuò)展類功能的方法.我讀過人們將它們稱為抽象子類".誰能解釋一下為什么?

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?

如果您能根據(jù)以下示例(來自我的演講幻燈片之一)解釋您的答案,我將不勝感激:

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

推薦答案

在討論混入是什么之前,描述它試圖解決的問題很有用.假設(shè)您有一堆想法或概念要建模.它們可能以某種方式相關(guān),但它們?cè)诖蠖鄶?shù)情況下是正交的——這意味著它們可以相互獨(dú)立.現(xiàn)在,您可以通過繼承對(duì)此進(jìn)行建模,并使每個(gè)概念都派生自某個(gè)通用接口類.然后在實(shí)現(xiàn)該接口的派生類中提供具體方法.

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.

這種方法的問題在于,這種設(shè)計(jì)沒有提供任何清晰直觀的方法來獲取每個(gè)具體類并將它們組合在一起.

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.

mix-ins 的想法是提供一堆原始類,其中每個(gè)類都為一個(gè)基本的正交概念建模,并且能夠?qū)⑺鼈冋吃谝黄鹨越M成更復(fù)雜的類,僅具有您想要的功能 - 有點(diǎn)像樂高積木.原始類本身旨在用作構(gòu)建塊.這是可擴(kuò)展的,因?yàn)樯院竽梢詫⑵渌碱愄砑拥郊现校粫?huì)影響現(xiàn)有的類.

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++,這樣做的一種技術(shù)是使用模板和繼承.這里的基本思想是通過模板參數(shù)將這些構(gòu)建塊連接在一起.然后你將它們鏈接在一起,例如.通過 typedef,形成一個(gè)包含你想要的功能的新類型.

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.

以您的示例為例,假設(shè)我們要在頂部添加重做功能.下面是它的樣子:

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() << '
';  // 84
  mynum.undo();
  cout << mynum.get() << '
';  // 42
  mynum.redo();
  cout << mynum.get() << '
';  // back to 84
}

你會(huì)注意到我對(duì)你的原作做了一些改動(dòng):

You'll notice I made a few changes from your original:

  • 虛函數(shù)在這里真的不是必需的,因?yàn)槲覀冊(cè)诰幾g時(shí)確切地知道我們組合的類類型是什么.
  • 我為第二個(gè)模板參數(shù)添加了一個(gè)默認(rèn)的 value_type 以使其使用不那么麻煩.這樣您就不必每次將一個(gè)片段粘在一起時(shí)都繼續(xù)輸入 .
  • 使用一個(gè)簡(jiǎn)單的 typedef 而不是創(chuàng)建一個(gè)從片段繼承的新類.
  • 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.

請(qǐng)注意,這只是一個(gè)簡(jiǎn)單的例子來說明混入的想法.所以它沒有考慮極端情況和有趣的用法.例如,在沒有設(shè)置數(shù)字的情況下執(zhí)行 undo 可能不會(huì)像您預(yù)期的那樣工作.

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.

作為旁注,您可能還會(huì)找到這篇文章 很有幫助.

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

這篇關(guān)于什么是 Mixin(作為一個(gè)概念)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 成人高清网站 | 久久久久久久综合色一本 | 久久视频免费观看 | 精品久久久久久久久亚洲 | 97国产一区二区精品久久呦 | 免费一二区 | 国产精品无 | 久久久无码精品亚洲日韩按摩 | 荷兰欧美一级毛片 | 日韩中出 | 久国产精品 | 久久久精品网站 | 性一交一乱一透一a级 | 91视频在线 | 色嗨嗨 | 日韩精品无码一区二区三区 | 欧美成人一区二区 | 欧产日产国产精品v | 日韩精品免费在线观看 | 狠狠干网 | 国产美女一区二区 | 天天夜碰日日摸日日澡 | 日韩色图视频 | 国产性网 | 99国内精品久久久久久久 | 欧美精品日韩精品国产精品 | 国产日韩av一区二区 | 日韩在线视频一区 | 国产免费福利在线 | 成人h视频在线 | 亚洲 欧美 日韩 在线 | 国内久久 | 日韩有码在线播放 | 一区二区三区视频在线观看 | 精品视频一区二区三区在线观看 | 成人精品一区亚洲午夜久久久 | 日韩一区在线播放 | 999久久久 | 青青草av | 国产成人精品一区二区三区在线 | 国产高清视频在线 |