I asked Claude Sonnet 3.5 for an example of polymorphism in DCI, and the AI gave me the following Ruby code.
In the code, the "polymorphism" is just a "switch case" statement in the context object, unlike the polymorphism in OO, which defines the behavior in each class's definition.
Is the AI's answer correct? If so, why does DCI design "polymorphism" as a "switch case"?
--
You received this message because you are subscribed to the Google Groups "object-composition" group.
To unsubscribe from this group and stop receiving emails from it, send an email to object-composit...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/9b2d5bd3-c984-4986-af6e-bdfdd1469f35n%40googlegroups.com.
Cope's answer is great (Cope = nickname for James Coplien, if you didn't know).
I think it might also be worth mentioning that designing objects with polymorphism can have downsides too, even in DCI (mainly in the "data" part where in most languages you still have classes and inheritance), if it's overused or used inappropriately. The main tradeoff to think about is how difficult it will be to determine where a method call actually ends up at run-time. I feel that the name of this group speaks to this a bit - often you can accomplish polymorphism in a clearer way using composition rather than inheritance.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/A75B3704-EE3E-4758-B104-6F0446AAB8F5%40gmail.com.
P.S. Inheritance isn't always bad, in case it sounded like that's what I was saying.
When I saw the title in the post I recalled a series of small videos from Japan where Coplien also talks about this aspect. Here is the link of the channel: https://youtube.com/@dcitokyo3554?si=9qNm5mOc4tsf2Ieg . Is about the videos from playlist "DCI Tokyo 2". Ilike a lot that content.
In short polimorphism, if I uderstood and recall ok, is not the main actor is more like an effect after doing what is needed. I will watch the videos again.
--
--
Regarding the definition of polymorphism:
Polymorphism means that the sender of a stimulus does not need to know the receiving instance’s class. The receiving instance can belong to an arbitrary class.
(Object-Oriented Software Engineering: A Use Case Driven Approach, p.55)
This definition emphasizes that the sender of a stimulus doesn't need to know the receiving instance's class. In DCI, while there's flexibility in which objects can play which Roles, the Context object does have knowledge of the specific instances it's working with, as it's responsible for assigning them to Roles.
@Hai is there an actual problem you're aiming to solve, or perhaps to evaluate if you can solve it better with DCI than other approaches? This discussion seems very abstract, and I can't tell what you're driving at.
--
You received this message because you are subscribed to the Google Groups "object-composition" group.
To unsubscribe from this group and stop receiving emails from it, send an email to object-composit...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/2CB21378-0C81-4F8E-92AD-7C3ABB68B9C4%40gmail.com.
Take turtle graphics as an example: the turtle can "move forward", "turn left", and "turn right"
If I want to add a new action for the turtleI think inclusion polymorphism is more aligned with the Open-Closed Principle.
Hi Hai,
I really think you are going about this the totally wrong way. If you mention DCI in your talk, I think you should just be honest and say you're still learning DCI (and haven't ever used it on a real project? If that's the case), so you don't give people the impression you're an expert and unfairly represent DCI. I certainly wouldn't let AI-generated code influence your thinking or your presentation in any way. To the extent that LLM-based AI ever "understands" code, it's extremely unlikely to "understand" DCI code because DCI code isn't widespread, and the DCI code on the web is a mix of good DCI code as well as some code labeled as DCI but either isn't DCI at all (due to people not understanding the importance of object identity and just trying to implement it in an existing programming language in a naive way), or code that is from someone just learning it or trying it out for a trivial example.
I have been in this group for many years now, and have never even seen DCI code like the FlyingContext example the AI came up with. It's really hard to talk about that AI example because I don't even know what problem it's trying to solve, but if there were a real problem I'm fairly certain that DCI programmers would solve it in a very different way.
And it's just generally true in life that if you really want to understand something and go beyond the most surface-level knowledge (or misunderstanding), you have to really dig into some concrete examples - and in the case of DCI, write plenty of code - and not only think in the abstract. I know that if you're trying to give a summary and contrast different approaches, that part of your talk might need to be abstract, but the way that's supposed to work is that you understand the topic concretely yourself and *then* summarize it abstractly, and perhaps offer a very simple concrete example so your audience can start understanding it too.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/c2385c82-c00e-4bae-bfcd-8a1048123f70n%40googlegroups.com.
So what is the right way to implement the turtle graphics in DCI? How would you design it?
I have some concept of turtle graphics and know that example has been around forever, but I personally haven't implemented them because they weren't part of the tutorials when I was first learning to program or the computer science classes I took. Not that I would need to be familiar—I'm sure looking at it with fresh eyes could be helpful.
But I would start by saying not to assume that it should even be
written in DCI in the first place. DCI takes OO to a new level but
OO is still the foundation, and some use cases are simple enough
(from the role-behavior perspective) that there isn't a need to
introduce DCI. In his book "Lean Architecture: for Agile Software
Development", Cope makes a distinction between an "atomic event
architecture" and a DCI architecture. Simple object-oriented
systems like a drawing program to draw various shapes on the
screen would be an example of an "atomic event architecture" that
doesn't really need DCI. In those cases, you can just put the
behavior and data together in the same code construct (usually a
class) and have no problem, because the behavior and data are
naturally coupled and don't diverge like they do in many other use
cases. I could point you to some more resources about this if
you're interested.
There are plenty of folks in the group (myself included) who are
interested in a programming model where you only have DCI contexts
and no classes, but even in that case you'd still have a
separation between the data part—which is much more static—and the
dynamic behavior part where roles are more prominent. And there
could very well still be behavioral parts of the system that are
more like "atomic event architecture" and end up looking a lot
like traditional OO even in the absence of a "class" keyword or
inheritance.
Den 23. sep. 2024 kl. 15.05 skrev Matthew Browne <mbro...@gmail.com>:
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/782e2277-35e6-4085-8996-3b260b73130d%40gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/CAF0aJ5dHh-4eV6EDX_v7M%2B3O3o8utRy95zzAziztjnra1t%2BdBQ%40mail.gmail.com.
On 23 Sep 2024, at 18.54, Matthew Browne <mbro...@gmail.com> wrote:One quick thought on this for now: in DCI, most likely you would still just have one turtle object that can do anything a turtle can do - same as OOP.
I do believe that I can write an entire program using DCI. It's a
programming paradigm, like OOP or FP. It should be able to write any
program.
boolean checkAndAdjustPosition() {boolean somethingMoved = falseList<Player> neighborList = Neighbors.of(this)Player player1 = neighborList[0], player2 = neighborList[1]double distance1 = distanceTo(player1), distance2 = distanceTo(player2);if (Math.abs(distance1 - distance2) > GameData.DELTA) {PlayerMoveCount.incrementCountForPlayermoveToSameDistanceFromNeighbors(player1, player2)somethingMoved = true// tail recursivefor (Player p : myWatchers()) {CurrentPlayer = pCurrentPlayer.moved}}return somethingMoved}
On 23 Sep 2024, at 18.54, Matthew Browne <mbro...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/CAJA9FQ-%2BB-2sZdKs5qXoNgaXhZUB3HNXQT3G012vCKRspy%2Ba8w%40mail.gmail.com.
I have already learned this territory from your and Trygve's book. I want to explore outside this territory.
I do not treat DCI as a design pattern. I think DCI is a programming paradigm; it should be able to build all kinds of programs.
But I don't know how.
That is why I am trying to convert textbook examples from OOP into DCI.
I should be able to build logging systems, data repositories, and plugin architectures using DCI.
Maybe DCI is a very unsuitable paradigm for this kind of stuff, but I should still be able to do it.
That is what I am trying to explore.
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/c0529f92-ba4e-44e3-9661-c63ec23c973dn%40googlegroups.com.
On 24 Sep 2024, at 17.16, Hai Feng Kao <khai...@gmail.com> wrote:That is why I am trying to convert textbook examples from OOP into DCI.
On 24 Sep 2024, at 17.16, Hai Feng Kao <khai...@gmail.com> wrote:I should be able to build logging systems, data repositories, and plugin architectures using DCI.
Maybe DCI is a very unsuitable paradigm for this kind of stuff, but I should still be able to do it.
A little exercise in understanding the expressive power of DCI.How would you in a class oriented language sensibly use a vector to represent any animal?
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/268336BB-F008-4EB0-B40E-9B10E2A0BA5E%40gmail.com.
Hi Rune, I think this could be an interesting exercise to explore (and hopefully Hai is interested in it too), but first I wanted to check if I understand correctly... When you say using a vector to represent an animal, do you mean like a line drawing of an animal represented as a series of X-Y coordinates?
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/CAJA9FQ8ZEda2CpZGniVN-EWXb2Mr8LWMktQx00_KZ_3NdR9YXg%40mail.gmail.com.
Hi Rune,
I've been busy, so just now starting to think about this again...
First of all I want to give the disclaimer that I know next to nothing about how computer graphics are calculated (I've been spoiled by letting the web browser do all the work for me ;) ), so this is all new to me.
In terms of updating system state, are you essentially suggesting that there would be a "current position" vector and then a second vector used to calculate the new position every time you want to move the animal?
And just to make sure we're on the same page about vectors, instantiating a new vector would mean something like the following, correct?:
new
Vector(distanceToMoveHorizontally, distanceToMoveVertically)
To view this discussion on the web visit https://groups.google.com/d/msgid/object-composition/DDE53A36-F5BF-4547-A02A-FAF73B56A59D%40gmail.com.
Hi all,
It turns out that turtle graphics might be an interesting use case
for DCI after all, if you want the app to be extensible to support
other moving objects besides turtles.
@Rune I don't know if this is quite what you had in mind, but here's the example I created, inspired by your idea:
https://codesandbox.io/p/sandbox/turtle-graphics-dci-example-6p6zsj
I decided to modernize it a bit by also including a feature to make the turtle go somewhere simply by clicking where you want it to go.
I have no prior background in vector math or game development, so I'm happy to take any suggestions to improving that implementation, but naturally I was mainly interested in the DCI aspects here. It's just a POC and has some flaws (which could probably be fixed without too much effort), e.g. it will happily let you move the turtle off the screen entirely.
The interesting DCI parts about this are:
1. It's polymorphic, i.e. it would work with any kind of object that you want to be moveable in a similar way
2. It reflects more than one mental model thanks to a nested
context (LinearMotionIntent is the outer context and
LinearMotionPhysics is the nested context that implements the
actual movement).
This is also the first time I've I instantiated a nested context in a dynamic way: rather than passing a LinearMotionPhysics instance to LinearMotionIntent, you pass the LinearMotionPhysics context declaration (could be any Context that implements the necessary methods):
...So LinearMotionIntent takes care of the implementation details
of how to instantiate LinearMotionPhysics (in this case just
passing the body
argument, but I could imagine this being a generally useful
pattern for cases like this).
Let me know what you all think! :-)
--
You received this message because you are subscribed to the Google Groups "object-composition" group.
To unsubscribe from this group and stop receiving emails from it, send an email to object-composit...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/object-composition/321f8459-876a-4f28-964c-5e9ebec2cf65%40gmail.com.
Hi Rune,
Yes, that's what I was going for - the LinearMotionPhysics context is the lower-level mental model in this case, and LinearMotionIntent (meant to reflect the mental model of an animator or designer, and in this case is also simple enough to match the mental model of the game player) builds on and is a compression of the LinearMotionPhysics context.
I'm not sure how one could implement an example like this with just a single vector as the main shared role player between the two mental models, which is what I initially thought you were suggesting, so that's why I created the Body2D class based on something I had seen in another game example, which was mainly useful as a way to encapsulate position (a vector) and angle (stored as just a number in my example) together. I'm sure there's more than one way of modeling a vector, and it could be interesting to explore an alternative model if you had one in mind, but either way I think the way the turtle/object is shared in these two very generic contexts helps show the usefulness and potential of DCI.
Thanks,
Matt
Oh, and one other detail I forgot to mention: the LinearMotionPhysics context uses role re-binding a lot, but it would also be possible to implement it with immutable role bindings / creating a new context every frame of the animation where the object changes position. I'm not sure which would be better in this case, but re-binding seemed simpler to me while writing it.