From the course: Go Design Patterns

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Factory pattern example

Factory pattern example - Go Tutorial

From the course: Go Design Patterns

Factory pattern example

- [Instructor] Let's actually implement a factory pattern example in go code. So for this example, we're going to implement a factory that creates two kinds of objects that are both types of publications, newspapers, and magazines. So here in the editor, let's open up the publication.go file. This file defines the ipublication interface, which has a set of methods for setting various pieces of data on a publication type, which is defined as the publication struct right here. So there's a name, the number of pages and the name of the publisher. So if we scroll down below that we can see that the publication struct implements the ipublication interface methods for setting and getting various fields. So that's pretty straightforward. So now let's go look at the magazine and newspaper.go files. The code in these files implement the magazine and newspaper types, which basically just embed the publication type. So…

Contents