0

The exercise is about making a software to manage a Robot Factory.

In my program I can create single parts and then robots with them but I also can have robots with smaller robots inside. That's where the Composite comes in. In fact the Component class could be a Part and the Composite class the Robot that implements a list of Parts and inherits from the Part class (that's because a robot can also be made from a single part)

Everything points to this solution but the problem comes when they told us that there are 2 types of Parts; Terrestrial and Aquatic and a Robot cannot be made with different types of parts.

I don't know if this is actually a viable way because the diagram implies I can have a Robot made with both kind of parts even though I'm going to limit it within the code.

Here's an UML diagram

enter image description here

4
  • What is the question? Commented Apr 23, 2013 at 23:01
  • I think if he UML is okay or how it can be done Commented Apr 23, 2013 at 23:02
  • 1
    btw I think you are going to have an abstract robot, an aquatic robot and an terrestrial robot , and obviously the aquatic robot with a list of aquatic parts , an dthe terrestrial terrestrial parts Commented Apr 23, 2013 at 23:03
  • Is my diagram solution correct? Edit: I just read your answer. That's interesting. Commented Apr 23, 2013 at 23:05

2 Answers 2

2

Here we have 2 solutions for this problem:

Robot UML

They are basically the same, although you are sparing the use of the "xor" restriction on the right, and trading it for added complexity.

There is a subtle difference between this 2 solutions. On the left, you can't have a robot without at least 1 aquatic or terrestrial part (which wasn't mentioned initially). On the right, you can have a robot without parts (which wasn't mentioned either).

By changing multiplicities in both solutions, you can choose between a robot with no parts at all or a robot that needs at least 1 part which isn't a Robot. This isn't ideal, and the Composite pattern doesn't suffer from what I've just mentioned. I can't picture a better solution though.

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

1 Comment

First of all thanks for the help. Increíble que sea un compatriota el que me contesta primero! I'm not getting something tho, in your examples we can't say the Robot inherits from Part. Isn't that defeating what the composite is?. For example if I follow this dofactory.com/Patterns/PatternComposite.aspx#_self1 my Component is Part and the composite is Robot, should't my robot be inheriting from Part to get all it's methods?
1

Here is a CodeReview article I wrote on Multiple Inheritance and the Composition Pattern using generics.

https://codereview.stackexchange.com/questions/14542/multiple-inheritance-and-composition-with-java-and-c-updated

This might give you some ideas of how to write the implementation. It appears that what you are talking about, in some respects, is polymorphism.

Firstly, yes you can use the composition pattern to achieve your goal, but do not forget the use of interfaces as well!

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.