dcsimg
A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 49

Thread: OOD/OOP best practice question

  1. #1
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508

    resolved OOD/OOP best practice question

    I've attached a document illustrating my question (2 diagrams)...in general, what is the best practice for communicating between the Document class and other classes it makes use of. Should each supporting class be able to communicate w/ each other or should all communication go through the Document class?

    If classes should communicate directly, what way can I implement this and does that violate the purpose of information hiding/abstraction? I thought about creating Interfaces and implementing them for getters/setters, but I'm not sure how to go about that?
    Attached Files Attached Files

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    The main purpose of the Document class is that it is the root class of the movie, which can be either Sprite, if there is no timeline or MovieClip, if there is a timeline. In a complex movie I just define a public static variable, which is accessible from other classes to add and manipulate objects.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    thx for the feedback, can you give me an example of this and how it would apply in the scenario I described?

    Say I want Class 1 & 2 to communicate to transfer some data, how would you accomplish that? thx again!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I looked at your diagram, and the answer is "it depends". There is no one "best practice" that fits all projects. Personally, when I write things I try to keep it conceptually clear first, and performance optimized second. A good heuristic for conceptual clearness is to ask yourself: "If I gave this code to someone with about my skill level who's never seen it before, could they understand/use/maintain it?" And I also ask myself (because I've been bitten before) "Will I know what the **** I was thinking when I open this project up again after 2 months doing something else?"

    If this is related to your other questions about event dispatching and whether to create a new Event type, I would suggest a new Event type IF the re-used event actually means two different things, semantically.

    The event dispatching/listening model is really neat, and allows things to be put together like lego blocks, but it's not always the appropriate solution. Sometimes class composition, or a well defined interface is a better approach to getting different instances of classes to deal with each other meaningfully.

    That was a big ol' non-answer, wasn't it?

  5. #5
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    See attachment. It is a bit more complex.
    Attached Files Attached Files
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  6. #6
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    5Tons, yes I agree that every situation is different, but I guess my question is being directed toward an application of higher complexity, like the one I'm working on now. Classes need to be able to pass data back and forth and I'm trying to figure out what's the best way w/o comprising good design, for my own sake I guess. I like to write clean code. So my situation was really, do I use the Document class to pass class instances to other classes when they need access to data from another class, do I create some sort of interface for passing data (which I don't know how to do), or do I just have a class send all of it's info back to the Document class and have it distribute that data to other classes (my current approach)?

    cancerinform, so I see what you are saying now. You create the root as a public static variable, my question is, how do other classes reference that root variable from their methods? how do you implement it? do you pass each class the reference to "_root" so they have access to all other object's on the main timeline? thx again!

  7. #7
    Member
    Join Date
    Jun 2005
    Posts
    56
    "Will I know what the **** I was thinking when I open this project up again after 2 months doing something else?"

    -- the most important question you should always ask yourself when you're writing any code!

    But no matter how hard you try when you open it again even when you've got more comments than code it still takes a long time to get your head around it again!

  8. #8
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    you out there cancerinform? just checkin'

  9. #9
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Hey yeah, I am always checkin'
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  10. #10
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    Quote Originally Posted by absolutezero342
    cancerinform, so I see what you are saying now. You create the root as a public static variable, my question is, how do other classes reference that root variable from their methods? how do you implement it? do you pass each class the reference to "_root" so they have access to all other object's on the main timeline? thx again!
    ok, can you answer this question? thx!

  11. #11
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Also ask, could I take this class (perhaps not the document class as thats pretty specific to the implementation at hand, but any other classes), import it into another project, instantiate it, and use it without
    having to modify any of the code in it.

    ps. did not actually look at the doc, but saw the thread and thought I would throw my two cents in (and that may actually be all its worth)
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  12. #12
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Because I don't use any scripts in frames and have only one frame, I reference to objects either over the static variable or over other objects using parent for example.

    could I take this class (perhaps not the document class as thats pretty specific to the implementation at hand, but any other classes), import it into another project, instantiate it, and use it without
    having to modify any of the code in it.
    Regarding reusing classes I have a set of classes, which I use for all movies and which provide different functionality. If there is something missing I add it, so the library grows.

    Otherwise every movie is unique.

    Another possibility is to use only the Document class, on which the movie is built.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  13. #13
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    Quote Originally Posted by cancerinform
    Because I don't use any scripts in frames and have only one frame, I reference to objects either over the static variable or over other objects using parent for example.
    yes, that is what I'm doing also, can you plz give me an example of how to implement referencing other objects through the static variable and using parent also...it's nice to know my options. thx again!

  14. #14
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Haven't you downloaded the example I attached? It's all in there.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  15. #15
    Palindrome emordnilaP Diniden's Avatar
    Join Date
    Feb 2008
    Posts
    230
    I have now had this exact same discussion with someone of a pretty high programming caliber :P(I didn't read all the posts but here is my input on the matter).

    We pretty much came down to this:

    If you want functionality/reuseability of your script you will ALMOST ALWAYS never send other objects directly into another object. Once you start sending specific objects to another object, that starts tieing down that one object to having to be a "part of a whole" to operate. Full functionality always keeps everything totally open so that the object your considering can be used for any case that may need it (ie- a tile based gameboard object...this object will always have data capabilities of tile information dealing...this can be clearly seen to be used in ANY given tile based game...HOWEVER if for one project you design this board so that it needs a "certain tile data object" to be passed into it, this very well can limit future uses of your board since other games most likely have different tile data types they will be using...)

    Now thinking functionally is actually a quite more challenging subject for OOP objects...now thinking about "practical and performance", near direct communication between objects can be considered almost essential. To design a simple game with menus etc (even like a mere 2 player pong game) with FULL OOP functionality could take you a week. This game, however, is quite a small minor simple task, so by all means throw in communications with objects all you want...it can't get all THAT tied up and confusing if it's just pong...

    Now handling Objects when you do let them communicate with each is another topic...After my discussion we decided it probably best to have a controller object (or use the document class) to have everything organized. So, store all your objects via arrays (can have multiple arrays organizing different object groupings) and then pass those arrays to the objects to allow full communication...this doesn't cause memory issues since passing arrays only passes reference and is well worth the organization. Also, using a controller object (or interfaces) helps GREATLY when dealing with event handling...in most cases it is typically good to run all the same event types under a single loop (ie- all onEnterFrame events loops through all of the objects functions of enterFrame under one ENTER_FRAME handler...)

    So from there, it's all about how well you code. A lot of coders like making their code so obvious it's like reading a book (ie-labeling their variables well, not using complicated performance algorithms to accomplish what can be explained typed out in simpler logic etc).

    And sorry if I didn't make much sense...getting late, but so far this is how I have come to accept OOP in my last weeks of OOP orientation :P

  16. #16
    Senior Member
    Join Date
    Jan 2008
    Posts
    150
    I was taught to do my code like a book.

    I still ended up with the lousy habit of passing objects right into other objects for easier access though.

  17. #17
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    OOP is all about intent, if you are going to make an extensible open source project that needs to take into account every possible use, your design will look different than if you were coding the exact same functionality as a final hack into your almost finished game.

    I generally prefer one of the many ways to take advantage of polymorphism as opposed to Events when trying to separate logic, especially in flash.

    I read "Premature generalization is the root of all evil" somewhere (A spin on Dijkstra's original quote) and its true, OOP can be taken too far, don't overkill it.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  18. #18
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    1. When I create a calendar, I create a calendar and I am not thinking to reuse all the code for a shoppingcart.
    2. I try distributing the code into many different classes. Several of these classes can then be reused for other purposes (See the Flashscript classes). I am currently doing that for all my projects including those public ones on my site. You can see a simple and typical example here:
    http://www.flashscript.biz/flashas3/...slideshow.html
    3. Using parent of objects not necessarily results in unique "only-one-time-use" of code, if point 2. is taken, which means you use function parameters. Some parameters can be used for different datatypes (principle of polymorphism).
    In fact in AS2 using parent is a very good habit, since it will avoid the use of _root, the problem of most AS2 movies.
    4. For some purposes I create a superclass, which saves writing code and just referring to that class.
    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  19. #19
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    cancerinform, I've looked at your code and it kinda brings me back to my original question...you have a class called "Controller" (which is the same functionally as my document class) and you import your document class and you create a variable called "_root" that is an alias for the "public static var _root:Sprite;" in your doc class and you use that to manipulate the main timeline, but do you have any object interaction or message passing and if so, how do you use "_root" to communicate between your "Arrange" class and your "AnimationCompleted" class (if you wanted to hypothetically)?

    this goes back to my diagram...I know how to communicate between my doc class and other classes, but how does Class 1 talk directly to Class 2? if you can use "_root" to do it in this way, how? also, if you can do it this way, that means you have to import your doc class or "Controller" class into every class that needs interaction, is that correct?

    thx again, just trying to gain understanding!

    PS...as a follow-up, I've tried accessing an object of Class 2 instantiated in my doc class (Main) from Class 1 using every possible combination of:

    root.obj
    this.obj
    root.parent.obj
    parent.root.obj

    and anything else I could think of...I keep getting:

    ReferenceError: Error #1069: Property obj not found on Main and there is no default value.

    I have a variable in Class 2 called "timeline" which is an alias for the document class' "this" and I have no problem using it except when trying to communicate to other objects!!! plz advise!
    Last edited by absolutezero342; 02-18-2008 at 07:17 PM.

  20. #20
    absolutezero3424
    Join Date
    Nov 2006
    Posts
    508
    can someone plz look at post #1, open the word doc, and tell me how to make Class 1 & Class 2 talk to each other...PLZ...I'm getting desperate!
    seems like an easy answer...and I'm sure I'm just not understanding something, but plz clarify.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center