Ruby 코드 예제로 S.O.L.I.D 원칙 설명하기
이번 주에는 Ugonna Thelma의 이 기사The S.O.L.I.D Principles in Pictures에서 설명한 S.O.L.I.D 원칙을 참조하여 루비로 코드 예제를 제공할 것입니다. 이것은 내가 지금까지 본 것 중 가장 선명한 이미지로 설명합니다.
그래서 제가 하려고 하는 것은 Ruby에서 그녀의 일러스트레이션 코드 예제를 보여주는 것입니다.
S — 단일 책임
# don't do this
class JackOfAllTrade
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def cook
"On it. #{name} is cooking your favorite food"
end
def garden
"#{name} is planting new flowers in the garden"
end
def paint
"#{name} is painting the walls in the sitting room"
end
def drive
"#{name} is driving you to the airport"
end
end
ngozi = JackOfAllTrade.new('Ngozi')
puts ngozi.cook
puts ngozi.garden
puts ngozi.paint
puts ngozi.drive
# => On it. Ngozi is cooking your favorite food
# => Ngozi is planting new flowers in the garden
# => Ngozi is painting the walls in the sitting room
# => Ngozi is driving you to the airport
# do this instead
class Chef
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def cook
"On it. #{name} is cooking your favorite food"
end
end
class Gardener
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def garden
"#{name} is planting new flowers in the garden"
end
end
class Painter
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def paint
"#{name} is painting the walls in the sitting room"
end
end
class Driver
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def drive
"#{name} is driving you to the airport"
end
end
ngozi_the_chef = Chef.new('Ngozi')
ngozi_the_gardener = Gardener.new('Ngozi')
ngozi_the_painter = Painter.new('Ngozi')
ngozi_the_driver = Driver.new('Ngozi')
puts ngozi_the_chef.cook
puts ngozi_the_gardener.garden
puts ngozi_the_painter.paint
puts ngozi_the_driver.drive
# => On it. Ngozi is cooking your favorite food
# => Ngozi is planting new flowers in the garden
# => Ngozi is painting the walls in the sitting room
# => Ngozi is driving you to the airport
# these should throw an `undefined method` error because it doesn't perform that function
puts ngozi_the_chef.garden
# => undefined method `garden' for #<Chef:0x00007ff4918305d0 @name="Ngozi"> (NoMethodError)
# puts ngozi_the_gardener.paint
# puts ngozi_the_painter.drive
# puts ngozi_the_driver.cook
O — 개방형
# don't do this
class Chef
def initialize(name)
@name = name.capitalize
@description = 'cooking a delicious meal'
end
def cook
"#{@name} is #{@description}"
end
attr_writer :description
end
ngozi_the_chef = Chef.new('ngozi')
puts ngozi_the_chef.cook
ngozi_the_chef.description = 'painting the house'
puts ngozi_the_chef.cook
# => Ngozi is cooking a delicious meal
# => Ngozi is painting the house
# do this instead
class Chef
def initialize(name)
@name = name.capitalize
end
def cook
"#{@name} is cooking a delicious meal"
end
end
class ChefAndPainter < Chef
def paint
"#{@name} is painting the walls in the sitting room"
end
end
ngozi_the_chef = Chef.new('ngozi')
puts ngozi_the_chef.cook
ngozi_the_chef_and_painter = ChefAndPainter.new('ngozi')
puts ngozi_the_chef_and_painter.cook
puts ngozi_the_chef_and_painter.paint
# => Ngozi is cooking a delicious meal
# => Ngozi is cooking a delicious meal
# => Ngozi is painting the walls in the sitting room
L — Liskov 치환
# don't do this
class Server
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def serve_coffee
"I am #{name}. Here is your coffee"
end
def serve_water
"I am #{name}. Here is your water"
end
end
class ServerChild < Server
undef_method :serve_coffee
end
lade = Server.new('lade')
puts lade.serve_coffee
evans = ServerChild.new('evans')
puts evans.serve_water
puts evans.serve_coffee
# => I am Lade. Here is your coffee
# => I am Evans. Here is your water
# => Traceback (most recent call last):
# => test.rb:45:in `<main>': undefined method `serve_coffee' for #<ServerChild:0x00007fc4ee851848 @name="Evans">
# do this instead
class Server
def initialize(name)
@name = name.capitalize
end
attr_reader :name
def serve_coffee
"I am #{name}. Here is your coffee"
end
def serve_water
"I am #{name}. Here is your water"
end
end
class ServerChild < Server
def serve_coffee
"I am #{name}. Here is your cappucino"
end
end
lade = Server.new('lade')
puts lade.serve_coffee
evans = ServerChild.new('evans')
puts evans.serve_water
puts evans.serve_coffee
# => I am Lade. Here is your coffee
# => I am Evans. Here is your water
# => I am Evans. Here is your cappucino
I — 인터페이스 분리
# don't do this
class Robot
def initialize
@no_of_arms = 2
@no_of_antennas = 4
end
def spin_around
'I can spin around'
end
def rotate_arm
"I am rotating my #{@no_of_arms} arms"
end
def paint_house
"Painting the house with my painting brush arm"
end
def search_for_stations
"#{@no_of_antennas} antennas connecting to the closest radio station"
end
end
class PainterRobot < Robot
end
class RadioRobot < Robot
end
# RadioRobot should not know anything about painting and PainterRobot shouldn't concern itself with searching for stations
puts RadioRobot.new.paint_house
puts PainterRobot.new.search_for_stations
# => Painting the house with my painting brush arm
# => 4 antennas connecting to the closest radio station
# do this
class Robot
def initialize
@no_of_arms = 2
end
def spin_around
'I can spin around'
end
def rotate_arm
"I am rotating my #{@no_of_arms} arms"
end
end
class PainterRobot < Robot
def paint_house
"Painting the house with my painting brush arm"
end
end
class RadioRobot < Robot
def initialize
@no_of_antennas = 4
end
def search_for_stations
"#{@no_of_antennas} antennas connecting to the closest radio station"
end
end
puts RadioRobot.new.search_for_stations
puts PainterRobot.new.paint_house
# => 4 antennas connecting to the closest radio station
# => Painting the house with my painting brush arm
D — 종속성 반전
# don't do this
class Robot
def initialize
@no_of_arms = 2
end
def spin_around
'I can spin around'
end
def rotate_arm
"I am rotating my #{@no_of_arms} arms"
end
end
class PainterRobot < Robot
def paint_house
"Painting the house with my painting brush arm"
end
end
puts PainterRobot.new.paint_house
# we want it to be able to paint with whatever tool we give it
# => Painting the house with my painting brush arm
# do this
class Robot
def initialize
@no_of_arms = 2
end
def spin_around
'I can spin around'
end
def rotate_arm
"I am rotating my #{@no_of_arms} arms"
end
end
class PainterRobot < Robot
def initialize(tool)
@tool = tool
end
def paint_house
"Painting the house with my #{@tool} arm"
end
end
puts PainterRobot.new('painting brush').paint_house
puts PainterRobot.new('paint sprayer').paint_house
puts PainterRobot.new('paint roller').paint_house
# => Painting the house with my painting brush arm
# => Painting the house with my paint sprayer arm
# => Painting the house with my paint roller arm
그것이 이 기사의 내용입니다. 개념을 더 이해하기 위해 주요 기사를 읽을 수 있습니다.
아래에 의견과 생각을 남겨주세요. 나는 그들을 읽고 싶습니다.
다음주까지.
Reference
이 문제에 관하여(Ruby 코드 예제로 S.O.L.I.D 원칙 설명하기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/nkemjiks/explaining-s-o-l-i-d-principles-with-ruby-code-examples-2636텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)