Skip to main content
September 26, 2013
Question

OOP 101 advice (please)

  • September 26, 2013
  • 1 reply
  • 1505 views

Hi guys

If I create a class (scroller class), and want that class to be accessed by lots of other classes, so that if one class adds a movieclip to the scroller, the previous movieclip in there (possibly from another class), is removed, how do you make it so that the scroller class is affected by all the other classes?

Do you have to

- instantiate the scroller class in your Document class

- inject it's instance into the constructors of all the classes that need access to that scroller

?

or

- create a global model to inject into all the classes, including the scroller, and populate a setter with the movieclip that you want to scroll, and have the scroller get that clip using the getter?

Because if I simply set up a var scroller:Scroller = new Scroller(); in each class, they will all have thier own exclusive scrollers, right? Which I don't want - I just want one that all the classes can affect.

Cheers guys.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
September 26, 2013

create a singleton or static scroller class that contains a public method to which you can pass your objects that you want the scroller class to impact.  eg, the scroller class can add listeners etc to the passed object.  all the passed objects should have the same basic setup, eg a movieclip that "scroll" and a mask that reveals the scrolling movieclip in the region you want visible.

September 26, 2013

Cheers kGlad

But how do I give all of the classes that need it access to that scroller class?

The way I understood it, a static class that receives an object from class5 isn't going to react to receiving an object from class7.

Better way to explain how I'm seeing it:

Take a static class keeps adding numbers it receives to a var total:int;

Class5 sends it 5 - total is 5

Class5 sends it 7 - total is 12

Class8 sends it 3 - total is 3, as it's totally separate from class5.

Or are static classes able to be completely independent of each class, but react to many?

Amy Blankenship
Legend
September 26, 2013

Singletons are bad practice! They are also not object-oriented, but instead are procedural. In other words, the main people who use them are people who actually can't get their heads around how to do Object Oriented Design and so have to revert to the horrible practices they can understand. Don't be that person.

The reason you're having this issue is because your design is bad at the root. All of these Classes should not be directly adding children to a scroller. Instead, they should be dispatching events that describe what has happened in them (that may or may not need to result in something being added to the scroller). The one Controller Class that knows about the scroller should be listening for these events and make a decision about what, if anything should be added to the scroller.

That Controller will then either directly remove/add children to the scroller or it will set a property, dispatch an event, or call a method on the scroller that will allow the scroller to handle whatever needs to be handled internally.