顯示具有 Design Pattern 標籤的文章。 顯示所有文章
顯示具有 Design Pattern 標籤的文章。 顯示所有文章

2018年6月29日 星期五

可以傳 AR object 的自制 form object


```ruby
class BaseForm
  include ActiveModel::Model

  attr_reader :record
  def initialize(record)
    @record = record
    record.attributes.each do |key, value|
      class_eval do
        delegate "#{key}=", to: :record
        delegate "#{key}", to: :record
      end
    end
  end

  def save
    valid? && record.save
  end

  def save!
    valid? && record.save!
  end
end

```


2018年6月10日 星期日

用 Ruby proc 做 success and error handler, like JS Promise


```rb

# A base class for all classes implement calls to API.
class ApiCall
  attr_reader :params

  def self.call(params)
    new(params).call
  end

  def initialize(params)
    @params = params
  end

  def call
    @res = execute
    self
  end

  def on_success
    yield @res if @res.success
    self
  end

  def on_error
    yield @res unless @res.success
    self
  end

  private

  def execute
    fail NotImplementedError
  end
end

StripeCall.(number: 'valid')
  .on_success { |response| puts response.body }
  .on_error { |response| puts response.body }
# => ok response

StripeCall.(number: 'invalid')
  .on_success { |response| puts response.body }
  .on_error { |response| puts response.body }
# => bad response
```


感覺有點借鑑 JS promise 的做法,傳 on_success 和 on_failure 的 proc 進到 method 當作 handler,之前有想過這種做法,剛好在 Ruby weekly 看到有人示範了,的確是減少了 if else 判斷,也省略了 response 這種 hidden context,不過最後為了寫的方便一點用了 method(:handle_success) 就沒有特別喜歡,但還是學習了這個用法,又是 meta programing 流派...


原文: https://railsguides.net/conditional-execution-with-dsl/


另外在 comment 裡看到一個 gem https://github.com/apneadiving/waterfall

也是挺有趣的,真的很有 functional programing 的味道

是不是大家開始慢慢被影響導致越來越多人用 Funtional Ruby 了呢~?最近也常常聽到和看到有人要用 Ruby 實現以及使用 elixir pipe operator XD

2018年5月21日 星期一

一篇講 OO cohesion 和 coupling 簡單易懂的文章

一直想找個好方式跟同事解釋怎麼 decoupling 和 design better code,這篇文章涵蓋了最重要的兩個概念而且用很簡單易懂的 code 解釋了,很不錯。

cohesion: class 裡的 methods 是不是 share 一樣的 context?

coupling: dependency

http://www.rubyguides.com/2018/05/ruby-cohesion-and-coupling/

2018年4月26日 星期四

kickstarter 的 event sourcing approach

也解釋了一些 event sourcing 相關的東西,還不錯的深入淺出好文,而且還有 gif 圖加分XD

這張就很棒的解釋了他們所謂的 Aggregation (綠色, state), Reactor (黃色), Calculator (藍色箭頭), Event (藍色方塊)



https://kickstarter.engineering/event-sourcing-made-simple-4a2625113224

2018年2月24日 星期六

builder pattern




http://rockssdlog.blogspot.jp/2012/05/design-pattern-builder-pattern.html

2017年11月13日 星期一

rails before_actoin design pattern - Aspect-oriented_programming (AOP)

想要對 ruby class 實作 before_action ,查到

https://stackoverflow.com/questions/23444964/is-it-possible-to-do-a-before-action-in-ruby-like-in-rails

於是看了一下 Aspect-oriented_programming (AOP)

https://en.wikipedia.org/wiki/Aspect-oriented_programming

看更簡單的解釋:
https://stackoverflow.com/questions/242177/what-is-aspect-oriented-programming

也不用解釋太多,其實就是 before_action 在做的事。

2017年10月19日 星期四

rails singleton class



```rb
# File activesupport/lib/active_support/core_ext/object/singleton_class.rb, line 3
  def singleton_class
    class << self
      self
    end
  end
```

2017年7月26日 星期三

Null object pattern


在需要 check object 是否存在的時候使用 null object pattern,如果 object 不存在就用 null object,null object 提供跟原本 object 很像的 API 接口,並預設這些 API 的行為,讓程式可以安心的使用這些 API


https://www.tutorialspoint.com/design_pattern/null_object_pattern.htm

Ruby:

https://gist.github.com/davidbella/6918455

Form object pattern


優點:

1. 把讓 controller 和 view 的邏輯拆開,讓 form object 負責,符合 single responsibility principle
2. 如果 include active_record 可以享用 validations, errors 和符合 convention 等優點




http://culttt.com/2015/11/04/using-form-objects-in-ruby-on-rails/
https://webuild.envato.com/blog/creating-form-objects-with-activemodel-and-virtus/
https://robots.thoughtbot.com/activemodel-form-objects

2017年5月19日 星期五

非常詳細且很棒的 hypermedia api 介紹


讓 client side 知道下一步做什麼,進而可以寫出像 call method 一樣的 client helper。


https://robots.thoughtbot.com/writing-a-hypermedia-api-client-in-ruby

2017年4月7日 星期五

OOP - interface segregation principle ISP 介面分離原則


用戶不應該被迫相依於他們用不到的函示

如果有用不到的函示,應該做成不同的 interface 或是 protocal

有要用到再裝上去就好

https://danielkjchen.wordpress.com/2016/02/05/%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E4%BB%8B%E9%9D%A2%E9%9A%94%E9%9B%A2%E5%8E%9F%E5%89%87-interface-segregation-principle/

http://ithelp.ithome.com.tw/articles/10101106


OOP - Law of Demeter 最小知識原則


意思就是假設 A 要問 B 一個問題,但是 B 要問 C 才能知道答案,那麼 A 應該只需要問 B 就好,A 不需要知道 B 還需要問 C,對 A 來說問 B 就能知道答案了

範例

A.askB #=> Answer

違反此原則的範例:

A.askB.askC #=> Answer


http://ithelp.ithome.com.tw/articles/10101265

OOP - Dependency-Inversion Principle


重點:program to an interface,  not an implementation


原本上層的類別會依賴下層的類別,就如同要蓋二樓就必須蓋好一樓
但 Dependency-Inversion Principle 的意思就是應該要讓上層和下層都依賴於抽象層,也就是上面重點說的 interface



http://teddy-chen-tw.blogspot.com.au/2012/01/5dependency-inversion-principle.html

OOP - LSP(Liskov Substitution Principle)Liskov替換原則


Liskov替換原則的定義是:『子類別必須能夠替代基礎類別』

不要繼承不必要的遺產,沒用到而去繼承反而是種累贅甚至會搞壞了整個系統也不一定。


http://ithelp.ithome.com.tw/articles/10100827

OOP - SRP(Single Responsibility Principle)單一責任原則


『你只有一個理由需要更改這個class,如果有一個以上的理由就表示:這個class負責超過一個以上的責任。』


http://ithelp.ithome.com.tw/articles/10100557

OOP - OCP (Open/Close Principle)


概念:讓程式「可以擴增」但是「不能修改」

http://ithelp.ithome.com.tw/articles/10100008