건설자 모델과 공장 모델의 차이
공장 모델은 전체 대상의 창설 방법을 중시하고 건설자 모델은 대상의 창설 과정을 중시하며 창설 대상의 과정 방법은 창설할 때 자유롭게 호출할 수 있다.
건설자 모델의 예를 보면 알 수 있다.
1 public class EmployeeBuilder
2 {
3 private int id = 1;
4 private string firstname = "first";
5 private string lastname = "last";
6 private DateTime birthdate = DateTime.Today;
7 private string street = "street";
8
9 public Employee Build()
10 {
11 return new Employee(id, firstname, lastname, birthdate, street);
12 }
13
14 public EmployeeBuilder WithFirstName(string firstname)
15 {
16 this.firstname = firstname;
17 return this;
18 }
19
20 public EmployeeBuilder WithLastName(string lastname)
21 {
22 this.lastname = lastname;
23 return this;
24 }
25
26 public EmployeeBuilder WithBirthDate(DateTime birthdate)
27 {
28 this.birthdate = birthdate;
29 return this;
30 }
31
32 public EmployeeBuilder WithStreet(string street)
33 {
34 this.street = street;
35 return this;
36 }
37
38 public static implicit operator Employee(EmployeeBuilder instance)
39 {
40 return instance.Build();
41 }
42 }
호출:void main(){
Employee emp1 = new EmployeeBuilder().WithFirstName("Kenneth")
.WithLastName("Truyers");
Employee emp2 = new EmployeeBuilder().WithBirthDate(new DateTime(1983, 1,1));
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.