0
0

More than 1 year has passed since last update.

OOP: interfaceについて

Last updated at Posted at 2022-05-16

interface

interfaceはただ戻りと引数だけが定義された写像を書いてるだけで写像の具体的な演算は定義されていない。
そのクラスに定義された写像の一覧を作っておくことでクラスを分類できるようにする。
interfaceはClassではないのでそれ単体ではインスタンスを生成できない。interfaceを使うには、あとでクラスを作るときにinterfaceをクラスに取り込み、interfaceで定義された写像の中身の演算を定義しなければならない。

interface_ex.php
interface DataStrageAccessInterface
{
    public function fetch($key);
    public function save($key, $value);
}
class DbAccessor implements DataStrageAccessInterface
{
    public function fetch($key)
    {
        return Database::fetch($key);
    }

    public function save($key, $value)
    {
        return Database::save($key, $value);
    }
}

class FileSystemAccessor implements DataStrageAccessInterface
{
    private $csv = null;

    public function __construct($fileName)
    {
        $this->csv = CsvAccessorFactory::create($fileName);
    }

    public function fetch($key)
    {
        return $this->csv->fetch($key);
    }

    public function save($key, $value)
    {
        return $this->csv->save($key, $value);
    }
}

FileSystemAccessor implements DataStrageAccessInterfaceを訳すと「FileSystemAccessorはDataStrageAccessInterfaceを実装します」なので日本語のままの意味である。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0