Problem Statement
Without clear guidance on when to apply each structural pattern, developers often misuse them, leading to rigid, hard-to-maintain code or overcomplicated designs that slow down development and increase bugs.
This diagram shows client code interacting with different structural patterns to organize system components based on specific needs.
### Before: No pattern, direct class usage class OldService: def specific_request(self): return "Old service response" class Client: def __init__(self): self.service = OldService() def do_work(self): return self.service.specific_request() client = Client() print(client.do_work()) ### After: Adapter pattern applied class OldService: def specific_request(self): return "Old service response" class ServiceAdapter: def __init__(self, old_service): self.old_service = old_service def request(self): return self.old_service.specific_request() class Client: def __init__(self): self.service = ServiceAdapter(OldService()) def do_work(self): return self.service.request() client = Client() print(client.do_work())
class ResourceProxy {
constructor() {
this.resource = null;
}
access() {
if (!this.resource) {
this.resource = new RealResource();
}
return this.resource;
}
}class ComplexSystem {
operationA() {}
operationB() {}
operationC() {}
}
class SimpleInterface {
constructor() {
this.system = new ComplexSystem();
}
simpleOperation() {
this.system.operationA();
this.system.operationB();
this.system.operationC();
}
}
What is the likely mistake?Pass the test and this pattern moves unknown