Complete the code to define a structural pattern that connects classes.
class Adapter: def __init__(self, adaptee): self.adaptee = adaptee def request(self): return self.adaptee.[1]()
The Adapter pattern wraps an adaptee's specific method to match the expected interface.
Complete the code to show how a decorator adds behavior to a class.
class Component: def operation(self): return "Component operation" class Decorator(Component): def __init__(self, component): self.component = component def operation(self): return self.component.operation() + [1]
The decorator extends the original operation by adding extra behavior as a string.
Fix the error in the composite pattern code to correctly add child components.
class Composite: def __init__(self): self.children = [] def add(self, component): self.children.[1](component)
To add a single component to the children list, use the append method.
Fill both blanks to complete the proxy pattern that controls access to a resource.
class Proxy: def __init__(self, real_subject): self.real_subject = real_subject def request(self): if self.check_access(): return self.real_subject.[1]() else: return [2]
The proxy calls the real subject's request method if access is allowed; otherwise, it returns an access denied message.
Fill all three blanks to complete the flyweight pattern that shares objects efficiently.
class FlyweightFactory: def __init__(self): self._flyweights = {} def get_flyweight(self, key): if key not in self._flyweights: self._flyweights[[1]] = Flyweight([2]) return self._flyweights[[3]]
The factory uses the key to check and store flyweight objects, ensuring reuse.