13. Interfaces and Abstracts

13.1. Interfaces

  • Nie można tworzyć instancji

  • Wszystkie metody muszą być zaimplementowane przez potomków

  • Tylko deklaracje metod

  • Metody nie mogą mieć implementacji

Listing 304. 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')

13.2. Abstract classes and methods

  • Nie można tworzyć instancji

  • Można tworzyć implementację metod

Listing 305. Abstract Class
from abc import ABC, abstractmethod


class Document(ABC):
    def __init__(self, filename):
        self.filename = filename

    @abstractmethod
    def display(self):
        with open(self.filename) as file:
            return file.read()

class PDFDocument(Document):
    def display(self):
        content = super().display()
        # display ``content`` as PDF Document

class WordDocument(Document):
    def display(self):
        content = self.display()
        # display ``content`` as Word Document


file1 = PDFDocument('filename.pdf')
file1.display()

file2 = Document('filename.txt')  # TypeError: Can't instantiate abstract class Document with abstract methods display