locked
OOP object association and best practices RRS feed

  • General discussion

  • I'm seriously learning how to design a good OOP class. I've read a lot of articles online and I am always cannot figure out the best way to design a good oop class. For my study, I was trying to model a music track. For example, we know that a music track has a artist, associated with album and genre , etc... For illustration, I came up with this ridiculously bad design 

    As you can see, this is not a good design since I would love to:

    1. Associate a track with an album, artist and genre so that if I modify a track all its atributes are also modified
    2. Use OOP concepts such as inheritance and polymorphism 

    Can someone please help me out? I'm so confused and I have been doing this for a long time.

    • Changed type Mickey Mouze Monday, February 27, 2012 3:49 PM
    Monday, February 27, 2012 1:58 AM

All replies

  • I think to come up with better design, if you read some book on OOAD i.e. Object Oriented Analysis and Design. that will help. you can consider OOAD by Booch. Design pattern book is also recommended to relate, compare and identify the pattern of your problem and their solutions, limitations etc. You can consider Design pattern book by Eric Gama et all (famously known as gang of four).

    However for your task I'll suggest you to first write the user story i.e. how your user wants to use the software, better if you identify the use cases. Once you will be done with the user story just go by the rule of identifying the class and methods, i.e. every noun is class and every verb is method. Another good thing to remember while designing is "encapsulate whatever varies". use inheritance if you want to extend the functionality of your class and polymorphism if you want to over-ride the default behavior.
    Please remember design is something which comes with experience and your willingness to make your own design better, so keep your zeal up and refine your design.

    Friday, March 2, 2012 4:49 AM
  • When modeling, it can be very difficult to "weave" the model in a designer. 

    In addition to what Haider said, I would also suggest that you take your design and just start building an application with it. Pay attention to what works and what doesn't, and try to change the model to provide for what works. While I'd never suggest charging into an actual enterprise application with a half-baked design model, doing so on a personal project to get a better handle on OOD is a great learning exercise. 

    Another alternative is to just get a good old paper and pencil and work on that instead of wrestling with software. 

    Design is always an iterative process about putting something down, then working through it to get an idea for what works and what won't. As you gain more experience, the trial and error will be about pitting your model against use cases to see what breaks. 

    Best of luck!

    Philo


    Philo Janus, MCP Bridging business & Technology: @ http://www.saintchad.org/ Author: Pro InfoPath 2007 & Pro InfoPath 2010 Pro PerformancePoint 2007 Pro SQL Server Analysis Services 2008 Building Integrated Business Intelligence Solutions with SQL Server 2008 R2 & Office 2010

    Tuesday, March 6, 2012 11:49 AM
  • Thanks everyone who replied. Following your suggestions, I just finished reading " Head first design pattern". I know this is a java book but it should fairly easily translate into .net. I know this is a kinda of stupid question to ask but I am now confused as hell on which design pattern to use to solve my problem. Can someone please suggest the best design pattern that can be used to model my music player objects? The music player will be just used as a music player, i.e it will have at least the following methods:

    • Play
    • Pause
    • Stop
    • PlayNext
    • PlayPrevious

    Can please someone suggests which pattern to use and why? Remember this is just for learning purposes so don't worry about the deadlines. Also it won't hurt if there is any other tutorials I should read to expand my modeling knowledge.

    Peace,

    Tuesday, March 27, 2012 5:35 PM
  • Firstly you should go through your classes in the first post and recreate them much smaller.

    USE INHERITANCE :)

    For example you have to props: FileName, FileSize in the AudioTrack. You could start with your hierarchy by creating a "File" class with the mentioned properties.

    Make also use of INTERFACES. There are probably thousands of solutions but you could start with some kind of this:

    Create a IPlayable Interface. The AudioTrack class implements this which provides among other things the Duration property.
    Now you can create a class that handles the play. For the beginning we call it "Player". This class usese a Method to Load a Tracklist ->

    public void LoadTracklist(IList<IPlayable> tracklist)
    {
    //save list to a field for navigation 
    }

    How the player plays your music depends on what tec. you are using.
    WPF:
    http://social.msdn.microsoft.com/Forums/en/devdocs/thread/1329f8dd-ae71-4164-b5e3-2780f89c7e07

    Tuesday, March 27, 2012 7:20 PM
  • Thanks very much. I figured it's always good to learn from my mistakes and most importantly learn from experienced programmers like you
    Tuesday, March 27, 2012 9:08 PM