2

I built a class (such as a class to make simple animations):

public class myAnimations {

    private Animation animation;
    private ImageView imageView;

    public myAnimations(ImageView img) {
        super();
        this.imageView = img;
    }

    public void rotate(int duration, int repeat) {
        animation = new RotateAnimation(0.0f, 360.0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setRepeatCount(repeat);
        animation.setDuration(duration);
    }

    public void play() {
        imageView.startAnimation(animation);
    }

    public void stop() {
        animation.setRepeatCount(0);
    }
}

and I just can use it in this way:

ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = new myAnimations(myImage);
animation.rotate(1000, 10);
animation.play(); //from this way…

but if I wanted to be able to use it like this:

ImageView myImage = (ImageView) findViewById(R.id.my_image);
myAnimations animation = new myAnimations(myImage);
animation.rotate(1000, 10).play(); //…to this way

so I can call this double method (I don't know the name), how should I build my class?

PS please feel free to edit the title if you know the name of my what I need.

shaithana
  • 2,470
  • 1
  • 24
  • 37
  • 5
    possible duplicate of [how to achieve method chaining in java?](http://stackoverflow.com/questions/21180269/how-to-achieve-method-chaining-in-java) – Richard Schwartz Aug 23 '15 at 14:23
  • Please note that class names in Java start with upper case. It's a convention which is followed strictly and it helps keep your code clean from ambiguities. – Sufian Aug 23 '15 at 15:28

2 Answers2

7

You're asking about allowing method chaining and to do this, some of your methods should not return void, but rather the they should return this. For example:

// note that it is declared to return myAnimation type
public MyAnimations rotate(int duration, int repeat) {
    animation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setRepeatCount(repeat);
    animation.setDuration(duration);
    return this;
}

Thus when calling this method, you can chain another method call onto it since it returns the current object:

animation.rotate(1000, 10).play();

You will need to do this for every method that you want to allow chaining.

Note that as per Marco13, this is also referred to as a Fluent Interface.

As an aside, you will want to learn and use Java naming conventions. Variable names should all begin with a lower letter while class names with an upper case letter. Learning this and following this will allow us to better understand your code, and would allow you to better understand the code of others. So re-name your myAnimations class to MyAnimations.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    Also referred to as [Fluent Interface](https://en.wikipedia.org/wiki/Fluent_interface) – Marco13 Aug 23 '15 at 14:22
  • Shouldn't it be `MyAnimations` instead of `myAnimations`? – Tom Aug 23 '15 at 14:29
  • 1
    @Tom: please read the last paragraph of my answer. Edit: Ah, you did not read the original question. Someone has changed the original poster's code as it started out at `public class myAnimations`. – Hovercraft Full Of Eels Aug 23 '15 at 14:29
  • @HovercraftFullOfEels OPs class name is `MyAnimations`, but he justed edited that. Didn't noticed that. – Tom Aug 23 '15 at 14:31
  • @HovercraftFullOfEels thanks for the explanations, but if I wan to able to use the same method in both way (chained and not-chained) is it possible? I need to write two different methods with two different names? – shaithana Aug 23 '15 at 14:33
  • 1
    @GiovanniDiGregorio If you don't want to chain them, then just don't do it. – Tom Aug 23 '15 at 14:34
  • @HovercraftFullOfEels I re-edit the question with the class name error to make useful your good answer, thanks! – shaithana Aug 23 '15 at 14:39
0

It is called Builder Design Pattern, which is used to avoid handling too many constructors.

To achieve it, first your method return type should be your class name and you have to return this for all the methods which you want in chain.

So, in your case, rotate method return type will be myAnimations.

public myAnimations rotate(int duration, int repeat) {
    animation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setRepeatCount(repeat);
    animation.setDuration(duration);
    return this;
}

Now, you can call as per your expectation,

animation.rotate(1000, 10).play();

Also, I strongly recommend to use proper naming convention for class name. It should be ideally MyAnimations.

Darshan Patel
  • 2,839
  • 2
  • 25
  • 38