/ Design Patterns / Behavioral patterns / Null Object Null Object Design Pattern in Python Back to Null Object description """ Encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. """ import abc class AbstractObject(metaclass=abc.ABCMeta): """ Declare the interface for Client's collaborator. Implement default behavior for the interface common to all classes, as appropriate. """ @abc.abstractmethod def request(self): pass class RealObject(AbstractObject): """ Define a concrete subclass of AbstractObject whose instances provide useful behavior that Client expects. """ def request(self): pass class NullObject(AbstractObject): """ Provide an interface identical to AbstractObject's so that a null object can be substituted for a real object. Implement its interface to do nothing. What exactly it means to do nothing depends on what sort of behavior Client is expecting. """ def request(self): pass Support our free website and own the eBook! 22 design patterns and 8 principles explained in depth 406 well-structured, easy to read, jargon-free pages 228 clear and helpful illustrations and diagrams An archive with code examples in 4 languages All devices supported: EPUB/MOBI/PDF formats Learn more...