OOP issue: class extension, function overriding, and jQuery type syntax

I have a problem with OOP with Flash, actionscript 3. This is a personal project, and I'm looking for a design template or workaround for this problem, and my goal is to learn new things.

I created the Chain class. I created this util class to simplify the deferred function procedure. You can create a chain of functions by adding them with a delay in milliseconds. This chain can be executed several times, even in reverse order. This class has functions that return themselves. This allows you to use jQuery-style syntax, for example:

var chain:Chain = new Chain(); 
chain.wait(100).add(myFunction1,300).wait(300).add(myFunction2,100);
// etc..

As an example, I left a lot of functions to demonstrate the problem. The Chain class is basically pure for adding functions and starting / stopping the chain.

public class Chain 
{  
 function wait(delay:int = 0):Chain
 {
   // do stuff
   return this;
 }

 public function add(func:Function, delay:Number = 0):Chain
 {
      list.push( new ChainItem(func, delay) );
      return this;
 }
}

ChainTween. , , ChainTween . tweenengine, Chain. . Chain Chain, Chain.

public class ChainTween extends Chain
{  
 function animate(properties:Object = null, duration:Number = 0, easing:Function = null):ChainTween
 {
   // do stuff
   return this;
 }
}

: , wait() Chain, Chain .

var chain:ChainTween = new ChainTween();
chain.wait(100).animate({x:200}, 100).wait(250);

wait() add() ChainTween, .

chain.wait(100) ChainTween, , . - ChainTween Chain ( ), , . , , , .

Chain ChainTween, , public , .

, ?

+3
6

. Fluent Interface, Google "Fluent Interface Inheritance", .

#, Java ++ . , AS3, , .

+2

, , . . Chain - :

public function animate(args:Object, time:int):Chain {
    throw new Error("Animate is supported only on ChainTween");
}

ChainTween. , .

+1

Chain, ( - ) add animate... Chain, , , , ... .

var chain:ChainTween = new ChainTween();
var params:Object = {x:200};
chain.wait(100).add(animate, 300 , params).wait(300);

alxx , , - - , Javascript, AS3 - , . , rotate, fadeOut, . ChainTween, , Object *...

, , rotate, fadeOut animate add() ( , ) Chain.

+1

* , .

0

, IChain, (add, wait ..)

0

. Wait - "IChainTween", ChainTween, "then", .

package
{
    public interface IChainTween
    {
        function doSomething():IChainTween;
        ...
        function then():IChain;
    }
}

package
{
    public class ChainTween implements IChainTween
    {
        private originalChain:IChain;
        public function ChainTween(IChain chain)
        {
            originalChain = chain;
        }
        ...
        public function doSomething():IChainTween
        {
            return this;
        }
        public function then():IChain
        {
            return originalChain;
        }
    }
}
0

Source: https://habr.com/ru/post/1780841/


All Articles