Enterprise Integration Zone is brought to you in partnership with:

Ayende Rahien is working for Hibernating Rhinos LTD, a Israeli based company producing developer productivity tools for OLTP applications such as NHibernate Profiler (nhprof.com), Linq to SQL Profiler(l2sprof.com), Entity Framework Profiler (efprof.com) and more. Ayende is a DZone MVB and is not an employee of DZone and has posted 452 posts at DZone. You can read more from them at their website. View Full User Profile

Design Patterns in the Test of Time: Composite

11.20.2012
| 3117 views |
  • submit to reddit

 

The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

More on this pattern.

This post is supposed to come on the 1st of Nov or there about, which means that I’m going to do a small divergence into politics now. Here is an example of how the upcoming presidential elections in the states.

        public interface IVotingUnit

        {

            int Weight { get;set; }

            int CandidateId { get;set; }

        }

         

        public class Voter : IVotingUnit

        {

            [Obsolete("Racist")]

           string Id { get;set; }

        

           int Weight { get;set; }

        

           int CandidateId { get;set; }

       }

        

       public class WinnerTakesAllState : IVotingUnit

       {

           string StateCode { get;set; }    

        

           int Weight { get;set; }

           int CandidateId { get;set; }

        

           public void AddVote(Voter vote){

               // calculate

           }

       }

Yes, this is probably a bad example of this, but I find it hilarious.  The basic idea is that you can have an object that represent many other objects, but show the same external interface.

Composites are usually used for the more CS style of programming. Composites are very common in tree structures such as compiler AST or DOM. But beyond that, I can’t easily recall any real world usage of them in my applications. There are usually better alternatives, and you have to remember that when you speak about enterprise applications, loading / storing the data is just as important. And trying to implement the composite pattern under those circumstances will lead to a world of hurt.

Recommendation: If your entire dataset can easily fit in memory, and it make sense, go ahead. Most of the time, you probably should stay away.




Published at DZone with permission of Ayende Rahien, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sebastian Mueller replied on Tue, 2012/11/20 - 3:23am

Your example is probably the worst example one could give for the composite pattern. Please take the time and read more on the subject before posting. Did you actually read the wikipedia link you were posting?

Don't give recommendations about using or not using the pattern unless you understand that pattern - and obviously you do not, since you seem to never have (knowingly) implemented that pattern yourself.

Sorry for sounding harsh, but this "article" does way more harm than good. For those who don't know about the pattern you provide misleading advice and for those who know the pattern it's just garbage...

Lund Wolfe replied on Thu, 2012/11/22 - 6:30pm

You do need to use your judgement to determine when a design pattern fits your situation, and you need to have a good grasp of OO and some experience before you can appreciate the patterns.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.