디자인 모델 - 생성과 관련된 모델
17367 단어 Ruby
Singleton
개요
그 반에는 단지 하나의 실례가 있을 것이라고 보증한다
외부에서 직접 대상을 생성하는 것을 방지하고 관리 클래스 자체가 동시에 생성할 수 있는 대상의 수량
용도
항상 프로그램에서 같은 대상을 처리하기를 원합니다
DB 연결 수를 제한하려면
예제
require 'singleton'
class SingletonObject
# instanceメソッドが定義され、newメソッドがprivateに設定される
include Singleton
attr_accessor :counter, :name
def initialize
@counter = 0
@name = "Singleton"
end
end
obj1 = SingletonObject.instance
obj1.counter += 1
p "--- #{obj1.name} ---"
p obj1.counter
obj2 = SingletonObject.instance
obj2.counter += 1
obj2.name = "UpdatedName"
p "--- #{obj1.name} ---"
p obj2.counter
p obj1.equal?(obj2)
# "--- Singleton ---"
# 1
# "--- UpdatedName ---"
# 2
# true
Builder
개요
다른 개체에 복잡한 인스턴스 생성 의뢰(Director)
Builder가 작업 담당
Director에서 작업 프로세스 담당
예제
# ConcreteBuilder
class SaltWater
attr_accessor :water, :salt, :color
def initialize(water, salt, color)
@water = water
@salt = salt
@color = color
end
def add_material(salt_amount)
@salt += salt_amount
end
def update_color
@color = "blue"
end
end
# ConcreteBuilder
class SugarWater
attr_accessor :water, :sugar, :color
def initialize(water, sugar, color)
@water = water
@sugar = sugar
@color = color
end
def add_material(sugar_amount)
@sugar += sugar_amount
end
def update_color
@color = "red"
end
end
# Builder
class WaterWithMaterialBuilder
attr_reader :water_with_material
def initialize(class_name)
@water_with_material = class_name.new(0,0, "white")
end
def add_material(material_amount)
@water_with_material.add_material(material_amount)
end
def add_water(water_amount)
@water_with_material.water += water_amount
end
def update_color
@water_with_material.update_color
end
def result
@water_with_material
end
end
# Director
class Director
attr_reader :builder
def initialize(builder)
@builder = builder
end
def magic
@builder.add_water(150)
@builder.add_material(90)
@builder.add_material(35)
@builder.update_color()
end
end
builder = WaterWithMaterialBuilder.new(SugarWater)
director = Director.new(builder)
director.magic
p builder.result
p director.builder.water_with_material.color
# #<SugarWater:0x007fed55824b10 @water=150, @sugar=125, @color="red">
# "red"
builder = WaterWithMaterialBuilder.new(SaltWater)
director = Director.new(builder)
director.magic
p builder.result
p director.builder.water_with_material.color
# #<SaltWater:0x007fed5582fe98 @water=150, @salt=125, @color="blue">
# "blue"
Abstract Factory
개요
연관된 객체의 컬렉션을 생성할 수 있습니다.
모순되지 않는 대상을 만들기 위해
예제
class DinnaerFactory
def initialize
end
def createDinner
return [createAppetizer,createMain]
end
def createMain
end
def createAppetizer
end
end
class Soup
def initialize
end
end
class Rice
def initialize
end
end
class Salad
def initialize
end
end
class Pizza
def initialize
end
end
class JapaneseFactory < DinnaerFactory
def createMain
Rice.new
end
def createAppetizer
Soup.new
end
end
class ItalyFactory < DinnaerFactory
def createMain
Pizza.new
end
def createAppetizer
Salad.new
end
end
japanese = JapaneseFactory.new
puts japanese.createDinner
italy = ItalyFactory.new
puts italy.createDinner
Factory Method
개요
하위 클래스에 대한 인스턴스 생성
실례의 생성 부분을 끊으면 추가, 변경, 유지보수에 편리하도록 결합도를 낮출 수 있다
예제
class Saxophone
def initialize(name)
@name = name
end
def play
puts "#{self.class} #{@name} is being played!"
end
end
class Trumpet
def initialize(name)
@name = name
end
def play
puts "#{self.class} #{@name} is being played!"
end
end
class InstrumentFactory
def initialize(number_instruments)
@instruments = []
number_instruments.times do |i|
instrument = new_instrument("No.#{i + 1}")
@instruments << instrument
end
end
def ship_out
@tmp = @instruments.dup
@instruments = []
@tmp
end
end
class SaxophoneFactory < InstrumentFactory
def new_instrument(name)
Saxophone.new(name)
end
end
class TrumpetFactory < InstrumentFactory
def new_instrument(name)
Trumpet.new(name)
end
end
factory = SaxophoneFactory.new(3)
saxophones = factory.ship_out
saxophones.each { |saxophone| saxophone.play }
factory = TrumpetFactory.new(2)
trumpets = factory.ship_out
trumpets.each { |trumpet| trumpet.play }
Reference
이 문제에 관하여(디자인 모델 - 생성과 관련된 모델), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/sunoko/items/ef85ec577f900606b56c텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)