1

I have a system of Models:

abstract class R00_Model_iUnique { }
abstract class R00_Model_iFamilyUnique extends R00_Model_iUnique { } // for models with hierarchy
abstract class R00_Model_iTaggedUnique extends R00_Model_iUnique { } // for models with tags

// and, for example
class R00_Model_User extends R00_Model_iUnique { }
class R00_Model_Comment extends R00_Model_iFamilyUnique { }
class R00_Model_Post extends R00_Model_iTaggedUnique { }

There is gonna be R00_Model_iCommentableUnique and R00_Model_Post wants to be inherited from it. But It isn't possible, it's already inherited from R00_Model_iTaggedUnique, and I don't think It's clever to inherit R00_Model_iTaggedUnique from R00_Model_iCommentableUnique or vice versa. I've thought up only one idea how to implement it, but I have some doubts. Maybe you can tell me about some smart methods or criticize that method?


I thought up to make R00_Model_i*Unique not classes, but interfaces, and create helper objects, such as R00_Model_Helper_iUnique (maybe it is a common patern, and there is a cool name, I don't think 'Helper' will be cool there?). Then, in R00_Model_iUnique, create __call(), which checks all the Interfaces of a called object and looks up a called method in the helper.

Or there is too many reflection and other evil slow stuff, isn't it?

4
  • I think designing the class as an interface instead is a good start. Commented Oct 29, 2009 at 6:40
  • you mean, my solution is right or you mean, I should make them only an interfaces and forget about their methods? Commented Oct 29, 2009 at 6:43
  • 1
    As general comment naming your classes R00_Model_* is a bad idea. I imagine that means you will have R00_View_* and R00_Controller_* But you see Model, View and Controller aren't essential to your problem domain They are an artificial artifact that we as developers make to help us organize code. But the purpose of the Model is precisely to model what your domain is. Avoiding that naming convention will make your code simpler in the future to understand and debug. Commented Oct 29, 2009 at 7:17
  • I use prefix 'R00_Model_' as a namespace for everything that comes from somewhere: from database or from webservices etc. For my needs, it covers everything that can be inherited from abstact classes from question. I really don't want to create lots of classes in R00_ namespace. I can name them R00_Data_User etc, but I don't think it will increase any simplicity. Or I understand you wrong? Commented Oct 29, 2009 at 8:09

1 Answer 1

1

You are in the right direction with using an interface and helpers (composition).

This is precisely one of the reasons why on of the principles of the Design Patterns Book (GoF) is "Favor composition over inheritance".

Composition will give you the flexibility you require to use methods of different classes In the class that you need it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.