design-pattern-examples-in-crystal

UML model and code examples of design patterns for Crystal. The model is created with Astah.

Design Pattern Examples in Crystal

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

# ˅
# ˄

Installation on Ubuntu

This has been confirmed on Ubuntu 22.04 / 20.04.2 / 18.04.5, not confirmed on macOS and Windows.

UML Modeling Tool

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.04
    sudo apt-get install libgirepository1.0-dev libgtk-3-dev libcairo-gobject2 gir1.2-freedesktop
    
    Ubuntu 22.04
    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.
    shards install
    
    Note: The following error (File::AlreadyExistsError) occurs when running the shards command, but the installation seems to succeed.
    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

Usage on Ubuntu

This has been confirmed on Ubuntu 22.04 / 20.04.2 / 18.04.5, not confirmed on macOS and Windows.

Code Generation from UML

  1. Open the Astah file (model/DesignPatternExamplesInCrystal.asta).
  2. Select model elements on the model browser of Astah.
  3. 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)

  1. Open the workspace file (design-pattern-examples-in-crystal.code-workspace) in VS Code.
  2. Open main.cr for the pattern you want to run, and click Run > Start Debugging (or press F5).

References

  • 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

Licence

This project is licensed under the Creative Commons Zero (CC0) license. The model and code are completely free to use.

CC0

Other Language Examples

C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Scala, Swift, TypeScript

Repository

design-pattern-examples-in-crystal

Owner
Statistic
  • 52
  • 2
  • 0
  • 0
  • 1
  • 7 months ago
  • April 6, 2021
License

Creative Commons Zero v1.0 Universal

Links
Synced at

Wed, 06 Nov 2024 22:16:53 GMT

Languages