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
Coder100

I feel like what should happen is (you forgot your semis, that bothered me a lot):

and you now see you could even make this only one class! Maybe even polymorphism idk

DynamicSquid

@Coder100 Also what is SomeTypeEnum advanced?

Coder100

the type, land or water @DynamicSquid

DynamicSquid

@Coder100 if statements would just get too ugly, no way

Coder100
DynamicSquid

@Coder100 if statements means I would need to pass in parameters, and it couldn't work well with other interfaces, and it's not modular, etc.

Coder100

hm, maybe im not understanding your question right, sorry :( @DynamicSquid

Coder100

i was thinking them more similar than not sorry :( @DynamicSquid

DynamicSquid

@Coder100 what can I clarify?

Coder100

do they all have similar moves? @DynamicSquid

Coder100

advanced squid for land and aquatic have same moves just reworded and same for basic @DynamicSquid

firefish

@Coder100 what is this

nonsense just use
public void Swim():w
oh no the old VIM habits

Coder100

@firefish i mean in C++ that's what we do

firefish

@Coder100 Well that's my C# brain for you then

xxpertHacker

@firefish I hate Java and C# for making people do this:

When C++ just says:

DynamicSquid

@xxpertHacker exactly, it's so annoying

DynamicSquid

@Coder100 no the whole point is that they're different functions

firefish

@xxpertHacker what the hell in C# structs can have methods

xxpertHacker

@firefish In C#, What is the difference between a struct and a class?

firefish

@xxpertHacker Structs are inline, and classes are allocated on the heap.

xxpertHacker

@firefish Hmm... well, I was using C++ structs as public classes.

Even then, it could've been this:

Still less typing.

firefish

@xxpertHacker Is you ask me, that public: thing looks out of place and hideous

Coder100

looks beautiful @firefish

Coder100

i wish all langs had that syntax, @C# @firefish

firefish

@Coder100 did you mean @CSharpIsGud instead of @C#?

xxpertHacker

@firefish Thus why I simply prefer using the struct keyword in C++ instead, it implies it already, while not looking weird.

But still, public/private fields are better than tediously putting them one by one.

firefish

@xxpertHacker In C++ structs can have methods too, only in plain C you cannot

xxpertHacker

@firefish In C++, structs are perfectly equivalent to classes, with one difference. They are public by default.

So, yes, it makes perfect sense that they would have methods.

Although, C structs can be used as namespaces, by using function pointers.

Eh, still not a method.

firefish

@xxpertHacker Yes, still not a method.
But in Rust there are no such thing as classes! Only structs and enums, in which you can implement wither your own methods, or a trait (the closest thing rust has to an interface)

Coder100

me likey c++ rust not make me commit unsafe @firefish

firefish

@Coder100 do what you "likey", grilled cookie (or is it cookey now?)

CSharpIsGud

@firefish You can, it's just ugly because you have to use function pointers.

If you really want to you can give them self references as the first argument.

CSharpIsGud

@xxpertHacker structs are value types, no default constructor, new operator isn't needed to make them, they can't inherit from structs or classes, no non-static variable initializers

xxpertHacker

@CSharpIsGud Assuming you're talking about C# structs, that'll be worth keeping in the back of my mind, should I ever find myself debugging C# or learning the language.

Also, I presume the unnecessary new is because they aren't heap allocated, is C#'s new related to heap allocation at all?

CSharpIsGud

@xxpertHacker I think they just did new because it was a thing, new doesn't really make a difference.
C# can allocate structs on the heap if they are located inside a class.

xxpertHacker

@CSharpIsGud Well, of course, if a class instance is heap-allocated, then I would expect all of its members to be on the heap too, including structs.

But, since you're here, and you use OOP styled code (right?), you seem like you would be among the most capable programmers here capable of helping out @DynamicSquid with his question.

CSharpIsGud

@xxpertHacker even in C# I avoid inheritance everywhere possible unless it actually makes what I want easier.