Легковаговик — це структурний патерн, який економить пам’ять завдяки розподілу спільного стану, винесеного в один об’єкт, між безліччю об’єктів.
Легковаговик дозволяє економити пам’ять, записуючи в кеш однакові дані, що використовуються різними об’єктами.
Складність:
Популярність:
Застосування: Сенс використання Легковаговика — це економія пам’яті. Тому, якщо в програмі немає такої проблеми, ви навряд чи знайдете там приклади Легковаговика.
Ознаки застосування патерна: Легковаговик можна визначити за створюваними методами класу, які повертають закешовані об’єкти, замість створення нових.
Концептуальний приклад
Цей приклад показує структуру патерна Легковаговик , а саме — з яких класів він складається, які ролі ці класи виконують і як вони взаємодіють один з одним.
main.rb: Приклад структури патерна
require 'json'
# The Flyweight stores a common portion of the state (also called intrinsic
# state) that belongs to multiple real business entities. The Flyweight accepts
# the rest of the state (extrinsic state, unique for each entity) via its method
# parameters.
class Flyweight
# @param [String] shared_state
def initialize(shared_state)
@shared_state = shared_state
end
# @param [String] unique_state
def operation(unique_state)
s = @shared_state.to_json
u = unique_state.to_json
print "Flyweight: Displaying shared (#{s}) and unique (#{u}) state."
end
end
# The Flyweight Factory creates and manages the Flyweight objects. It ensures
# that flyweights are shared correctly. When the client requests a flyweight,
# the factory either returns an existing instance or creates a new one, if it
# doesn't exist yet.
class FlyweightFactory
# @param [Hash] initial_flyweights
def initialize(initial_flyweights)
@flyweights = {}
initial_flyweights.each do |state|
@flyweights[get_key(state)] = Flyweight.new(state)
end
end
# Returns a Flyweight's string hash for a given state.
def get_key(state)
state.sort.join('_')
end
# Returns an existing Flyweight with a given state or creates a new one.
def get_flyweight(shared_state)
key = get_key(shared_state)
if !@flyweights.key?(key)
puts "FlyweightFactory: Can't find a flyweight, creating new one."
@flyweights[key] = Flyweight.new(shared_state)
else
puts 'FlyweightFactory: Reusing existing flyweight.'
end
@flyweights[key]
end
def list_flyweights
puts "FlyweightFactory: I have #{@flyweights.size} flyweights:"
print @flyweights.keys.join("\n")
end
end
# @param [FlyweightFactory] factory
# @param [String] plates
# @param [String] owner
# @param [String] brand
# @param [String] model
# @param [String] color
def add_car_to_police_database(factory, plates, owner, brand, model, color)
puts "\n\nClient: Adding a car to database."
flyweight = factory.get_flyweight([brand, model, color])
# The client code either stores or calculates extrinsic state and passes it to
# the flyweight's methods.
flyweight.operation([plates, owner])
end
# The client code usually creates a bunch of pre-populated flyweights in the
# initialization stage of the application.
factory = FlyweightFactory.new([
%w[Chevrolet Camaro2018 pink],
['Mercedes Benz', 'C300', 'black'],
['Mercedes Benz', 'C500', 'red'],
%w[BMW M5 red],
%w[BMW X6 white]
])
factory.list_flyweights
add_car_to_police_database(factory, 'CL234IR', 'James Doe', 'BMW', 'M5', 'red')
add_car_to_police_database(factory, 'CL234IR', 'James Doe', 'BMW', 'X1', 'red')
puts "\n\n"
factory.list_flyweights
output.txt: Результат виконання
FlyweightFactory: I have 5 flyweights:
Camaro2018_Chevrolet_pink
C300_Mercedes Benz_black
C500_Mercedes Benz_red
BMW_M5_red
BMW_X6_white
Client: Adding a car to database.
FlyweightFactory: Reusing existing flyweight.
Flyweight: Displaying shared (["BMW","M5","red"]) and unique (["CL234IR","James Doe"]) state.
Client: Adding a car to database.
FlyweightFactory: Can't find a flyweight, creating new one.
Flyweight: Displaying shared (["BMW","X1","red"]) and unique (["CL234IR","James Doe"]) state.
FlyweightFactory: I have 6 flyweights:
Camaro2018_Chevrolet_pink
C300_Mercedes Benz_black
C500_Mercedes Benz_red
BMW_M5_red
BMW_X6_white
BMW_X1_red