I have an application that lets users create their own character from clothing like hat, pants and facial features, like beard and eyes. I'm using a canvas to display the built up character. The order in which these items get drawn are important, ie. the body of the character should appear behind the pants.

My teacher told me to use the Composite pattern to store the items that are currently on the character and to draw my character in the correct order with the use of the Decorator pattern.

I currently have two lists, one which contains the facial features, and one for the clothes. I just can't imagine a way to make the drawing work with the Decorator pattern, the only way I see is to actually hard - code the order like this: create a character, create a CharacterWithBody decorator and instantly add it to my character, than ie. create a CharacterWithHat decorator and add it to the character, but for now it would not actually containy any image of any hat, it would basically be empty. Later on when I add an item to the fully decorated character, through the added item's class I'd know which decorator should store the image (or any information).

So basically this would be my inital character:

character -> bodyDec -> eyesDec -> mouthDec -> beardDec (empty) -> hatDec(empty)...

I think this could work but I don't feel that this would be a good approach. Any advice would be helpful. Thanks!

  • 1
    Possible duplicate of Choosing the right Design Pattern
    – gnat
    Commented Nov 19, 2016 at 14:14
  • not at all, this is a specific problem while that one is a general quesiton
    – masm64
    Commented Nov 19, 2016 at 14:39
  • 5
    Nevertheless, that's still a good read. You should read it, especially the part that says "Design patterns are not building blocks." Also, if your teacher told you to do it a certain way, you should probably at least try to do it that way first. Commented Nov 19, 2016 at 14:51

2 Answers 2


Do you want to build your character in code or do you want to make it easy for a user of your software to build a character using an interface? These are vastly different use cases. Until you say different I'll presume you simply wish to build in code.

I'm not sure why you'd use both the composite and the decorator pattern here. Either seems to do the job.

The decorator pattern would let you write construction code that looks like this:

Character exampleCharacter =
   new HatDefault(
   new BeardDefault(
   new MouthDefault(
   new EyesDefault(
   new BodyDefault(
   new CharacterDefault()

The Character interface would have a display() method so you could call exampleCharacter.display() and Hat would see it first, turn around and call display() on Beard, then Hat would wait for Beard to return, once Beard returns a beard, and everything else, should be part of the image and Hat can now draw a hat on top of the image.

The composite pattern would let you write construction code that looks like this:

Character exampleCharacter = new CharacterDefault();
exampleCharacter.add( new HatDefault() );

Character face = new FaceDefault();    
face.add( new MouthDefault() );
face.add( new BeardDefault() );
face.add( new EyesDefault() );

exampleCharacter.add( face );
exampleCharacter.add( new BodyDefault() );

Again you'd call exampleCharacter.display() to draw this image. This time instead of the call moving from decorator to decorator in a straight line we'd flow through the structure. CharacterDefault would be the first to see the display call. It would loop through to call all of the Character members in it's collection: body, face, hat. When it got to the face the face would loop through its collection: mouth, beard, eyes.

Either one lets you control the order things are drawn. Composite lets you manage sub lists.

I don't see a good reason to mush them together here.


First of all...

Patterns are not made up from you or told to you. They were identified by the perception of the problem domain.

If your teacher told you to use pattern X and Y then he made some assumptions you have to know as we have an arbitrary solution space for every problem. The point is: what is his idea and reference to come to this conclusion?

I do not essentially disagree on the suggestions your teacher made. He maybe has gone through a process identifying what patterns he identified on the problem domain.

Some thoughts...

Do your eyes hold your body or does the body hold your eyes? The assumption I would make is that your body hold your eyes. But that is an arbitrary decision even it seems plausible to me.

Maybe you want to distinguish between your body and your head. Maybe your eyes hold your head? I am convinced vice versa. But that again is an assumption I made.

If you follow this path you may come up with something like this: Your body consists of a head, a torso, two legs, two arms. Your head consists of two eyes, a mouth, one nose, two ears. Both arms consist of a upper arm, a forearm and a hand. A hand consists of 5 fingers. A finger consists of ...

These are all assumptions of MY perception of reality and my way to model it. At some point I omitted details as I do not see them as relevant. I "abstract" from them as they may not be part of the problem to be solved.

If you build up your body like this you come up with a tree structure. A tree structure is properly mapped to the "composite pattern".

If I put on cloths I would first put on a T-Shirt and then a pullover. I could do otherwise even I would feel strange. The same with my underpants and my jeans.

To avoid those "strange" things you introduce the assumption that it matters in which order cloths are put on. This may be a design decision you want to enforce. So you want order in some way. Order can be achieved through a list.

If you put on cloths, jewelry or a wristwatch you "decorate" yourself. A forearm can hold a wristwatch, your ear can hold ear rings, your torso may hold a T-shirt, a pullover AND a jacket in this order.

So every element of your body may have a list of decorations. The thing is that these decorations may overlap AND be composed again. Your trousers can be decorated with a belt and trouser clips. So I guess this will be a tree as well.

To break it down: My model includes a tree structure of the body, a tree structure of decorative elements AND a connection between them. The tree structures can be represented with the composite pattern, the connection between them with the decorator pattern.


It all depends if you can grap the problem domain. You have to identify the structure of it to identifiy patterns. The ability depends on logical thinking and reference points to compare with (e.g. experience and perception of reality).

So my thoughts may differ on different reasons from others. I ...

... may not have a whole understanding of the problem domain

... have a different (maybe wrong) perception of reality on this issue

... introduced artefacts that are irrelevant

... omitted artefacts that are relevant


I strongly recommend to learn the application of design pattern. You should not think about HOW to implement it anymore once you are sure you identified the pattern in your problem domain.

  • Any explanation for downvoting?
    – oopexpert
    Commented Nov 20, 2016 at 13:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.