Topic: inheritance or composite

  Print this page

1.inheritance or composite Copy to clipboard
Posted by: zyzhang
Posted on: 2003-11-03 23:06

It's said in OO Principles that favorite composite,instead of inheritance.

I have problme on implementating it.Suppose, I have three classes a,b,c.a is supper class of b and c , and there are few difference between b and c.Using composite, i put reference to a in b and c, but for every methods in a ,i have to recreate them in b and c(just calling a's corresponding methods), this is tedious work, even though i can get some benefit from the composite.

Is there any comments on it.

2.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: floater
Posted on: 2003-11-04 00:27

I think you misunderstand this rule. It says everytime when you have objects, use this rule *before* you set the relations among them.

Since you already have a relation(a is super for b, c), then forget about it, unless you want to rearrange them.

One exception to the above is the decorator design pattern.

3.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: zyzhang
Posted on: 2003-11-04 01:06

I did want to rearrange(refactor) it for performance improvement,cause i want to pooling a (common object for b and c).The problem is i have to cre every methods signiture in a for b and c(just delegate to a),i do not want to break the client api, but it's tedious work.

Hopefully, you can understand me.

4.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: zyzhang
Posted on: 2003-11-04 17:39

Eventually, I found the related design patterns fitting well to solve this problem. They are flyweight and composite design pattern,the running prototyping works fine and the performance improved dramatically.

5.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: floater
Posted on: 2003-11-04 22:51

Good. Could you share the inside thought(only a b c, not your real code, which might not be shared)? I think it's a good context to show us how to use these two patterns and their intention.

Hehe, I guessed you are interested in the interface, so I mentioned decorator. Sounds like you are more interested in the aggregation (because of the pooling).

So, it's not really about Composite Inheritence Principle(joking, Smile).

6.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: zyzhang
Posted on: 2003-11-05 01:29

The product i am developing on is rules engine. the data object model is a runtime created complex tree structure.There are indeed a lot of similar redundant objects existing in memory,and the performance is bad, but is acceptable.This is the context of the problem.

I did not know much about design patterns before, but i do have experience on the OO Analysis and Design for the complex products development.Yesterday, i did research on the design patterns and found the flyweight design pattern is just fits my problem context: Pooling(sharing) for Common Objects or States for the all objects related.

First, I tried to abstract and create the common object(some leaf nodes in the tree) as flyweights,and put in the pool(I am using apache common-pool).

Second, i refactored existing classes structure according to the composite design pattern, added some inferfaces for all nodes and composite object(with children), respectively.

Third, i modified corresponding methods on the impletation classes to borrow and return the common flyweight object from the pool.

In this way, I can dramatically reduce the number of objects in the memory,and the performance improved sharply(Using JProbe to get runtime profile).

I think there are also some drawbacks for the pattern, cause some of my leaf nodes(in the pool) can have mutiple parents, i have to handle it to fit my program context.

Anyway, the two design patterns generally can work together to handle comple object structure(especially for tree stucture) and share the common objects to improve the performance.

Currently, i am writting and chaging the unit test for my programm,it's also a tedious work.

The real world programming generally is much more complex than the examples from the books,but the books and website can give you a light on the related subject.Hopefully,everyone can benefit from this site and share their experience.

7.Re:inheritance or composite [Re: zyzhang] Copy to clipboard
Posted by: zyzhang
Posted on: 2003-11-06 00:46

I think I'd better to explain how my design idea developped for the solution . The original thought is based on the fact that a,b,c objects are essentially the same objects,but with hierarchy structure defined for them.Also, the semantics between them are composition. My first attempt is to try to abstract the common stuff and pooling them. then, i analysed the objects model and identified the common attributes in these classes,but the class structure are so complex, i need to abstract the interfaces to make them uniformly accessible, at this moment, i realized i should turn to design patterns to get the proven design solution for this common problem.I sent my request to this topic.Currently, it seems that the topic is not fit well to my problem.

Eventually, I found the right design patterns to sort out my problem. These patterns also give me the clue how to abstract the interface and reexamine the existing class structure, this made me quickly go on the right track.

I created a running prototype quickly(3 hours) and adapted to the existing programs(hard work), i modified the existing test cases(hard work) and then released it(happy): performace dramatically improved, cause the objects kept in memory are reduced significantly.

Again, I learned the three design patterns(Flyweight,factory and composite) in the real world application , alone with the decorator that floater
mentioned.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923