What is Observer pattern?
“Define a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.”
Why do we need Observer pattern?
The Observer pattern allows you to notify parts of your program that some other part of your program has changed.
Can you give me an example of Observer pattern?
class Chernobyl
attr_reader :observers, :radition_level
def initialize
@observers = []
@radition_level = 0
end
def radition_level_increased
@radition_level += 1
notify_observers
end
def add_observer(observer)
observers «_space;observer
end
def remove_observer(observer)
observers.delete(observer)
end def notify_observers
observers.each do |observer|
observer.notify(self)
end
endend
class Scientist
def notify(stantion)
p stantion.radition_level
end
enddirector = Scientist.new worker = Scientist.new
chernobyl = Chernobyl.new
chernobyl. add_observer director
chernobyl. add_observer worker
chernobyl. radition_level_increased
chernobyl. radition_level_increased
What are the key components of Observer pattern?
Can you give example of the simplest observer?
$a = 1
def update
$a = $a + 1
puts 'Notification was sent'
# email.to('adaw@gmail.com', 'awdawd@ffea.go')
endupdate
update
update
p $a
What is Decorator pattern?
The Decorator pattern allows us to add behavior to a given object without having to add that behavior to the class of the object.
Can you give example of Decorator pattern?
class Person attr_reader :name, :surname
def initialize(name, surname)
@name = name
@surname = surname
end
end
class DecoratedPerson attr_reader :person
def initialize(person)
@person = person
enddef full_name
“#{person.name} #{person.surname}”
end
end
person = Person.new(‘Leo’, ‘Messi’)
p DecoratedPerson.new(person).full_name
What are the key component of Decorator?
1) Object
2) The method which adds additional behavior/state to this object
What is Factory method pattern?
Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.
Can you provide example of Factory Method pattern?
class ShapeFactory
def shape(name)
case name
when 'Circle'
Circle.new
when 'Square'
Square.new
when 'Rectangle'
Rectangle.new
end
end
end
class Circle
def create
'Circle was created'
end
end
class Square
def create
'Square was created'
end
end
class Rectangle
def create
'Rectangle was created'
end
endp ShapeFactory.new.shape(‘Rectangle’).create
What are the key components of Factory Method pattern?
1) Products classes ( which factory going to return )
2) Method (which every product has)
3) Factory method ( returns one of the Product class )
4) Run method
What is Abstract Factory Pattern?
Abstract Factory is a creational design pattern, which solves the problem of creating entire product families without specifying their concrete classes.
Can you give me an example of abstract factory?
class Furniture
def self.get_furniture(type)
case type
when 'English'
EnglishFurnitureFactory.new
when 'Europe'
EuropeFurnitureFactory.new
end
end
end
class EnglishFurnitureFactory
def factory_method(type)
# chair
# table
end
endclass EuropeFurnitureFactory
def factory_method(type)
end
end
p Furniture.get_furniture(‘English’).factory_method(‘Chair’)
What are the key elements of Abstract Fatories?
1) Many Product Factories ( with defined factory method)
2) One main factory_method to define which to select
What is Singleton pattern?
Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code
Can you give me example of Singleton class?
class Config attr_reader :instance
def self.instance
@instance ||= new
end
private_class_method :new
end
Config.instance
What is Command pattern?
Command is behavioral design pattern that converts requests or simple operations into objects.
Can you provide a simple example of Command pattern?
class TurnOnLight
def execute
'Light was turn on'
end
enddef run(command)
command.execute
end
p run(TurnOnLight.new)
What are the key element of Command pattern?
1) Class which incapsulate action ( command )
2) Invoker which executes this action
3) Undo the execution
Can you provide example of Adapter pattern?
class Cat
def meow
p "Meow"
end
end
cat = Cat.new
cat.meow
class Dog
def gav
p "Gav"
end
end
dog = Dog.new
dog.gav
class CatAdapter attr_reader :cat
def initialize(cat)
@cat = cat
end
def sound
cat.meow
end
end
class DogAdapter attr_reader :dog
def initialize(dog)
@dog = dog
enddef sound
dog.gav
end
end
[CatAdapter.new(cat), DogAdapter.new(dog)].each { |animal| animal.sound }
What is Adapter pattern?
Adapter is a structural design pattern, which allows incompatible objects to collaborate.
What are the key components of Adapter pattern?
What is Facade pattern?
Facade is a structural design pattern that provides a simplified interface to a complex system of classes, objects and methods.
Facade defines a higher-level interface that makes the subsystem easier to use
Can you provide an example of Facade pattern?
def show @cats = Cat.names @dogs = Dog.names #... @animal_count = @cats + @dogs # + ..
@animal_uniq_count = 0 # comlex logic
end
class Facade
def animal_count
@cats = Cat.names
@dogs = Dog.names
#...
@animal_count = @cats + @dogs # + ..
end
endFacade.new.animal_count