课程: Go Design Patterns

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Builder pattern example

Builder pattern example

- [Instructor] For this example, we're going to use the builder pattern to create a notification object, which represents the kind of notification pop-up message you might see on your phone or desktop computer. The notification 'Go file' contains the definition of the notification struct that the builder will create. Note that all the fields are lowercase, which means that they are not exported from this struct which makes the notification type effectively immutable, unless you provide methods to change the fields, which I haven't. So this is one of the advantages of using a builder. You can make the finished object immutable pretty easily. All right, so now let's take a look at the builder code. Here in the builder.Gofile I have the notification builder struct along with several methods that are used to modify the builders properties and a function named new notification builder. And if I scroll down a bit, we can see…

内容