Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active April 20, 2017 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/bf8320137374c457afd6c7500d802117 to your computer and use it in GitHub Desktop.
Save kenwebb/bf8320137374c457afd6c7500d802117 to your computer and use it in GitHub Desktop.
Composition over inheritance
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Thu Apr 20 2017 08:18:37 GMT-0400 (EDT)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Composition over inheritance
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: bf8320137374c457afd6c7500d802117
Keywords:
My Notes
--------
April 19, 2017
Implement te Duck example on the wikipedia page.
Use a more Xholon-like approach.
Base it on:
https://gist.github.com/kenwebb/5c0cd0c62961c93255ba0277191fb18d
References
----------
(1) https://en.wikipedia.org/wiki/Composition_over_inheritance
Composition over inheritance (or composite reuse principle) in object-oriented programming is the principle that classes should achieve polymorphic behavior and code reuse by their composition (by containing instances of other classes that implement the desired functionality) rather than inheritance from a base or parent class. This is an often-stated principle of OOP, such as in the influential book Design Patterns.
it has an example based on Duck classes; it's presented as a UML class diagram
(2) https://upload.wikimedia.org/wikipedia/commons/b/ba/UML_diagram_of_composition_over_inheritance.svg
the UML class diagram for the Duck example
(3) https://codingdelight.com/2014/01/16/favor-composition-over-inheritance-part-1/
(4) https://facebook.github.io/react/docs/composition-vs-inheritance.html
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
In this section, we will consider a few problems where developers new to React often reach for inheritance, and show how we can solve them with composition.
]]></Notes>
<_-.XholonClass>
<PhysicalSystem/>
<Animal>
<Duck>
<MallardDuck/>
<RedheadDuck/>
<RubberDuck/>
<DecoyDuck/>
<PekingDuck/>
</Duck>
<Turkey/>
</Animal>
<!-- behaviors -->
<!-- Fly behaviors -->
<Flyable superClass="Script">
<FlyWithWings/>
<CannotFly/>
<FlyRocketPowered/>
</Flyable>
<!-- Quack behaviors -->
<Quackable superClass="Script">
<Quack/>
<CannotQuack/>
<DuckCall/>
<Squeak/>
</Quackable>
<!-- Display behaviors -->
<Display superClass="Script"/>
</_-.XholonClass>
<xholonClassDetails>
<MallardDuck><Color>rgba(0,255,0,1.0)</Color></MallardDuck>
<RedheadDuck><Color>rgba(255,0,0,1.0)</Color></RedheadDuck>
<RubberDuck><Color>rgba(255,255,0,1.0)</Color></RubberDuck>
<FlyWithWings><DefaultContent><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode;
},
act: function() {
me.println(me.parent().name() + " " + me.name());
}
}
//# sourceURL=FlyWithWings.js
]]></DefaultContent></FlyWithWings>
<CannotFly><DefaultContent><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode;
},
act: function() {
me.println(me.parent().name() + " " + me.name());
}
}
//# sourceURL=CannotFly.js
]]></DefaultContent></CannotFly>
<Quack><DefaultContent><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode;
},
act: function() {
me.println(me.parent().name() + " " + me.name());
}
}
//# sourceURL=Quack.js
]]></DefaultContent></Quack>
<Squeak><DefaultContent><![CDATA[
var me, beh = {
postConfigure: function() {
me = this.cnode;
},
act: function() {
me.println(me.parent().name() + " " + me.name());
}
}
//# sourceURL=Squeak.js
]]></DefaultContent></Squeak>
</xholonClassDetails>
<PhysicalSystem>
<MallardDuck multiplicity="2">
<FlyWithWings/>
<Quack/>
</MallardDuck>
<RubberDuck multiplicity="2">
<CannotFly/>
<Squeak/>
</RubberDuck>
<Turkey/>
<Duck/>
<RedheadDuck/>
</PhysicalSystem>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Duck</title>
<rect id="PhysicalSystem/Duck" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>Turkey</title>
<rect id="PhysicalSystem/Turkey" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment