Complete the code to define the Flyweight interface method.
class Flyweight: def [1](self, extrinsic_state): pass
The Flyweight interface defines an operation method that accepts extrinsic state.
Complete the code to implement intrinsic state storage in the ConcreteFlyweight class.
class ConcreteFlyweight(Flyweight): def __init__(self, [1]): self.intrinsic_state = intrinsic_state
The constructor takes intrinsic_state which is stored internally and shared.
Fix the error in the FlyweightFactory method to return existing flyweights or create new ones.
class FlyweightFactory: def __init__(self): self._flyweights = {} def get_flyweight(self, key): if key not in self._flyweights: self._flyweights[key] = ConcreteFlyweight([1]) return self._flyweights[key]
The factory uses the key as intrinsic state to create or retrieve flyweights.
Fill both blanks to complete the operation method using intrinsic and extrinsic state.
class ConcreteFlyweight(Flyweight): def __init__(self, intrinsic_state): self.intrinsic_state = intrinsic_state def operation(self, [1]): print(f"Intrinsic: {self.intrinsic_state}, Extrinsic: { [2] }")
The operation method takes extrinsic_state as parameter and uses it in the print statement.
Fill all three blanks to create a flyweight factory, get flyweights, and perform operations with extrinsic state.
factory = FlyweightFactory() flyweight1 = factory.get_flyweight([1]) flyweight2 = factory.get_flyweight([2]) flyweight1.operation([3])
The factory creates flyweights with keys "A" and "B". The operation uses extrinsic state "extrinsic1".