Using interfaces won't work because I want a single implementation. Using this solution would end in a lot of redundant code because I plan on having quite a few sub classes (composition vs inheritance). I've decided that a problem-specific design solution is what I'm looking for, and I can't think of anything elegant.

Basically I want classes to have separate properties, and for those properties to be attached at design time to any sub class I choose. Say, I have class 'ninja'. I would like to be able to make arbitrary sub classes such as 'grayNinja' where a gray ninja will always have a sword and throwing stars. Then possibly 'redNinja' who will always have a sword and a cape. Obviously swords, stars, and capes will each have their own implementation - and this is where I can't use interfaces. The closest solution I could find was the decorator pattern, but I don't want that functionality at runtime. Is the best solution an offshoot of that? Where inside the Black Ninja class constructor, I pass it through the constructors of sword and throwingStar? (those being abstract classes)

haven't coded in a while and reading hasn't gotten me too far - forgive me if the answer is simple.

Edit: Answered my own question. I can't mark it as 'answer' until tomorrow. Please let me know if there's a problem with it that I didn't catch. All the reading this problem forced me to do has been awesome. Learned quite a bit.

It's not clear to me why you don't like composition.
–
BetaJun 16 '11 at 21:17

(+1) Because some O.O. languages support single inheritance, and no interfaces at all.
–
umlcatJun 16 '11 at 21:32

1

@Beta - Composition requires you to retype the method every single time. I plan on not only having a lot of properties, but a lot of sub classes. The redundant code seems extremely messy and unnecessary.
–
PhilJun 16 '11 at 21:46

Eh? You don't have to duplicate the implementation of sword for each sword-carrying type of ninja, but I suppose all (or most) sword-carrying ninja types will need a short drawSword method, which in most cases will be a simple wrapper of sword's draw method. Is that the redundancy you're trying to avoid?
–
BetaJun 16 '11 at 22:06

what language are you trying to fit this into?
–
AlexJun 16 '11 at 22:17

4 Answers
4

You want classes to have separate properties. Have you considered coding exactly that?

For example, you want a RedNinja that is-a Ninja that has-a sword and cape. Okay, so define Ninja to have an inventory, make it accessible through Ninja, and pass in an inventory through RedNinja's constructor. You can do the same thing for behaviors.

Are you using "behaviors" in a technical sense? How do you get a redNinja to draw its sword? Does it have a drawSword method?
–
BetaJun 16 '11 at 22:25

One alternative to having a drawSword() method is to hold a behavior object whose action is to draw a sword. Similar to the collection of objects, it could be included in a set of behaviors. You could use a decorator to make it easy to check canDrawSword() and drawSword().
–
Andy ThomasJun 17 '11 at 0:29

@Beta - I believe this is the solution I was vaguely referring to at the end of my original question (which is slightly different from the decorator pattern). I can't quite wrap my head around it, so will probably code it right now to see how it might all work. I will get back with my results.
–
PhilJun 17 '11 at 13:16

Ah, I was misunderstanding how this would work. Andy, are you saying that for every behavior, I would need a 'can<DoBehavior>' method to check it? That seems like more code than just using a compositional approach.
–
PhilJun 17 '11 at 14:08

Im afraid, that the most close example is the composition design pattern. In order, to become more similar to inheritance, I make a generic base class that all composite classes share, and I make a main class that will be the result of the multiple inheritance, that has a copy of all the public methods of the component classes.

If you want to use interfaces, to enforce that main class have all important methods,
then make an interface that matches each composing class, and implemented in the main class.

I am familiar with C++, but not this language. I take it that "interface" has a specific meaning in this language, and what you're doing sure looks like multiple inheritance to me.
–
BetaJun 16 '11 at 22:47

@Beta looks like C# where you can inherit from multiple interfaces
–
RaynosJun 16 '11 at 22:59

@Beta and @Raynos. Its pseudocode, not a real language, but its suppouse to be similar to C++. First example, C++ only classes. Second example, I use a C++ variant (not C#) that supported interfaces.
–
umlcatJun 16 '11 at 23:08

This is using composition no? If this is the only way, then I can deal with that. I just thought there might be a way without use of all those wrapper methods. thanks for your time
–
PhilJun 17 '11 at 13:09

Ah, wish I could edit my comment - as I skimmed over the part where you say that it basically is using composition. Thanks for the clear code and response.
–
PhilJun 17 '11 at 13:40

Alright so mix-ins through extension methods are going to be my preferred route. I couldn't figure out how to use dynamic proxies in vb.net (seemed to require libraries with lots of documentation that didn't cover specifically what I needed). Dynamic proxies also seems to be a bit dirtier of a solution than using extension methods. Composition would have been what I defaulted to if the previous two didn't work.

So one problem with extension methods, is that the code gets a little dirtier if you want to hold variables. Not much though. Another problem is that all the extension methods must be defined in modules, so the code might look a little goofy to a new eye. I will solve this by defining my interface and module with the corresponding extension method in the same file.

finally, here's some sample vb.net code if you don't want to see a full fledged example through the link.

Imports System.Runtime.CompilerServices 'for extension methods
Public Interface ISword
End Interface
Public Interface IThrowingStar
End Interface
Module ExtensionMethods
<Extension()>
Public Sub swingSword(ByVal hasASword As ISword)
Console.WriteLine("Sword has been swung")
End Sub
<Extension()>
Public Sub throwStar(ByVal hasAStar As IThrowingStar)
Console.WriteLine("Star has been thrown")
End Sub
End Module
Public Class RedNinja
Inherits Ninja
Implements IThrowingStar, ISword
Public Sub New()
End Sub
End Class
Public MustInherit Class Ninja
private curHealth as Integer
Public Sub New()
curHealth = 100
End Sub
Public Function getHP() As Integer
Return curHealth
End Function
End Class
Module Module1
Sub main()
Console.WriteLine("Type any character to continue.")
Console.ReadKey()
Dim a As New RedNinja
a.swingSword() 'prints "Sword has been swung"
a.throwStar() 'prints "Star has been thrown"
Console.WriteLine("End of program - Type any key to exit")
Console.ReadKey()
End Sub
End Module

But I guess you're probably programming in C#, and this is language agnostic question, so here goes language agnostic answer: check out composite and factory design patterns, that should give you some ideas.

Also, it might not be needed to pass everything in constructor. Check out IoC pattern as well.

'dynamic proxies in java' was a very good read. Do you know offhand if this functionality is provided in vb.NET? I will look it up if this happens to be the best solution.
–
PhilJun 17 '11 at 13:32

Well, it turns out that C# has something like it (TransparentProxy) and I see that there are several libraries that build on it to make it more like dynamic proxy in Java, but I don't know enough about .NET to give you anything more specific.
–
DomchiJul 22 '11 at 22:12