Developer Zone

MATLAB的高级软件开发

Mix It Up and Mix It In

What can we learn about software from an ice cream shop? Quite a bit apparently. It was in the early 70's that史蒂夫的冰淇淋开了,向世界介绍的概念ix-ins. The idea was simple but powerful. Lets produce a small amount of base ice cream flavors like vanilla, chocolate, and strawberry. Then, the strategy goes, provide a plethora of small independent ingredients to mix in to the base flavor resulting in unending flavor possibilities.

Given the success of this approach and many many other ice cream shops like Cold Stone Creamery and Diary Queen that provide similar services the approach seems almost obvious. Well, many things seem obvious in retrospect.

这个想法本身很有价值!我想be able to serve the customer who wants their ice cream to be crunchy as well as those that gag at the thought. If they also want (need!) that smooth caramel ribbon that should be no problem. It is relatively easy to provide 4 different ice cream flavors with different combinations of nuts and caramel to appease all customer types, but this becomes a bit harder when we experience the combinatorial explosion of including (or not!) nuts, oreos, sprinkles, gummy bears, peanut butter cups, and on and on. The genius of the mixin approach is the realization that while each of these items on their own does not an ice cream flavor make, when you can independently choose which of them to mix in for a particular customer the ice cream shop owner can be very nimble to adapt to the needs at hand.

Does this resonate with MATLAB? As we strive tokeep MATLAB class hierarchies wideMixin方法可能非常吸引人。要应用这种方法,我们需要做的是使用我们想在许多不同类型的类或对象上共享的各种属性和功能,然后将它们包装成自己的独立Mixin类。这样的混合班应该只有一个目的或责任。这个想法是,这是一个本身没有太多意义的班级,就像核桃一样,作为冰淇淋产品并没有太多的价值。但是,当与其他代码混合时,可以提供丰富而灵活的软件产品。

Technically speaking, we already saw this with our首次讨论on this theme of flexible inheritance hierarchies. In this example we see the following code:

ClassDefCelestialBodypropertiesMassendendClassDef(Abstract) RoundedpropertiesRadiusendendClassDef小行星<天体propertiesShapeendendClassDef月亮<天体和圆形propertiesHostPlanetendendClassDefStar < CelestialBody & Roundedmethodsfunction辐射(恒星)endendendClassDefPlanet < CelestialBody & RoundedpropertiesMoonsendendClassDef陆地板<星球propertiesCrustendendClassDef(Abstract) HasRingsproperties戒指endendClassDefGasGiant < Planet & HasRingsendClassDef冰球<星球和山地end

该代码在下面产生宽的层次结构:

上次我们没有真正讨论的是,这实际上不仅继承了接口,而且实际上是从Roundedandhasrings基础类。这是构图的替代方法,它将接口和实现带入继承类。成功的关键是,要使用这种方式使用的类仅提供一种能力和一种能力。它是任何Mixin类的资产,它实际上并不是它本身并不有用的。实际上,山姆有正确的直觉herewhen he felt like these classes should be abstract. Making them abstract further solidifies their intent, prevents them from being used on their own, and thus prevents the class from becoming bloated with additional content as it evolves. The observant reader will have noticed that the relevant classes are indeed abstract in this version whereas they were not in the earlier post.

那么,我们什么时候使用Mixins,什么时候使用构图?这确实是一个很好的设计问题,由于盲目遵守经验法则的风险,我完全不舒服提供任何指导。事实是,您应该意识到自己的软件设计世界中的两种方法。如果您倾向于陷入对所有事物使用继承的陷阱,并发现您考虑的代码共享多于继承提供的抽象,那么我建议加强您对构图的使用。如果您发现自己不断编写锅炉板代码,因为您严格遵守许多小界面的继承,那么您绝对想考虑Mixins是否合适。他们确实很强大。

Note well that there is a distinct difference in the type of the resulting object when either of the two approaches are used. When composition is used you can decouple the type of the object from the implementation, whereas the use of mixins you receive the type and the implementation. Sometimes you want the type, like when your client code wants to accept any object that"HasRings"因为一个概念的概念仅在天体代码中存在。有时您不这样做,例如当您想确保在圆形天体上运行的天体代码拒绝篮球和橘子Roundedbehavior.

MATLAB itself has a few built-in mixin types, likematlab.mixin.Heterogenous,,,,matlab.mixin.Copyable,,,,andmatlab.mixin.CustomDisplay。从这些示例中,您可以看到其中的一些原则。他们每个人都有广泛的适用性,但范围狭窄。他们都没有真正产生任何本质上有价值的东西,但是当混合到现有的对象结构中时,它们具有很大的价值。您是否曾经使用过MATLAB随附的这些Mixin类?如果没有,您有兴趣遵循它们使用的一些示例吗?您是否编写自己的混音课程以利用它们提供的灵活性?

Who ever thought that an ice cream shop would have such a lasting impact on the culture and ethos of software architecture? Actually who am I kidding? Ice cream is awesome, it was always destined for such greatness!




与Matlab®R2015A一起出版

|

Comments

要发表评论,请单击here登录您的数学帐户或创建一个新帐户。