[JAVA] A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]

Is this the correct way to use the fluid interface?

I had a person with long Java experience help me with a C # project at one site. Some of the code used a fluid interface, but I wasn't quite convinced. I tried to create a sample code by referring to the code at that time.

Specifications) Update the DB by merging the screen values with the results obtained from the DB.

  1. There are Header1, Header2, Header3 in DB, and all of them are to be updated.
  2. The value can be edited on the screen, and the merge timing is when the registration button is clicked.
        /**Main processing**/
        private Dao _dao = new Dao();

        public void Main()
        {
            //Get the current information from the DB.
            var param = new UpdateParameter(_dao)
                            .SetHeader(1)
                            .SetHeader2("a")
                            .SetHeader3(2);

            //Reflect the information on the screen.
            SetValueFromView(param);

            //Update the DB.
            _dao.Update(param);
        }

        private void SetValueFromView(UpdateParameter param)
        {
            //Header1 of the parameter that got the value of the screen,2,Reflect in 3.
            //Code omitted
        }

        /**Parameter class that summarizes the DTO of the DB update table**/
        public class UpdateParameter
        {
            public Header Header { get; private set; }
            public Header2 Header2 { get; private set; }
            public Header3 Header3 { get; private set; }

            private readonly Dao _dao = null;

            public UpdateParameter(Dao dao)
            {
                _dao = dao;
            }

            public UpdateParameter SetHeader(int id)
            {
                Header = _dao.GetHeader(id);
                return this;
            }

            public UpdateParameter SetHeader2(string code)
            {
                Header2 = _dao.GetHeader2(code);
                return this;
            }

            public UpdateParameter SetHeader3(int id)
            {
                Header3 = _dao.GetHeader3(id);
                return this;
            }
        }

        /** Header1,2,3 DTO**/
        public class Header
        {
            public int Id { get; set; }
        }

        public class Header2
        {
            public string Code { get; set; }
        }

        public class Header3
        {
            public int Id { get; set; }
        }

        /**Dao for data acquisition and update**/
        public class Dao
        {
            public Header GetHeader(int id)
            {
                //Get header
                return new Header() { Id = id };
            }

            public Header2 GetHeader2(string code)
            {
                //Get header 2
                return new Header2() { Code = code };
            }

            public Header3 GetHeader3(int id)
            {
                //Get header 3
                return new Header3() { Id = id };
            }

            public void Update(UpdateParameter param)
            {
                //Execute DB update process. Code omitted
            }
        }
java
    private Dao dao = new Dao();

    public void main() {
        //Get the current information from the DB.
        UpdateParameter param = new UpdateParameter(dao)
                .setHeader(1)
                .setHeader2("a")
                .setHeader3(2);

        //Reflect the information on the screen.
        setValueFromView(param);

        //Update the DB.
        dao.update(param);
    }

    private void setValueFromView(UpdateParameter param) {
        //Header1 of the parameter that got the value of the screen,2,Reflect in 3.
        //Code omitted
    }

    public class UpdateParameter {
        private Header header = null;
        private Header2 header2 = null;
        private Header3 header3 = null;

        private final Dao dao;

        public UpdateParameter(Dao dao) {
            this.dao = dao;
        }

        public UpdateParameter setHeader(int id) {
            this.header = dao.getHeader(id);
            return this;
        }

        public UpdateParameter setHeader2(String code) {
            this.header2 = dao.getHeader2(code);
            return this;
        }

        public UpdateParameter setHeader3(int id) {
            this.header3 = dao.getHeader3(id);
            return this;
        }

        public Header getHeader() {
            return header;
        }

        public Header2 getHeader2() {
            return header2;
        }

        public Header3 getHeader3() {
            return header3;
        }

    }

    /** Header1,2,3 DTO**/
    public class Header {
        private int id;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }

    public class Header2 {
        private String Code;

        public String getCode() {
            return Code;
        }

        public void setCode(String code) {
            Code = code;
        }

    }

    public class Header3 {
        private int Id;

        public int getId() {
            return Id;
        }

        public void setId(int id) {
            Id = id;
        }
    }

    /**Dao for data acquisition and update**/
    public class Dao {
        public Header getHeader(int id) {
            //Get header
            Header header = new Header();
            header.setId(id);
            return header;
        }

        public Header2 getHeader2(String code) {
            //Get header 2
            Header2 header2 = new Header2();
            header2.setCode(code);
            return header2;
        }

        public Header3 getHeader3(int id) {
            //Get header 3
            Header3 header3 = new Header3();
            header3.setId(id);
            return header3;
        }

        public void update(UpdateParameter param) {
            //Execute DB update process. Code omitted
        }
    }

What I was interested in

It seems that the interface is such that UpdateParameter flows, but this code has the following problems.

  1. UpdateParameter refers to Dao.
  2. There are multiple places to fix when the number of tables increases.
  3. SetHeader1 to SetHeader3 must be called.

You can see the advantage of a flowing interface if it is a class like StringBuilder. On the other hand, it feels strange to use it in a class that only has simple information such as UpdateParameter.

Try to fix it to a safe code.

       /**Main processing**/
        private Dao _dao = new Dao();

        public void Main()
        {
            //Get the current information from the DB.
            var param = new UpdateParameter();
            //Get data from DB in main process.
            param.Header = _dao.GetHeader(1);
            param.Header2 = _dao.GetHeader2("a");
            param.Header3 = _dao.GetHeader3(2);

            //Reflect the information on the screen.
            SetValueFromView(param);

            //Update the DB.
            _dao.Update(param);
        }

        private void SetValueFromView(UpdateParameter param)
        {
            //Header1 of the parameter that got the value of the screen,2,Reflect in 3.
            //Code omitted
        }

        /**Parameter class that summarizes the DTO of the DB update table**/
        public class UpdateParameter
        {
            public Header Header { get; set; }
            public Header2 Header2 { get; set; }
            public Header3 Header3 { get; set; }

            //Remove Dao reference
        }

        /** Header1,2,3 DTO**/
        public class Header
        {
            public int Id { get; set; }
        }

        public class Header2
        {
            public string Code { get; set; }
        }

        public class Header3
        {
            public int Id { get; set; }
        }

        /**Dao for data acquisition and update**/
        public class Dao
        {
            public Header GetHeader(int id)
            {
                //Get header
                return new Header() { Id = id };
            }

            public Header2 GetHeader2(string code)
            {
                //Get header 2
                return new Header2() { Code = code };
            }

            public Header3 GetHeader3(int id)
            {
                //Get header 3
                return new Header3() { Id = id };
            }

            public void Update(UpdateParameter param)
            {
                //Execute DB update process. Code omitted
            }
        }

java
    private Dao dao = new Dao();

    public void main() {
        //Get the current information from the DB.
        UpdateParameter param = new UpdateParameter();
        //Get data from DB in main process.
        param.setHeader(dao.getHeader(1));
        param.setHeader2(dao.getHeader2("a"));
        param.setHeader3(dao.getHeader3(2));

        //Reflect the information on the screen.
        setValueFromView(param);

        //Update the DB.
        dao.update(param);
    }

    private void setValueFromView(UpdateParameter param) {
        //Header1 of the parameter that got the value of the screen,2,Reflect in 3.
        //Code omitted
    }

    public class UpdateParameter {
        private Header header = null;
        private Header2 header2 = null;
        private Header3 header3 = null;

        //Remove Dao reference

        public void setHeader(Header header) {
            this.header = header;
        }

        public void setHeader2(Header2 header2) {
            this.header2 = header2;
        }

        public void setHeader3(Header3 header3) {
            this.header3 = header3;
        }

        public Header getHeader() {
            return header;
        }

        public Header2 getHeader2() {
            return header2;
        }

        public Header3 getHeader3() {
            return header3;
        }

    }

    //The following code is omitted

I just deleted the Dao reference of UpdateParameter and got the DB on the main processing side, but this code is convincing.

Summary

I have about a year of Java development experience, so I'm wondering if the code shown in the sample is normal for Java. Also, what is the reason for using a fluid interface even if the problems described in "What I was interested in" are left as they are.

[Previous article (code collection that is a little worrisome)] (http://qiita.com/csharpisthebest/items/7d9d2c7e4da4b89488fd)

[Next article (redundant assignment statement)] (http://qiita.com/csharpisthebest/items/f9ebc741e40037553d5b)

Table of Contents

Recommended Posts

A flowing interface? -I want to give you an opportunity to write good code. 3 [C # refactoring sample]
Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
A little worrisome code collection-I want to give you a chance to write good code. 2 [C # refactoring sample]
Complicated conditional branching-I want to give you a chance to write good code. 1 [C # refactoring sample]
Bool type handling-I want to give you a chance to write good code. 7 [C # refactoring sample]
Too many function arguments-I want to give you a chance to write good code. 8 [C # refactoring sample]
Wasteful processing of collections-I want to give you a chance to write good code. 5 [C # refactoring sample]
Is it possible to separate function calls and conditionals? --I want to give you a chance to write good code. 9 [C # refactoring sample]
I want to write a nice build.gradle
I want to write a unit test!
I want to simply write a repeating string
7 things I want you to keep so that it doesn't become a fucking code
I want to write a loop that references an index with Java 8's Stream API
I want to give a class name to the select attribute
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to summarize Apache Wicket 8 because it is a good idea
I want to ForEach an array with a Lambda expression in Java
I want to develop a web application!
I want to make an ios.android app
Rails6 I want to make an array of values with a check box
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
I want to recursively get the superclass and interface of a certain class