简体   繁体   中英

How to create class and chaining methods

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM