8. Interface

8.1. What it is?

  • Python don't have interfaces

  • Cannot instantiate

  • Inheriting class must implement all methods

  • Only method declaration

Listing 360. Interfaces
class CacheInterface:
    def get(self, key: str) -> str:
        raise NotImplementedError

    def set(self, key: str, value: str) -> None:
        raise NotImplementedError

    def is_valid(self, key: str) -> bool:
        raise NotImplementedError

8.2. When use it?

8.3. Example

Listing 361. Interfaces
class CacheInterface:
    def get(self, key: str) -> str:
        raise NotImplementedError

    def set(self, key: str, value: str) -> None:
        raise NotImplementedError

    def is_valid(self, key: str) -> bool:
        raise NotImplementedError


class CacheDatabase(CacheInterface):
    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


class CacheRAM(CacheInterface):
    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


class CacheFilesystem(CacheInterface):
    def is_valid(self, key: str) -> bool:
        ...

    def get(self, key: str) -> str:
        ...

    def set(self, key: str, value: str) -> None:
        ...


fs = CacheFilesystem()
fs.set('name', 'Jan Twardowski')
fs.is_valid('name')
fs.get('name')

ram = CacheRAM()
ram.set('name', 'Jan Twardowski')
ram.is_valid('name')
ram.get('name')

db = CacheDatabase()
db.set('name', 'Jan Twardowski')
db.is_valid('name')
db.get('name')