In this cyberpunk world, it’s critical to have a rock-solid codebase that can handle any request that comes its way. And that’s where the Chain of Responsibility pattern comes in. Think of it as a cybernetically enhanced bodyguard that can handle any request, be it a simple “Matrix Request” or a high-stakes “Hack Request”.
interface CyberpunkRequest { public function handle(); } class HackRequest implements CyberpunkRequest { public function handle() { echo "Initiating hack sequence...\n"; echo "Hacking into top secret government website...\n"; echo "Hack successful. Welcome to the Matrix.\n"; } } class MatrixRequest implements CyberpunkRequest { public function handle() { echo "Loading Matrix...\n"; echo "Welcome to the red pill version of the Matrix.\n"; } } class CyberpunkRequestHandler { public function handleRequest(CyberpunkRequest $request) { $request->handle(); } } // Usage $request = new HackRequest(); $handler = new CyberpunkRequestHandler(); $handler->handleRequest($request); $request = new MatrixRequest(); $handler->handleRequest($request);
And to make sure that this bodyguard is always ready for action, we’re going to use the SOLID principles:
interface CyberpunkRequest { public function handle(); } class HackRequest implements CyberpunkRequest { public function handle() { echo "Initiating hack sequence...\n"; echo "Hacking into top secret government website...\n"; echo "Hack successful. Welcome to the Matrix.\n"; } } class MatrixRequest implements CyberpunkRequest { public function handle() { echo "Loading Matrix...\n"; echo "Welcome to the red pill version of the Matrix.\n"; } } interface CyberpunkRequestHandler { public function handle(CyberpunkRequest $request); } class RequestHandler implements CyberpunkRequestHandler { public function handle(CyberpunkRequest $request) { $request->handle(); } } // Usage $request = new HackRequest(); $handler = new RequestHandler(); $handler->handle($request); $request = new MatrixRequest(); $handler->handle($request);
We’ve used the SOLID principles to write code that’s built to last:
- Single Responsibility Principle (SRP): Each class has a single responsibility. The HackRequest and MatrixRequest classes are responsible for handling their own specific request and the RequestHandler class is responsible for handling the request by calling the handle() method on the request object.
- Open-Closed Principle (OCP): Classes are open for extension, but closed for modification. For example, you can add new request types without modifying the existing RequestHandler class, by just creating new classes that implement the CyberpunkRequest interface.
- Liskov Substitution Principle (LSP): The RequestHandler class accepts any object that implements the CyberpunkRequest interface, allowing for the substitution of the request objects without affecting the correctness of the program.
- Interface Segregation Principle (ISP): The RequestHandler class only depends on the handle() method of the CyberpunkRequest interface, classes that implement this interface are not forced to implement unnecessary methods.
- Dependency Inversion Principle (DIP): High-level classes such as RequestHandler depend on the abstractions(interfaces) rather than the concrete implementations (classes) of the request objects.