Builder Pattern

  • View
    64

  • Download
    1

Embed Size (px)

Transcript

  • Mental Log Week 11

    Builder Pattern

    Dinh Hoang Long

    Mental Log

    Dinh Hoang Long, Mental Log Builder Pattern 1/28

  • Outline

    Outline

    Bi ton, vn

    Builder Pattern

    tng c bn

    p dng vo bi tonPhn tch cc thnh phn trong Builder Pattern

    Builder Pattern trong thc t

    Java: StringBuilerPHP: Zend_Db_Select

    Dinh Hoang Long, Mental Log Builder Pattern 2/28

  • Bi ton, vn

    Bi ton, vn

    1 i tng phc tp c to ra t nhiu thnh phn nh

    Trong cc thnh phn to nn i tng, c thnh phn bt buc vthnh phn khng bt buc

    Dinh Hoang Long, Mental Log Builder Pattern 3/28

  • Bi ton, vn

    Bi ton, vn

    1 i tng phc tp c to ra t nhiu thnh phn nh

    Trong cc thnh phn to nn i tng, c thnh phn bt buc vthnh phn khng bt buc

    Dinh Hoang Long, Mental Log Builder Pattern 3/28

  • Bi ton, vn

    User Class

    public class User{

    private String first_name; // required

    private String last_name; // required

    private int age; // optional

    private String phone; // optional

    private String address; // optional

    ...

    }

    Dinh Hoang Long, Mental Log Builder Pattern 4/28

  • Bi ton, vn

    User Class

    Constructor:public User(String first_name_in , String last_name_in , int age_in , String phone_in ,

    String address_in){

    first_name = first_name_in;

    last_name = last_name_in;

    age = age_in;

    phone = phone_in;

    address = address_in;

    }

    Dinh Hoang Long, Mental Log Builder Pattern 5/28

  • Bi ton, vn

    User Class

    Constructor:public User(String first_name_in , String last_name_in , int age_in , String phone_in ,

    String address_in){

    first_name = first_name_in;

    last_name = last_name_in;

    age = age_in;

    phone = phone_in;

    address = address_in;

    }

    public User(String first_name_in , String last_name_in , int age_in , String phone_in){

    this(first_name_in , last_name_in , age_in , phone_in , "");

    }

    Dinh Hoang Long, Mental Log Builder Pattern 6/28

  • Bi ton, vn

    User Class

    Constructor:public User(String first_name_in , String last_name_in , int age_in , String phone_in ,

    String address_in){

    first_name = first_name_in;

    last_name = last_name_in;

    age = age_in;

    phone = phone_in;

    address = address_in;

    }

    public User(String first_name_in , String last_name_in , int age_in , String phone_in){

    this(first_name_in , last_name_in , age_in , phone_in , "");

    }

    public User(String first_name_in , String last_name_in , int age_in){

    this(first_name_in , last_name_in , age_in , "");

    }

    Dinh Hoang Long, Mental Log Builder Pattern 7/28

  • Bi ton, vn

    User Class

    Constructor:public User(String first_name_in , String last_name_in , int age_in , String phone_in ,

    String address_in){

    first_name = first_name_in;

    last_name = last_name_in;

    age = age_in;

    phone = phone_in;

    address = address_in;

    }

    public User(String first_name_in , String last_name_in , int age_in , String phone_in){

    this(first_name_in , last_name_in , age_in , phone_in , "");

    }

    public User(String first_name_in , String last_name_in , int age_in){

    this(first_name_in , last_name_in , age_in , "");

    }

    public User(String first_name_in , String last_name_in){

    this(first_name_in , last_name_in , 0);

    }

    Dinh Hoang Long, Mental Log Builder Pattern 8/28

  • Bi ton, vn

    User Class

    Constructor: PHPpublic function User($first_name_in , $last_name_in , $age_in = 0, $phone_in = "",

    $address_in = ""){

    $first_name = $first_name_in;

    $last_name = $last_name_in;

    $age = $age_in;

    $phone = $phone_in;

    $address = $address_in;

    }

    Dinh Hoang Long, Mental Log Builder Pattern 9/28

  • Bi ton, vn

    User Class

    Nhc im:

    Khi s lng tham s nhiu ln th s lng Constructor cn vitnhiu ln

    Kh nm bt c th t cc tham s trong Constructor

    Cn nm r default ca cc tham s khng mun set.

    User hlong = new User("Dinh", "Hoang Long", 22, "", "Hanoi");

    Dinh Hoang Long, Mental Log Builder Pattern 10/28

  • Bi ton, vn

    User Class

    Nhc im:

    Khi s lng tham s nhiu ln th s lng Constructor cn vitnhiu ln

    Kh nm bt c th t cc tham s trong Constructor

    Cn nm r default ca cc tham s khng mun set.

    User hlong = new User("Dinh", "Hoang Long", 22, "", "Hanoi");

    Dinh Hoang Long, Mental Log Builder Pattern 10/28

  • Bi ton, vn

    User Class

    Nhc im:

    Khi s lng tham s nhiu ln th s lng Constructor cn vitnhiu ln

    Kh nm bt c th t cc tham s trong Constructor

    Cn nm r default ca cc tham s khng mun set.

    User hlong = new User("Dinh", "Hoang Long", 22, "", "Hanoi");

    Dinh Hoang Long, Mental Log Builder Pattern 10/28

  • Bi ton, vn

    User Class

    Gii php: JavaBeans Convention

    To 1 constructor khng c tham s no (0-argument constructor),hoc 1 constructor duy nht vi s lng tham s ti thiu.

    S dng cc phng thc SetX (input_value) ch nh cc gi trmun set.

    class User{

    ...

    public void setAge(age_in){

    age = age_in;

    }

    }

    User hlong = new User("Dinh", "Hoang Long");

    hlong.setAge (22);

    Nhc im?

    Dinh Hoang Long, Mental Log Builder Pattern 11/28

  • Bi ton, vn

    User Class

    Gii php: JavaBeans Convention

    To 1 constructor khng c tham s no (0-argument constructor),hoc 1 constructor duy nht vi s lng tham s ti thiu.

    S dng cc phng thc SetX (input_value) ch nh cc gi trmun set.

    class User{

    ...

    public void setAge(age_in){

    age = age_in;

    }

    }

    User hlong = new User("Dinh", "Hoang Long");

    hlong.setAge (22);

    Nhc im?

    Dinh Hoang Long, Mental Log Builder Pattern 11/28

  • Bi ton, vn

    User Class

    Gii php: JavaBeans Convention

    To 1 constructor khng c tham s no (0-argument constructor),hoc 1 constructor duy nht vi s lng tham s ti thiu.

    S dng cc phng thc SetX (input_value) ch nh cc gi trmun set.

    class User{

    ...

    public void setAge(age_in){

    age = age_in;

    }

    }

    User hlong = new User("Dinh", "Hoang Long");

    hlong.setAge (22);

    Nhc im?

    Dinh Hoang Long, Mental Log Builder Pattern 11/28

  • Bi ton, vn

    User Class

    Nhc im

    Object c to ra tr nn khng bn vng, d b thay i.

    Ngi s dng kh kim sot c trng thi ca object(V qu d b thay i).

    Gii php: Builder Pattern

    Dinh Hoang Long, Mental Log Builder Pattern 12/28

  • Bi ton, vn

    User Class

    Nhc im

    Object c to ra tr nn khng bn vng, d b thay i.

    Ngi s dng kh kim sot c trng thi ca object(V qu d b thay i).

    Gii php: Builder Pattern

    Dinh Hoang Long, Mental Log Builder Pattern 12/28

  • Bi ton, vn

    User Class

    Nhc im

    Object c to ra tr nn khng bn vng, d b thay i.

    Ngi s dng kh kim sot c trng thi ca object(V qu d b thay i).

    Gii php: Builder Pattern

    Dinh Hoang Long, Mental Log Builder Pattern 12/28

  • Builder Pattern tng c bn

    tng c bn

    Builder Pattern

    L 1 pattern dnh cho vic to object (creational pattern)

    Builder pattern c s dng khi s lng tham s cn thit to 1object l ln (>4), hoc qu trnh to object phc tp.

    Builder pattern s dng 1 object c vai tr Builder, to 1 objectkhc.

    Dinh Hoang Long, Mental Log Builder Pattern 13/28

  • Builder Pattern tng c bn

    tng c bn

    Builder Pattern

    L 1 pattern dnh cho vic to object (creational pattern)

    Builder pattern c s dng khi s lng tham s cn thit to 1object l ln (>4), hoc qu trnh to object phc tp.

    Builder pattern s dng 1 object c vai tr Builder, to 1 objectkhc.

    Dinh Hoang Long, Mental Log Builder Pattern 13/28

  • Builder Pattern tng c bn

    tng c bn

    Builder Pattern

    L 1 pattern dnh cho vic to object (creational pattern)

    Builder pattern c s dng khi s lng tham s cn thit to 1object l ln (>4), hoc qu trnh to object phc tp.

    Builder pattern s dng 1 object c vai tr Builder, to 1 objectkhc.

    Dinh Hoang Long, Mental Log Builder Pattern 13/28

  • Builder Pattern p dng vo bi ton

    p dng vo bi ton

    tng

    To 1 class UserBuilder c vai tr to 1 object cho class User.

    Vic to object cho class User b rng buc phi qua UserBuilder.

    Gii php: Nested Class

    Dinh Hoang Long, Mental Log Builder Pattern 14/28

  • Builder Pattern p dng vo bi ton

    p dng vo bi ton

    tng

    To 1 class UserBuilder c vai tr to 1 object cho class User.

    Vic to object cho class User b rng buc phi qua UserBuilder.

    Gii php: Nested Class

    Dinh Hoang Long, Mental Log Builder Pattern 14/28

  • Builder Pattern p dng vo bi ton

    p dng vo bi ton

    tng

    To 1 class UserBuilder c vai tr to 1 object cho class User.

    Vic to object cho class User b rng buc phi qua UserBuilder.

    Gii php: Nested Class

    Dinh Hoang Long, Mental Log Builder Pattern 14/28

  • Builder Pattern p dng vo bi ton

    UserBuilder Class

    class User{

    public static class UserBuilder (){

    private String first_name;

    private String last_name;

    private int age = 0;

    private String phone = "";

    private String address = "";

    public UserBuilder(String first_name_in , String last_name_in){

    first_name = first_name_in;

    last_name = last_name_in;

    }

    }

    }

    Dinh Hoang Long, Mental Log Builder Pattern 15/28

  • Builder Pattern p dng vo bi ton

    UserBuilder Class

    class User{