This discussion is archived
10 Replies Latest reply: Apr 2, 2010 6:09 AM by 800387 RSS

Real structures for this design pattern

843853 Newbie
Currently Being Moderated
Hello , this is my first time posting and I hope to be doing it in the right section. I have a school assignment that says the following: Identify real or abstract structures that could be modeled by the class diagrams represented below: [http://img704.imageshack.us/img704/3523/diagrams.png]

Upon searching I found out that this is a composite design pattern. I have to implement 2 scenarios for this class system. I was thinking of taking class A (from the uploaded image) to be the Software ( more like a program oriented towards a certain domain like multimedia ), B1,B2 could be an Application/Compiler, and the composite class (D) is an Operating System( because is is usually compiled out of many software (program) pieces) . I want to ask for your opinions on this example because I'm not sure whether this is correct or not. If it isn't could you please give me a real-life example of a correct model ?.
  • 1. Re: Real structures for this design pattern
    800387 Newbie
    Currently Being Moderated
    The classical interpretation of the pattern is for a hierarchical or tree structure, where the differences between nodes and leafs are factored into a least common denominator interface. Think of a file and a directory. Both have a name, a create date, permissions, etc. However, at some point one directory will be root. That one is slightly different. Then for a file, it will have a size and the ability to read and write from I/O streams.

    - Saish
  • 2. Re: Real structures for this design pattern
    843853 Newbie
    Currently Being Moderated
    Roronoa_Zorro wrote:
    Hello , this is my first time posting and I hope to be doing it in the right section. I have a school assignment that says the following: Identify real or abstract structures that could be modeled by the class diagrams represented below: [http://img704.imageshack.us/img704/3523/diagrams.png]

    Upon searching I found out that this is a composite design pattern. I have to implement 2 scenarios for this class system. I was thinking of taking class A (from the uploaded image) to be the Software ( more like a program oriented towards a certain domain like multimedia ), B1,B2 could be an Application/Compiler, and the composite class (D) is an Operating System( because is is usually compiled out of many software (program) pieces) . I want to ask for your opinions on this example because I'm not sure whether this is correct or not. If it isn't could you please give me a real-life example of a correct model ?.
    Unless your assignment specifically required you to use a computer analogy I would use something else, and even then, I'd still use something else first to understand it better.Since OperatingSystem objects aren't composed with other OperatingSystem objects this doesn't really fit the pattern. Now of course you may be able to run several virtual operating systems inside one but that stretches the example too much for my taste. A directory tree may be seen as an example of the composite pattern -- class A can be a Node, different types of files are specializations of a Node (B1, B2, ...) and are not composable (can't put one file inside another), and a Directory (D) is a Composable specialization of a Node -- it can contain other Nodes. [Edit: I see Saish beat me to the file system analogy]

    With real-world examples you have to be careful as some obvious examples that seem to correspond to computer world, such as containers, boxes, etc, don't actually work, e.g. boxes aren't composable because if box1 fits into box2, then box2 won't fit into box1. Power extension cords and surge protectors are composable extensions of Pluggable class, though not recommended that you take advantage of that feature too much lest you burn your house down. Bags are composable in the real world since they aren't rigid and can be folded up -- class A would just be Object, class B1, B2, etc would be stuff that isn't composable like Apple and Orange, and D would be Bag. You can put Apple, Orange, and Bag instances inside another Bag instance.

    Maybe we should have a contest here -- who can come up with the most creative real-world analogy to the Composite pattern? I know mine are pretty lame.

    Edited by: fromrussiawithjava on Mar 30, 2010 5:36 AM
  • 3. Re: Real structures for this design pattern
    843853 Newbie
    Currently Being Moderated
    Thanks to both of you for these answers.It has helped me ways. I have decided to use the file system example. If is possible I have another request. In our assignment it is mentioned that we discuss about class elements that are inherited overwritten, overloaded. Also we need to implement 2 scenarios (with an interface using swing) for the class system . Could you please suggest me 2 scenarios from which to identify the polymorphic aspects of the class system? I made a rough sketch but it is missing many key elements( due to the fact that I don't have the scenarios in mind to know in which direction to go). Here is the rough draft I made so far:
    For class A I chose AbstractFile( the name is not fitting as I do not plan on making it an abstract class, but I couldn't think if another). Its attributes are: path, size, upperDirectory, created, read, write (list is still open). Methods: execute (the reason for making it a method is explained below), hide, delete, rename, getSize, getCreated, getPath, ( the list is still open).
    For class D I cose Directory. Atributes semnification for directories: read-> the right to be listed, write ->the right to create new files/dir in the current directory, . Methods: overloaded should be: getSize ,rename, delete,hide. Overloaded: execute->can be vizited during a search ( due to the fact that the meaning is different I though to make it a method).
    As for the classes D1,D2 I have no idea what what to implement due to the fact that I don't know what to show in the interface so that the polymorphic aspects can be outlined. My requests is if you can so kindly give me 2 scenarios for the class system from which polymorphic aspects can be deduced , and your opinion of my sketch so far and if you can indentify further aspects(attributes, overriden / overwritten methods forgotten and that can be outlined in a scenario) please share them with me. I'm sorry if I have gotten off the topic of my title, but these things seem very hard for me and I don't know the terminology of many things to search for with google(not to mention that not all examples are concludent, or correct). Thank you once again for your time.
  • 4. Re: Real structures for this design pattern
    800387 Newbie
    Currently Being Moderated
    Your interface should have all the common methods in AbstractFile. Directory would presumably have a method listChildren(), otherwise you could never see the sub-directories and files contained. AbstractFile probably has a getParent() method in it. Have no idea what hide() would do. If you imagine a UI, then all kinds of additional events are possible (e.g., double click directory to open a directory, double click file to execute a program, single click either to get more information or to generally select, etc.)

    - Saish
  • 5. Re: Real structures for this design pattern
    843853 Newbie
    Currently Being Moderated
    That's what I'm afraid of doing, because I believe it can get complicated and way past the purpose of the assignment. I was looking for simpler implementation. I thought that the hide method should turn on/off the visibility of the file (directory). I also wondering what other subclasses I could get out of AbstractFile. Also thank you for the method suggestion, indeed I will need them.

    Edited by: Roronoa_Zorro on Mar 30, 2010 2:03 PM

    Edited by: Roronoa_Zorro on Mar 30, 2010 2:05 PM
  • 6. Re: Real structures for this design pattern
    800387 Newbie
    Currently Being Moderated
    Roronoa_Zorro wrote:
    That's what I'm afraid of doing, because I believe it can get complicated and way past the purpose of the assignment. I was looking for simpler implementation. I thought that the hide method should turn on/off the visibility of the file (directory). I also wondering what other subclasses I could get out of AbstractFile. Also thank you for the method suggestion, indeed I will need them.

    Edited by: Roronoa_Zorro on Mar 30, 2010 2:03 PM

    Edited by: Roronoa_Zorro on Mar 30, 2010 2:05 PM
    Well on Unix, just about everything can be thought of as a file descriptor. Devices, external mounts, symbolic links, etc. Take a look at java.io.File for ideas for more operations.

    - Saish
  • 7. Re: Real structures for this design pattern
    843853 Newbie
    Currently Being Moderated
    Thank you so very much for pointing out the java.io.File package. However I couldn't make much out of symbolic link or devices. From what I read they (the symbolic links) just store the path name to the file or directory but considering that it will inherit from AbstractFile many of it's methods and attributes (mentioned in one of my above posts) will not be needed, and I don't know how to deal with that situation because I need to talk in my assignment about these attributes and overloaded/overwritten methods. I'm sorry if I spoke too soon without a proper documentation, but if it is possible to implement the Devices and SymbolicLink classes accordingly please give me a reference to a documentation. I was thinking of specializing AbstractFile with something like ImageFile, VideoFile, BinaryFile, ExeFile. Do you think it will be correct because at the moment I'm unable to pinpoint the exact methods that need to be overwritten/overloaded(the specialized behavior and how to show it in my UI ) nor the attributes(that should be added).
  • 8. Re: Real structures for this design pattern
    800387 Newbie
    Currently Being Moderated
    Whatever you can imagine. You could have MP3File, and display information about the artist, song, genre, length, etc., instead of its size and create date. It's all up to your own creativity.

    - Saish
  • 9. Re: Real structures for this design pattern
    843853 Newbie
    Currently Being Moderated
    Thanks so much, with this I think I have my image plan all formed out
  • 10. Re: Real structures for this design pattern
    800387 Newbie
    Currently Being Moderated
    Best of luck.

    - Saish