Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

OOP 101 advice (please)

Guest
Sep 26, 2013 Sep 26, 2013

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.

TOPICS
ActionScript
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2013 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 26, 2013 Sep 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 26, 2013 Sep 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2013 Sep 26, 2013

if you use a static class, it means all your objects will use that one static class.  you won't have instances of the scroller class.

that class will handle all the events etc.  eg,

package {

       public class Scoller {

               // import whatever needed

              public function Scroller() {

              }

              static public function addScoller(obj:MovieClip):void{

                     obj.mask.addEventListner(MouseEvent.MOUSE_MOVE,f);

                    params(obj,0,obj.mask.y,obj.mask.height,obj.mask.y-obj.scrolled_mc.height+obj.mask.height);

              }

               private function f(e:MouseEvent):void{

                    obj.scrolled_mc.y=MovieClip(e.currentTarget.parent).m*localY+e.currentTarget.parent.b;

               }

               private function params(mc:MovieClip,x1:Number,y1:Number,x2:Number,y2:Number):void{

                    mc.m=(y1-y2)/(x1-x2);

                    mc.b=y1-mc.m*x1;

               }

       }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 26, 2013 Sep 26, 2013

klgad: celebritymusic asked for an OOP solution, which Singleton is not and static Classes are not. You use Singleton/statics when you can't figure out how to get the right reference to the right object, so you just give everybody a reference to it and damn the consequences.

It also means that

  1. You can't switch out a different scroller or something that performs a similar function in a slightly different situation.
  2. You can't test the code by mocking the scroller (see 1)
  3. All of the Classes that reference the scroller have a dependency on it, so can't be used for something slightly different but exactly in line with their core functionaliry
  4. It can be really difficult to figure out who, exactly, is calling this global object in a way that is causing a bug. It's like trying to figure out who gave an STD to a prostitute, vs. who gave it to a monogamous person.

In short, bad news. Did you read the links I posted about why global state is bad practice and not actually object oriented at all?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2013 Sep 26, 2013

i know you are a very opinionated individual and i know you are convinced you are right. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 26, 2013 Sep 26, 2013
LATEST

I'm very protective of newbies, especially when it's obvious that they want to follow good practices. I've inherited enough code from people who think junk like Singletons and statics are ok that I feel like I owe it to my fellow developers to try to steer the new ones away from that.

If you want to write that kind of code, go ahead--that's between you and your employer.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines