Last active
August 29, 2015 14:02
-
-
Save onigra/52b48e26108ccd866fe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 基底クラス | |
class Payment | |
def payment_class(payment_type) | |
"#{payment_type}Payment" | |
end | |
def pay(payment_type) | |
Object.const_get(payment_class payment_type).new.commit | |
end | |
end | |
# 具象クラス | |
class CardPayment < Payment | |
def commit | |
puts "card" | |
end | |
end | |
class ConveniPayment < Payment | |
def commit | |
puts "conveni" | |
end | |
end | |
# 呼び出す側 | |
class Order | |
def pay(payment_method) | |
Payment.new.pay payment_method | |
end | |
end | |
Order.new.pay('Card') # => CardPayment.commit | |
Order.new.pay('Conveni') # => ConveniPayment.commit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 基底クラス | |
class Payment | |
def payment_class(payment_type) | |
"#{payment_type}Payment" | |
end | |
def pay(payment_type, order) | |
Object.const_get(payment_class payment_type).new.commit order | |
end | |
end | |
# 具象クラス | |
class CardPayment < Payment | |
def commit(order) | |
call_api order.card_attributes | |
end | |
def call_api | |
# カード決済のAPI叩く | |
end | |
end | |
class ConveniPayment < Payment | |
def commit(order) | |
call_api order.conveni_attributes | |
end | |
def call_api | |
# コンビニ決済のAPI叩く | |
end | |
end | |
# 呼び出す側 | |
class Order | |
def pay(payment_method) | |
Payment.new.pay(payment_method, self) | |
end | |
def card_attributes | |
# クレジットカードの決済APIを叩く用の引数 | |
end | |
def conveni_attributes | |
# コンビニの決済APIを叩く用の引数 | |
end | |
end | |
Order.new.pay('Card') # => CardPayment.commit | |
Order.new.pay('Conveni') # => ConveniPayment.commit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Payment | |
{ | |
public static function new_instance($payment_type) | |
{ | |
return new sprintf('%sPayment', $payment_type); | |
} | |
public function pay() | |
{ | |
$this->commit(); | |
} | |
abstract protected function commit(); | |
} | |
class CardPayment extends Payment | |
{ | |
protected function commit() | |
{ | |
} | |
} | |
class ConveniPayment extends Payment | |
{ | |
protected function commit() | |
{ | |
} | |
} | |
Payment::new_instance('Card')->pay(); | |
Payment::new_instance('Conveni')->pay(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 基底クラス | |
class Payment | |
def self.commit | |
end | |
end | |
# 具象クラス | |
class CardPayment < Payment | |
end | |
class ConveniPayment < Payment | |
end | |
# 呼び出す側 | |
class Order | |
def pay(payment_type) | |
"#{payment_type}Payment".constantize.commit | |
end | |
end | |
Order.new.pay('Card') # => CardPayment.commit | |
Order.new.pay('Conveni') # => ConveniPayment.commit |
uru-bns
commented
Jun 17, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment