dongyi1524 2016-10-17 16:15
浏览 50
已采纳

是否为golang代码建立了已建立的模式名称,该名称似乎类似于mixin

The gist https://gist.github.com/anonymous/68c3b072538ec48c2667f7db276e781b is a minimal simplified example of a repeated golang code pattern that I have encountered in an existing code base which I am trying to document. From its usage it appears to be similar to a mixin, but neither myself or any of my colleagues have actually seen this pattern before in golang. Can anyone tell me an appropriate established name for this pattern?

The gist attempts to illustrate:

  • Unmodified behaviour implementation code (M's funcs) used by multiple types (A and B).
  • Included by composition into a host type (A and B).
  • Behaviour methods exported and accessible via a host.behaviour.method(...) call e.g. a.Formatter.FormatText(...).
  • Host methods are acted upon by the behaviour.
  • The behaviour holds state (private or exported) which modifies the execution of the behaviour.
  • The type that has add a specific behaviour added can be used by a function needing that behaviour by passing the behaviour field e.g. b := NewB().Formatter.
  • Many different behaviours can be composed into a given type (not actually shown for brevity, but you could imagine an M1, M2, M3, etc being included into A or B).

It does not seem to strictly satisfy much of the definitions for a mixin as it manipulates the host object in ways that were beyond M's knowledge when it was written (potentially modifying host state) and doesn't directly add methods to the host type:

And it doesn't seem to follow the same pattern as some self proclaimed 'golang mixins' I have found:

  • 写回答

2条回答 默认 最新

  • drn5375 2017-01-14 22:33
    关注

    What is a mixin?

    A mixin is basically a piece of code to be included ('mixed in') to another:

    // WARNING: Messy, untested C code ahead
    // Illustrates mixins in a non-OO language.
    // mixin_hasname.h
        char *name;
    // mixin_hasname_impl.h
    struct hasname_vtable { // Vtable for storing mixin functions. Saves a lot of memory in the long run, as only one vtable has to be defined per type.
        char *(*getname)(void *self);
    };
    
    #define CAT(X,Y) X##Y
    #define CATX(X,Y) CAT(X,Y)
    #define GEN_HASNAME_VTABLE(TYPENAME,PRENAME)\
    static struct hasname_vtable {\
        .getname = CATX(PRENAME,getname)\
    } CATX(PRENAME,vtable);\
    static char *CATX(PRENAME,getname)(void *self) {\
        return ((TYPENAME *)self)->name;\
    }
    // main.c
    struct my_c_struct {
    // include our mixin fields
    #include <mixin_hasname.h>
        int x, y, z;
    };
    
    // include our mixin implementation
    #include <mixin_hasname_impl.h>
    
    // generate an implementation for our struct
    GEN_HASNAME_VTABLE(struct my_c_struct, mycstruct_)
    
    int indirect(struct my_c_struct *x, struct hasname_vtable *hasname_vt) {
        printf("%s
    ", hasname_vt.getname(x));
    }
    
    int main(void) {
        struct my_c_struct x;
    
        x.name = "Name";
        x.x = 0, x.y = 0, x.z = 0;
    
        printf("%s
    ", mycstruct_getname(&x)); // Notice we never had to define mycstruct_getname ourselves; we avoided code duplication
        indirect(&x, mycstruct_vtable); // Generally, vtables are passed. Some languages pass typeid's or function pointers, however. (Few put their vtable in their data type (in this case `x`) because of the inefficient memory usage. In the case of C, though, it saves you from having to pass your vtable everytime you call an indirect function).
    
        return 0;
    }
    

    This also holds true for a mixin in OOP, but OOP adds a lot of restrictions on mixins. For example, mixins might not have access to private variables, child class members, class methods, etc. Golang is not really OO.

    Can this code pattern be called a mixin?

    So, with that out of the way, is this an example of a mixin? No, I wouldn't say so. The biggest problem I'm having with answering that question isn't that your pattern doesn't follow the definition defined for OO (Because Golang is not OO anyway), but that it doesn't really 'mix in' the code, it's just storing it in it's own field.

    struct struct_with_name_field {
        char *name;
    }
    // main.c
    struct my_c_struct {
        struct struct_with_name_field namefield;
        int x, y, z;
    }
    
    int main(void) {
        struct my_c_struct x;
    
        x.namefield.name = "Name";
        x.x = 0, x.y = 0, x.z = 0;
    
        return 0;
    }
    

    However, I do think it's perfectly reasonable to call this pattern a mixin within your team or your documentation, especially because, as far as I know, real mixins aren't possible in Golang anyway, and this pattern is as close as you will get.

    Summary

    Golang doesn't allow you to define mixins (only interfaces), so your pattern isn't a mixin. However, given the definition of mixin (Especially outside OOP), and the fact that your pattern is probably the closest you will get to mixins in Golang, it is very reasonable to call this a mixin anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。