Skip to main content
Known Participant
March 18, 2013
Question

OOP - Interfaces

  • March 18, 2013
  • 1 reply
  • 836 views

Is it a good approach to use Interfaces to call methods from others class?

Use the Interface to avoid callback chains or events listeners...

Example:

Class A contains reference to Class B and Class C.

Class C needs to call method in Class B.

Instead of sending callback to C from A that calls method in B, create a reference from Class B interface on Class C and call the method from that reference.

Is this the "good" OOP way?

This topic has been closed for replies.

1 reply

sinious
Legend
March 18, 2013

The role the classes play is pretty critical to the question. Are you following a programming pattern like MVC? In that scenario class A could either be a factory, controller or many other types and based on the 'role' of that class the rules would determine how you should craft the connection.

croxoverAuthor
Known Participant
March 18, 2013

No, I'm not using a pattern like MVC... if I am is pure coincidence!

I'm asking this because I don't know the correct approach.

Usually I use a class to serve as a main holder. That class holdes other Class where there are custom user controllers (lists, buttons, etc)  Sometimes one controller needs to call functions from another controller (inside the main holder). When this happens I usually use:

Class MainHolder

var classA:ClassA = new ClassA();
var classB:ClassB = new ClassB();

function MyFunction():void

{

     classB.MyFunction();

}

Class A

MainHolder(parent).MyFunction(); //dostuff

Class B

function MyFunction():void

{

     //dostuff
};

Or instead I pass the functions from MainHolder to the class A ou B

Class MainHolder

var classA:ClassA = new ClassA(MyFunction);
var classB:ClassB = new ClassB();

function MyFunction():void

{

     classB.MyFunction();

}

Class A

MyFunction(); //dostuff

Class B

function MyFunction():void

{

     //dostuff
};

But I think this isn't very good because If I have a deep display list and inside Class A I have another controller witch will trigger the function, I have to create a chain passing the function to that controller too.

I'm kind lost. Can someone point me the a good way to accomplish this?

sinious
Legend
March 18, 2013

Let's see if you're close to using a pattern. Give me some examples of what your functions actually do in Class A/B/C. I want to see how you're encapsulating functionality to determine the best way to recommend approaching it.