[Coding mode] method chain

Source: Internet
Author: User

Background

A method chain can put a function call on an object inside a statement. The core of the method chain pattern is the invocation of the previous object function, which returns the object of the next function call.

The builder pattern can be written as a method chain. The difference between a method chain and builder is that the builder has an intermediate object builder to stage the properties that need to be built.

There may be multithreading problems with the method chain.

Scene

Need ligatures.

Instance

final class rmbcurrency {    private int yuan = 0;     private int jiao = 0;    private int  fen = 0;    public rmbcurrency setyuan (Int yuan)  {         this.yuan = yuan;         return this;    }    public rmbcurrency  setjiao (Int jiao)  {        this.jiao = jiao;         return this;    }     public rmbcurrency setfen (Int fen)  {         this.fen = fen;        return this;     }     @OverRide    public string tostring () {         return yuan +  "."  + jiao +  "."  + fen;    }}
public class test{public static void Main (string[] args) {rmbcurrency currency = new Rmbcurrency (). Setyuan (1). s        Etjiao (1). Setfen (1);    System.out.print (currency); }}


[Coding mode] method chain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.