Model and code examples of GoF Design Patterns for Golang.
This project is available for the following objectives:
- To understand GoF Design Pattern examples in Golang.
- To understand the mapping between UML model and Golang code.
- To try Model-Driven Development (MDD) using Astah and M PLUS plug-in.
UML model example:
Golang code example:
// ˅
package bridge
import (
"fmt"
"strconv"
)
// ˄
type File struct {
// ˅
// ˄
name string
size int
// ˅
// ˄
}
func NewFile(name string, size int) *File {
// ˅
return &File{name: name, size: size}
// ˄
}
func (f *File) GetName() string {
// ˅
return f.name
// ˄
}
func (f *File) GetSize() int {
// ˅
return f.size
// ˄
}
// Print this element with the "upperPath".
func (f *File) Print(upperPath string) {
// ˅
fmt.Println(upperPath + "/" + f.String())
// ˄
}
func (f *File) String() string {
// ˅
return f.GetName() + " (" + strconv.Itoa(f.GetSize()) + ")"
// ˄
}
// ˅
// ˄
This project uses the Walk library, which works only on Windows.
UML Modeling Tool
- Download the modeling tool Astah UML or Professional, and install.
- Download M PLUS plug-in ver.2.7.6 or higher, and add it to Astah.
How to add plugins to Astah
Golang Development Environment
- Install Golang ver.1.22.0 or higher.
- Run
go install github.com/go-delve/delve/cmd/dlv@latest
to install delve. - Run
go install github.com/lxn/walk@latest
to install Walk library. - Install VS Code, add Golang extension.
- Launch VS Code. Click View -> Command Pallete (Ctrl+Shift+P) and run
> Go: Install/Update Tools
. Check all dependencies and click OK.
This project uses the Walk library, which works only on Windows.
Code Generation from UML
- Open the Astah file (model/DesignPatternExamplesInGolang.asta).
- Select model elements on the model browser of Astah.
- Click the Generate Code button.
The generated code has User Code Area. The User Code Area is the area enclosed by "˅" and "˄". Handwritten code written in the User Code Area remains after a re-generation. View code example.
For detailed usage of the tools, please see Astah Manual and M PLUS plug-in Tips.
Run (as a test run)
- Open the workspace file (design-pattern-examples-in-golang.code-workspace) in VS Code.
- Open
main_test.go
for the pattern you want to run, and click Run > Start Debugging (or press F5).
- Gamma, E. et al. Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994
- Hiroshi Yuki. Learning Design Patterns in Java [In Japanese Language], Softbank publishing, 2004
- Schmager, F. Evaluating the GO Programming Language with Design Patterns, 2010
This project is licensed under the Creative Commons Zero (CC0) license. The model and code are completely free to use.
C++, C#, Crystal, Java, JavaScript, Kotlin, Python, Ruby, Scala, Swift, TypeScript