<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">For a third approach take a look at&nbsp;<a href="http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/" class="">http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/</a>&nbsp;and the following posts (5).&nbsp;<div class="">Entity Component Systems avoid the problem of everything migrating up into the base class and the propagation of classes by separating aspects (attributes, things the entity has) of the entity into components. The entity itself is little more than a UUID handle to the components owned by that entity. In this example an alligator has a LandComponent and a SeaComponent. A System contains the code that acts on the data contained in those components and there is a system for each kind of component, for example a LandSystem that has code to make an entity move on land. Each update the System acts on every entity that has a component matching it.&nbsp;</div><div class=""><br class=""></div><div class="">The description above is the more modern AAA-game ideal of ECS but there is disagreement about the fine points (of course!). Apple’s GameplayKit implements an older form of ECS like that developed by Scott Bilas for Dungeon Siege (<a href="http://scottbilas.com/games/dungeon-siege/" class="">http://scottbilas.com/games/dungeon-siege/</a>) in 2003, where components have code and data.&nbsp;</div><div class=""><br class=""></div><div class="">I think this is very like the POP idea… but perhaps cleaner since the System processes, subject to the usual sync issues, can be separate threads. Adding a behaviour or a new entity is as simple as choosing the right components, aggregating them into an entity and then adding that entity to the update loop. And it doesn’t matter how many aspects you add to a class - all the code is encapsulated in the System, all the data in the Component. The POP approach is pretty good but you still wind up with a very beefy class.</div><div class=""><br class=""></div><div class=""><div><blockquote type="cite" class=""><div class="">On 16 Feb 2016, at 6:21 AM, Jon Hoffman via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><p class="MsoNormal">Sorry for the misunderstanding, I see what you are doing now,
you are using the command pattern, however I so see quite a bit of difference
between your design and my POP design.<span style="mso-spacerun:yes" class="">&nbsp;
</span>The biggest is the number of types needed between the designs.<span style="mso-spacerun:yes" class="">&nbsp; </span>For example, how the Lion attacks and moves on
land is different than how the Alligator attacks and moves on land therefore
you will need separate attack and movement types for each type of animal.<span style="mso-spacerun:yes" class="">&nbsp;&nbsp; </span>This means that you will need a Alligator
type and to go with that Alligator type you would need AlligatorLandAttack,
AlligatorSeaAttack, AlligatorLandMove and AlligatorSeaMove types.<span style="mso-spacerun:yes" class="">&nbsp; </span>You would also need the Lion type with a
LionLandAttack and LionLandMove type.<span style="mso-spacerun:yes" class="">&nbsp;
</span>You would need these separate types for each animal you create.<span style="mso-spacerun:yes" class="">&nbsp; </span>With my POP all of this logic would be
encapsulated in a single types.</p><div class="">&nbsp;<br class="webkit-block-placeholder"></div><p class="MsoNormal">By encapsulating all of the logic into a single type your
code will be easier to manage and also less error prone.<span style="mso-spacerun:yes" class="">&nbsp; </span>For example, in your code there is nothing
preventing a developer to accidentally adding the LionLandAttack type to the
Alligator type and since that error could occur anyplace where an instance of
the Alligator is created it could also be hard to track down.</p><div class="">&nbsp;<br class="webkit-block-placeholder"></div><p class="MsoNormal">One of the principles of Protocol-Oriented programming is
making our code safer and easier to manage.<span style="mso-spacerun:yes" class="">&nbsp;
</span>Your code would work but I would still say that the POP code would be
much easier to manage long term and would also be less error prone.<span style="mso-spacerun:yes" class="">&nbsp; </span>That is my opinion and everyone will have
their own.</p>

</div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Feb 15, 2016 at 1:52 PM, Daniel Tartaglia <span dir="ltr" class="">&lt;<a href="mailto:danielt1263@gmail.com" target="_blank" class="">danielt1263@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">That’s easy to do by allowing an Animal to hold multiple modes. Yes, the code below uses a Protocol, but only as an OO interface.<div class=""><br class=""></div><div class=""><font face="Monaco" class="">let alligator = Animal()</font></div><div class=""><font face="Monaco" class="">alligator.mode.append(Land())</font></div><div class=""><font face="Monaco" class="">alligator.mode.append(Sea())</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class="">protocol Mode {</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; func attack() -&gt; Bool</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; func move() -&gt; Bool</font></div><div class=""><font face="Monaco" class="">}</font></div><div class=""><font face="Monaco" class=""><br class=""></font></div><div class=""><font face="Monaco" class="">class Animal {</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; var modes: [Mode]</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; func attack() -&gt; Bool {</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; for mode in modes {</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if mode.attack() {</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break</font></div><span class=""><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; }</font></div><div class=""><font face="Monaco" class="">}</font></div><div class=""><br class=""></div><div class=""><br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 15, 2016, at 1:43 PM, Jon Hoffman &lt;<a href="mailto:hoffman.jon@gmail.com" target="_blank" class="">hoffman.jon@gmail.com</a>&gt; wrote:</div><br class=""><div class=""><div dir="ltr" class="">Thank you for the feedback however you cannot design the code as you describe, if I understand your explanation correctly, because one of the requirements is the animals may be apart of multiple categories.&nbsp; As the example in the post shows the alligator belongs to both the Land and the Sea categories.&nbsp; In you description that would mean that the Alligator type would need to be a subclass of both the Land and Sea superclasses which is not permitted.&nbsp; Remember that one of the drawbacks with OOP is a subclass can only inherit from one superclass.<div class=""><br class=""></div><div class="">Jon</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Mon, Feb 15, 2016 at 1:17 PM, Daniel Tartaglia <span dir="ltr" class="">&lt;<a href="mailto:danielt1263@gmail.com" target="_blank" class="">danielt1263@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">(Reposting because I forgot to change the subject line. Hope that this is the correct thing to do.)</div><div class=""><br class=""></div>I have to say John that I am not a fan of your OOP code. I would have written the OOP code very much like you wrote the POP version using the Strategy pattern.<br class=""><div class=""><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class=""><div class=""><font face="Monaco" class="">[Animal]&lt;*&gt;---&gt;[Mode]</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+------+------+</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;|</font></div><div class=""><font face="Monaco" class="">&nbsp; &nbsp; &nbsp; &nbsp; [Land] &nbsp;[Sea] &nbsp;[Air]</font></div><div class="">&nbsp; &nbsp; &nbsp;</div></div><div class=""><br class=""></div><div class="">(View the above with a mono-spaced font.)</div><div class=""><br class=""></div><div class="">In essence, I see no difference. There may be a difference, but I don’t think your example presents one.</div><div class=""><br class=""></div></div></div></div></blockquote></div></div></div></blockquote></div><br class=""></div></div></span></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>