Ruby Design Pattern

  1. 1. template pattern
  2. 2. Strategy pattern
  • Observer pattern
  • ruby design pattern
    1. 1. template pattern
    2. 2. Strategy pattern
  • Observer pattern
  • template pattern

    write abstract method in base class.

    In ruby raise in abstract method to make sure it’s implemented in subclass.

    Hook is the so called empty method in base class.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    class Base
    def abstract_method
    raise "this method need to be implemented"
    end
    def hook_method
    #just do nothin here but can be implemented to so something in subclass
    end
    end




    class Sub < Base
    def abstract_method
    # implemented the method
    end


    def hook_method
    # implement the method
    end
    end

    Example of this pattern in ruby: GenerickServer in webrick

    Strategy pattern

    The GoF call this “pull the algorithm out into a separate object” technique the Strategy pattern

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # pull the algorithm of renderer outside of the class
    class Reporter
    def initialize(content, renderer)
    @content = content
    @renderer = renderer
    end
    def render
    @renderer.render(@content)
    end
    end


    class MarkdownRenderer
    def self.render(content)
    # markdown render
    end
    end


    class HTMLRenderer
    def self.render(content)
    # html render
    end
    end


    content = "##title##"
    htmlReport = Reporter.new(content, HTMLRenderer)
    markdownReport = Reporter.new(content, MarkdownRenderer)

    Thanks for Ruby’s duck typing, we don’t need to have a base abstract class for the Renderer. We only need to make sure a class method render is defined

    Even we can use a callable proc to remove the class declaration.

    Example of this pattern in ruby: the Auth middle-ware Warden

    Observer pattern

    Take ActiveRecord hooks as an Example.

    n a nice example of the Convention Over Configuration pattern (see Chapter 18), ActiveRecord does not require you to register your observer: It just figures out that EmployeeObserver is there to observe Employees, based on the class name

    ruby design pattern

    template pattern

    write abstract method in base class.

    In ruby raise in abstract method to make sure it’s implemented in subclass.

    Hook is the so called empty method in base class.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    class Base
    def abstract_method
    raise "this method need to be implemented"
    end
    def hook_method
    #just do nothin here but can be implemented to so something in subclass
    end
    end




    class Sub < Base
    def abstract_method
    # implemented the method
    end


    def hook_method
    # implement the method
    end
    end

    Example of this pattern in ruby: GenerickServer in webrick

    Strategy pattern

    The GoF call this “pull the algorithm out into a separate object” technique the Strategy pattern

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # pull the algorithm of renderer outside of the class
    class Reporter
    def initialize(content, renderer)
    @content = content
    @renderer = renderer
    end
    def render
    @renderer.render(@content)
    end
    end


    class MarkdownRenderer
    def self.render(content)
    # markdown render
    end
    end


    class HTMLRenderer
    def self.render(content)
    # html render
    end
    end


    content = "##title##"
    htmlReport = Reporter.new(content, HTMLRenderer)
    markdownReport = Reporter.new(content, MarkdownRenderer)

    Thanks for Ruby’s duck typing, we don’t need to have a base abstract class for the Renderer. We only need to make sure a class method render is defined

    Even we can use a callable proc to remove the class declaration.

    Example of this pattern in ruby: the Auth middle-ware Warden

    Observer pattern

    Take ActiveRecord hooks as an Example.

    n a nice example of the Convention Over Configuration pattern (see Chapter 18), ActiveRecord does not require you to register your observer: It just figures out that EmployeeObserver is there to observe Employees, based on the class name

    如果你觉得本文对你有帮助,请给我点赞助。