Jul-26-2020, 07:04 PM
(Jul-26-2020, 05:43 PM)deanhystad Wrote: Good or bad design depends on the problem you are trying to solve. Global variables are usually bad, but sometimes they can lead to an elegant solution for the right kind of problems. Using a class with no instances is usually bad, but it can be an elegant way to implement a singleton.
With no information about your problem it is impossible to say if any design is good or bad.
Ok, let's assume you have a few workshops, you want to track the component list and sometimes they got a refill. (i know it's simplistic, but for now that's all i need)
Is it a good/bad design? How you would improve this?
class Workshop: def __init__(self, name): self.name = name self.components_data = ['d2', 'f3', 'g4', 'h5'] self.refill_all_components() def refill_all_components(self): self.components_available = self.components_data[:] self.components_used = [] def use_a_component(self): from random import shuffle shuffle(self.components_available) self.components_used.append(self.components_available.pop()) def __repr__(self): return (f'{self.name} components left: {self.components_available} components used: {self.components_used}') workshop1 = Workshop('Unit1') workshop2 = Workshop('Unit2') workshop3 = Workshop('Unit3') for i in range(2): workshop1.use_a_component() print(workshop1) workshop1.refill_all_components() print(workshop1)