Mixin
- Dont’t Repeat Yourself(DRY).
- An optional add-on; without a Mixin, the object would work just fine.
- Can override, or inject code into, the target’s methods
class BaseServer:
def process_request(self, request, client_address):
"""Can be Overridden by ForkingMixIn and ThreadingMixIn."""
pass
class TCPServer(BaseServer):
def __init__(self, *args, **kwargs):
# bind the TCP socket to a address and listen to the socket
# the rest would be handled by the BaseServer
pass
class ForkingMixIn:
def process_request(self, request, client_address):
"""Fork a new subprocess to process the request."""
pass
class ThreadingMixIn:
def process_request(self, request, client_address):
"""Create a new non-daemonic thread to process the request."""
pass
class ForkingTCPServer(ForkingMixIn, TCPServer):
pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
Abstract Class | Interface | Mixin | |
---|---|---|---|
Need implementation | Optional | F | T |
Can be extended | T | T | F |
Written on June 21, 2021