-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cn.sp.builder; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* JavaBean模式 | ||
* Created by 2YSP on 2019/10/10. | ||
*/ | ||
public class Person { | ||
|
||
private int age; | ||
|
||
private String name; | ||
|
||
private Date birthday; | ||
|
||
private String sex; | ||
|
||
private String phone; | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Date getBirthday() { | ||
return birthday; | ||
} | ||
|
||
public void setBirthday(Date birthday) { | ||
this.birthday = birthday; | ||
} | ||
|
||
public String getSex() { | ||
return sex; | ||
} | ||
|
||
public void setSex(String sex) { | ||
this.sex = sex; | ||
} | ||
|
||
public String getPhone() { | ||
return phone; | ||
} | ||
|
||
public void setPhone(String phone) { | ||
this.phone = phone; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cn.sp.builder; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* 重叠构造器模式 | ||
* Created by 2YSP on 2019/10/10. | ||
*/ | ||
public class Person2 { | ||
|
||
private int age; | ||
|
||
private String name; | ||
|
||
private Date birthday; | ||
|
||
private String sex; | ||
|
||
private String phone; | ||
|
||
|
||
public Person2(String name,Date birthday){ | ||
this.Person2(name,birthday,18); | ||
} | ||
|
||
public void Person2(String name, Date birthday, int age) { | ||
this.Person2(name,birthday,age,"man"); | ||
} | ||
|
||
public void Person2(String name, Date birthday, int age, String sex) { | ||
this.Person2(name,birthday,age,sex,"110"); | ||
} | ||
|
||
public void Person2(String name, Date birthday, int age, String sex, String phone) { | ||
this.name = name; | ||
this.birthday = birthday; | ||
this.age = age; | ||
this.sex = sex; | ||
this.phone = phone; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cn.sp.builder; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Builder模式可读性好,更加灵活 | ||
* Created by 2YSP on 2019/10/10. | ||
*/ | ||
public class Person3 { | ||
|
||
private final int age; | ||
|
||
private final String name; | ||
|
||
private final Date birthday; | ||
|
||
private final String sex; | ||
|
||
private final String phone; | ||
|
||
private Person3(Builder builder) { | ||
age = builder.age; | ||
name = builder.name; | ||
birthday = builder.birthday; | ||
sex = builder.sex; | ||
phone = builder.phone; | ||
} | ||
|
||
|
||
public static class Builder{ | ||
// required paramter | ||
private final String name; | ||
|
||
private final Date birthday; | ||
|
||
// optional value | ||
private int age; | ||
|
||
private String sex; | ||
|
||
private String phone; | ||
|
||
public Builder(String name,Date birthday){ | ||
this.name = name; | ||
this.birthday = birthday; | ||
} | ||
|
||
public Builder age(int age){ | ||
this.age = age; | ||
return this; | ||
} | ||
|
||
public Builder sex(String sex){ | ||
this.sex = sex; | ||
return this; | ||
} | ||
|
||
public Builder phone(String phone){ | ||
this.phone = phone; | ||
return this; | ||
} | ||
|
||
public Person3 build(){ | ||
return new Person3(this); | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
Person3 person3 = new Builder("张三",new Date()).age(18).sex("女").phone("110").build(); | ||
System.out.println(person3); | ||
} | ||
} |