mercredi 28 décembre 2016

Builder Pattern with only one class

I'm learning the Builder Pattern. I have a question, Why don't using a class (ex: MyCoffe)instead of 2 class (ex: MyCoffe, BuilderClass). Thanks you for reading.

package Creational.Builder;

public class MyCoffe {

        private String name;
        private int id;

        public MyCoffe(){

        }

        public MyCoffe setName(String newName){
            this.name  = newName;
            return this;
        }

        public MyCoffe setId(int newId){
            this.id = newId;
            return this;
        }

        public MyCoffe build(){
            return this;
        }

        public String toString(){
            return name + "/" + id;
        }

        public static void main(String[] args){
            MyCoffe myCoffe = new MyCoffe().setName("HelloWorld").setId(9999);  
            System.out.println(myCoffe);

            System.out.println("Thanks for help!");
        }
}

Aucun commentaire:

Enregistrer un commentaire