Quellcode durchsuchen

建造者模式

master
qdjjx vor 3 Jahren
Ursprung
Commit
6372d00c60
1 geänderte Dateien mit 47 neuen und 0 gelöschten Zeilen
  1. 47
    0
      专题/后端/DDD/7、Hi,建造者模式.md

+ 47
- 0
专题/后端/DDD/7、Hi,建造者模式.md Datei anzeigen

@@ -0,0 +1,47 @@
1
+```
2
+    public class ToDoItemBuilder
3
+    {
4
+        private ToDoItem _todo = new ToDoItem();
5
+
6
+        public ToDoItemBuilder Id(int id)
7
+        {
8
+            _todo.Id = id;
9
+            return this;
10
+        }
11
+
12
+        public ToDoItemBuilder Title(string title)
13
+        {
14
+            _todo.Title = title;
15
+            return this;
16
+        }
17
+
18
+
19
+        public ToDoItemBuilder Description(string description)
20
+        {
21
+            _todo.Description = description;
22
+            return this;
23
+        }
24
+
25
+        public ToDoItemBuilder WithDefaultValues()
26
+        {
27
+            _todo = new ToDoItem { Id = 1, Title = "title", Description = "desc" };
28
+            return this;
29
+        }
30
+
31
+        public ToDoItem Build() => _todo;
32
+    }
33
+```
34
+
35
+
36
+
37
+```
38
+    public class ToDoItem : BaseEntity, IAggregateRoot
39
+    {
40
+        public string Title { get; set; } = string.Empty;
41
+        public string Description { get; set; }
42
+        public bool IsDone { get; private set; }
43
+
44
+		......
45
+    }
46
+```
47
+

Laden…
Abbrechen
Speichern