Model and code examples of GoF Design Patterns for Crystal.
This project is available for the following objectives:
- To understand GoF Design Pattern examples in Crystal.
- To understand the mapping between UML model and Crystal code.
- To try model-driven development (MDD) using Astah and m plus plug-in.
UML model example:
Crystal code example:
# ˅
require "./file_system_element"
# ˄
# Since class name of "File" can not be used duplicately, the class name is "File_".
class File_ < FileSystemElement
# ˅
# ˄
@name : String
@size : Int32
def initialize(name : String, size : Int32)
# ˅
@name = name
@size = size
# ˄
end
def name : String
# ˅
return @name
# ˄
end
def size : Int32
# ˅
return @size
# ˄
end
# Print this element with the "upperPath".
def print(upper_path : String)
# ˅
puts "#{upper_path}/#{self.to_s}"
# ˄
end
# ˅
# ˄
end
# ˅
# ˄
This has been confirmed on Ubuntu 22.04 / 20.04.2 / 18.04.5, not confirmed on macOS and Windows.
UML Modeling Tool
- Download the modeling tool Astah UML/Professional ver.10.0.0 or higher, and install.
- Download m plus plug-in ver.3.0.0-preview or higher, and add it to Astah.
How to add plugins to Astah
Crystal Development Environment
- Install Crystal ver.1.0.0 or higher.
- Download and install VS Code.
- Install Crystal Language and Native Debug extensions for VS Code.
- Run this command to install the GTK and GDK development packages.
Ubuntu 20.04 / 18.04Ubuntu 22.04sudo apt-get install libgirepository1.0-dev libgtk-3-dev libcairo-gobject2 gir1.2-freedesktop
sudo apt-get install libgirepository1.0-dev libgtk-3-dev libcairo-gobject2 gir1.2-freedesktop sudo apt-get install gcc pkg-config git tzdata libpcre3-dev libevent-dev libyaml-dev libgmp-dev libssl-dev libxml2-dev
- Run this command in the project root directory to install crystal-gobject and cairo-gobject shards.
Note: The following error (File::AlreadyExistsError) occurs when running the shards command, but the installation seems to succeed.
shards install
Unhandled exception: Error creating symlink: '..' -> '/path_to_app/lib/cairo-gobject/lib': File exists (File::AlreadyExistsError)
- If you use Ubuntu18.04 based distributives, uncomment Rectangle structure in lib/cairo-gobject/src/cairo/lib_cairo.cr file (lines 7...12). Source: cairo-gobject
This has been confirmed on Ubuntu 22.04 / 20.04.2 / 18.04.5, not confirmed on macOS and Windows.
Code Generation from UML
- Open the Astah file (model/DesignPatternExamplesInCrystal.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.
Build and Run (in Debug Mode)
- Open the workspace file (design-pattern-examples-in-crystal.code-workspace) in VS Code.
- Open
main.cr
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
This project is licensed under the Creative Commons Zero (CC0) license. The model and code are completely free to use.
C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Scala, Swift, TypeScript