PDA

View Full Version : A little OOP class advice


matbury
11-04-2007, 03:09 AM
Ok, as you know, I'm new to the dark art of OOP, but doing my best to get to grips with it, so this question is for wizards who are well versed with this magical way.

To save me a lot of time and keyboard-head-banging and to get me started in the right direction, I'm making a dictation exercise - not the banana republic kind, the listen and type what you hear kind - and I'd like some advice.

What I have to do is the following:

Load in an XML file containing the 'Timed Text' captions data. The nodes look like this:

<CuePoint>
<Time>5146</Time>
<Type>event</Type>
<Name>If you are codependent, ask someone to press two for you.</Name>
</CuePoint>

(It's actually meant for <FLVCoreCuePoints> XML - so it's a pretty useful standard to go by.)

For each node, split the <Name> tag text into individual words and store them in an array.

i.e. var firstNode1Array:Array = new Array("If", "you", "are", "codependent,", "ask", "someone", "to", "press", "two", "for", "you.");

Make a copy of the array and remove any punctuation (i.e. full-stops, commas, convert upper-case to lower, etc.)

i.e. var secondNode1Array:Array = new Array("if", "you", "are", "codependent", "ask", "someone", "to", "press", "two", "for", "you");

Create input TextFields that correspond to the words in the firstNode1Array array.

i.e. |__| |___| |___| |___________| |___| |_______| |__| |_____| |___| |___ |___|.

Place all the text fields on the stage and put a 'listen' button, loaded from the FLA library, at the front of each sentence like this:

i.e. 'listen' |__| |___| |___| |___________| |___| |_______| |__| |_____| |___| |___ |___| 'listen' |__| |___| |___| |___________| |___| |_______| |__| |_____| |___| |___ |___| 'listen' |__| |___| |___| |___________| |___| |_______| |__| |_____| |___| |___ |___| 'listen' |__| |___| |___| |___________| |___| |_______| |__| |_____| |___| |___ |___|.

Load in an MP3 file.

Listen for when a user clicks on a 'listen' button and play the MP3 file at the corresponding <CuePoint> e.g. 5146 milliseconds.

Listen for KEY_UP events and check the input text of the focussed input TextField against the corresponding string in the secondNode1Array.

Individually, I can write all this code. I could write it all into one, big, unruly, messy class. I think OOP can offer a much better way.

So my UML-type question is this:

Which responsibilities should I divide into which classes and how should the classes relate to each other?

i.e. with regards to namespacing variables and functions, which classes extend which, when to use listeners or share functions, etc.

It'd really help me so much to know how you guys would approach this kind of problem. You've been my 'Open University' for a while now, and I really respect your advice.

Thanks in advance!

Sleeve
11-04-2007, 04:11 AM
You are asking a complex question that has as many answers as there are coding styles.

In my experience, classes can be very, very large or they can be just a few lines of code. Each developer with have their own unique spin on logic and will realize slightly differing styles for coding classes. When I tackle any project, I begin with the inspiration from which OOP was built; 'reusable design'.

The project you describe can be built correctly using just a single class or multiple classes using multiple levels of hierarchy. It's up to you. Keep in mind that base classes should exist when more than one other class may benefit from it. For example, you can build an XML parser base class that takes an XML object and parses it into various data chunks that can be used by multiple components or applications. If the parser is useful to only a single component, it probably doesn't need to be a separate class.

Personally, I see 4 possible classes here: 1 class that loads and parses your XML object. Another for the nuances of the 'listen' button. Another that builds the UI including the text fields and the manages the placement of the 'listen' button and text fields on stage and finally a class that plays the MP3.

I like to think of my classes as building blocks for the application. Instead of creating a single block and painting it 4 different colors, I like to create 4 blocks each with their own color and stack them together to look like a single block.

Just my 2 cents...