You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: structural/decorator/README.md
+14-6
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,28 @@
1
1
# Decorator
2
2
3
-
The decorator pattern provide a lot of benefits when working with legacy code. In this example I have a legacy code. For example a legacy recipe. A decorator add functionality. The functionality in this example is the method Decorate. In the real world Decorate() is represented by a method that must be decorate.
3
+
The decorator pattern provide a lot of benefits when working with legacy code. In this example legacy code is represented by a legacy recipe. A decorator add functionality and in this example decorators are new ingredients.
4
4
5
5
```go
6
6
typeLegacyRecipestruct {
7
-
Decorate() (string, error)
8
7
}
8
+
```
9
+
Starting from legacy code, we must have a default behavior. For example LegacyRecipe could have a method called `Decorate` that provide the recipe.
return"Legacy recipe with the following ingredients:", nil
13
+
return"Original behavior: ", nil
12
14
}
13
15
```
14
16
15
-
All object needed to implement the decorator pattern should implement same interface.
17
+
All object needed to implement the decorator pattern should implement same interface. Because of we must improve the functionality of `Decorate` method we'll create an interface like this.
16
18
17
19
```go
18
20
typeDecorableinterface {
19
21
Decorate() (string, error)
20
22
}
21
23
```
22
24
23
-
Because of we are treating with a legacy recipe. All new ingredient must contains a `Decorable`recipe.
25
+
And because of we are treating with a legacy recipe, all new ingredient must implement a `Decorable`interface.
0 commit comments