What design pattern should I use for specific multiple inheritance?
DynamicSquid

Let's say I have an interface called Squid.

And we have a bunch of different types of squids:

Here's everything a squid can be:

aquaticland
basicbasic-aquaticbasic-land
advancedadvanced-aquaticadvanced-land

How would I go about create an instance of a squid? Should I go about creating 4 classes BasicAquaticSquid, BasicLandSquid, AdvancedAquaticSquid, AdvancedLandSquid like this:

I think that's called the Abstract Factory method? It seems very tedious though. Any better way of doing so?

Thanks

You are viewing a single comment. View All
Answered by xxpertHacker [earned 5 cycles]
View Answer
xxpertHacker

I personally don't use classes, I don't know why, I just don't like OOP code, but, I recommend just using whatever is simplest and most maintainable.

...

Honestly, I sat here for more than 15 minutes, trying to think of a good way to create these interfaces, but nothing sounds good.

I have no clue how you should do this, maybe go look at some gaming industry's source code?

Although, maybe it's hard to understand it because of the lack of information.

What makes a squid advanced or basic?

How different is an aquatic squid from a land squid?

Depending on how you define the differences between them, you might not even need separate classes at all.

xxpertHacker

@DynamicSquid I'm not sure if this is a good way of setting it up, although, I am not a game developer either. (This is a game you're attempting to program, right?)

Now, I have no clue how your game is going to work nor do I have many details, but if the squids are capable of battling each other, ideally, you would want a generic setup that would allow you to pit any two squids or players against each other, at runtime, without compiling a separate code template for every single possible combination of creature that could meet.

Let's say the core of the game revolves around pitting squids against each other (wait this sounds like Pokemon or something, eww), you have a function called battle.
It takes two formal parameters, each being the squid to take a side in the battle.

How would you set it up, if each squid has different methods? Duck typing isn't safe. And compiling something like battle<Squid1, Squid2>(Squid1 squid1, Squid2 squid2); for every possibility isn't fun either.

Also, in every game that I've played, to date, every different player could all do similar things at a high-level, they just have different animations.

Think of a shooting game, each firearm deals damage, and has velocity, yet they have different animations, sounds, trajectories, and numbers associated with their capabilities.

Personally, I might do something generic, like .move, .primaryAttack, .secondaryAttack, .canSwim, and give them all different implementations, but I'm not sure if it's the best way to do this, or if anyone else would agree with a structure like this.

DynamicSquid

@xxpertHacker The main difference are the functions. Each class has it's own unique set of functions. For example AquaticSquid has Swim(), Drown(), and Float(). I'll try to reduce the number of functions I have, but for the time being, each class has it's own funtcions.

DynamicSquid

@xxpertHacker It's not really a game, more like a "museum" lol. But yeah, I think you're right. I want polymorphism (what's short form for "polymorphism"? is it "polymorph", or can I say "poly") to work as well, so I probably have to make generic methods.

Thanks for the help though!

xxpertHacker

@DynamicSquid Oh, oops, I presumed that it was a game, or something of the sort based on your initial example:

void DisplayInfo() {}
void Swim() {}
void BasicAttack() {}

BasicAttack seems like something that you would find in game, and considering that we're on Repl... it wouldn't normally be a bad guess.

But, good luck with your program!