Answer the question
In order to leave comments, you need to log in
Is the principle of openness/closedness fully respected?
In the following code, I simulate the loading of two types of engines into the warehouse. Please help me understand how well the principle of openness / closeness is observed.
If I add a third type engine, then I don't have to change the existing code. And you just have to add the load_diesel_engines () method to the Storage class. It seems to me that this is correct.
Doubts arise because when adding a third type of engine, you still have to change the code in which the user is prompted to enter a quantity via input()
petrol_engine_cnt = input('сколько двигателей типа pertol загрузить на склад?')
electro_engine_cnt = input('сколько двигателей типа electro загрузить на склад?')
storage = Storage()
storage.load_petrol_engines(int(petrol_engine_cnt), PetrolEngine)
storage.load_electro_engines(int(electro_engine_cnt), ElectroEngine)
print(storage.get_engines())
class Storage:
def __init__(self):
self.engines = []
def load_petrol_engines(self, cnt, Engine):
for i in range(cnt):
self.engines.append(Engine())
def load_electro_engines(self, cnt, Engine):
for i in range(cnt):
self.engines.append(Engine())
def get_engines(self):
return self.engines
class IEngine(metaclass=ABCMeta):
@abstractmethod
def get_type(self):
pass
@abstractmethod
def get_weight(self):
pass
class PetrolEngine(IEngine):
def __init__(self):
self.type = 'petrol'
self.weight = 100
self.serial_num = random.randint(1000000, 9999999)
def get_type(self):
return self.type
def get_weight(self):
return self.weight
def __repr__(self):
return '<Engine_raw: {}/{}>'.format(self.type, self.weight)
class ElectroEngine(IEngine):
def __init__(self):
self.type = 'electro'
self.weight = 50
self.serial_num = random.randint(1000000, 9999999)
def get_type(self):
return self.type
def get_weight(self):
return self.weight
def __repr__(self):
return '<Engine_raw: {}/{}>'.format(self.type, self.weight)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question