Skip to content

Latest commit

 

History

History
120 lines (90 loc) · 4.08 KB

README.md

File metadata and controls

120 lines (90 loc) · 4.08 KB

Design Pattern Examples in Ruby

Model and code examples of GoF Design Patterns for Ruby.
This project is available for the following objectives:

  • To understand GoF Design Pattern examples in Ruby.
  • To understand the mapping between UML model and Ruby code.
  • To try model-driven development (MDD) using Astah and m plus plug-in.

UML model example:

Ruby code example:

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# ˅
require_relative 'file_system_element'

# ˄

# Since class name of "File" can not be used duplicately, the class name is "File_".
class File_ < FileSystemElement
  # ˅

  # ˄

  public
  def initialize(name, size)

    @name = name

    @size = size

    # ˅

    # ˄
  end

  public
  def name
    # ˅
    @name
    # ˄
  end

  public
  def size
    # ˅
    @size
    # ˄
  end

  # Print this element with the "upperPath".
  public
  def print(upper_path)
    # ˅
    puts "#{upper_path}/#{self}"
    # ˄
  end

  # ˅

  # ˄
end

# ˅

# ˄

Installation

UML Modeling Tool

Ruby Development Environment

  • Install Ruby.
  • Install gems using the following commands.
    cd <this directory (design-pattern-examples-in-ruby)>
    bundle install
  • Optionally, install an IDE (e.g. RubyMine).

Usage

Code Generation from UML

  1. Open the Astah file (model/DesignPatternExamplesInRuby.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.

Run (if you use RubyMine)

  1. Open the project root directory (design-pattern-examples-in-ruby) in RubyMine.
  2. Open main.rb for the pattern you want to run, and click Run icon in the left gutter.

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

License

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#, Crystal, Go, Java, JavaScript, Kotlin, Python, Scala, Swift, TypeScript