开发者

Collections of Objects OOP

I'm interested in a general concept, how would you organize your code ( what classes would you use) in the following context.

You have to deal with articles/nodes or content. The name doesn't matter the concept is the same. Each object has 10-20 attributes.

The problem starts when I need to work with this objects. In order to ke开发者_运维知识库ep the code clean I try to move most of the operations in one or more classes.

The first method was to use a generic class called NodeManger. As you can imagine the methods stared pilling up so the only way to solve this was to start refactoring the code into several smaller classes with special purpose like NodeStorage, NodeConverter, NodeViewer, NodeBuilder and so on.

The operations you have to do on this collections seem few at the begging but in time they become bigger and bigger - you have to store the nodes in different databases ( CMS ) , each of them has a different naming structure - you have to extract the information from different tables - you can get via API different inputs that with different names for the attributes but in the end it's the same object - you have to select subsets, extract, filter, delete and so on.

So the questions are : Am I on the right track ? What would be an abstract structure you would devise in order to cope with these problems and be open to new operations that might appear ?


Remember that HasA (containment) relationships are GENERALLY a better choice than IsA (inheritance) relationships: it's probably better for your NodeManager class to contain several other objects which can act on data in various ways, rather than all of your objects extending objects which extend OTHER... well, you get the picture. It sounds like you're trying to design like this already, so I may just be preaching to the choir, but multiple inheritance gets out of hand really quickly, so it bears repeating.

With that said, sometimes you can't get away from inheritance, and most of the functionality that you're looking for can probably be implemented via abstract classes: create the abstract stubs of functionality you need, and then implement them on an as-needed basis for each of your various cases, so that you don't have to track that explicitly within the code (you mentioned different database connections with different naming conventions, for example).


It sounds to me that you need to look into Polymorphism and Inheritance.

  • http://php.net/manual/en/keyword.extends.php
  • http://php.net/manual/en/language.oop5.abstract.php

The above are good places to start looking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜