PHP OOP 筆記

# 前言

留下我學習 OOP 的痕跡

# 正文

# Object-Oriented and Procedural Programming

以下列出兩種範例, 藉由程式碼來比較兩者的不同之處

# Procedural

<?php
function readParams(string $source): array
{
$params = [];
// read text parameters from $source
return $params;
}
function writeParams(array $params, string $source)
{
// write text parameters to $source
}

$file = __DIR__ . "/params.txt";
$params = [
"key1" => "val1",
"key2" => "val2",
"key3" => "val3",
];
writeParams($params, $file);
$output = readParams($file);
print_r($output);

function readParams(string $source): array
{
$params = [];
if (preg_match("/\.xml$/i", $source)) {
// read XML parameters from $source
} else {
// read text parameters from $source
}
return $params;
}
function writeParams(array $params, string $source)
{
if (preg_match("/\.xml$/i", $source)) {
// write XML parameters to $source
} else {
// write text parameters to $source
}
}

# Object-Oriented

<?php
abstract class ParamHandler
{
protected $source;
protected $params = [];
public function __construct(string $source)
{
$this->source = $source;
}
public function addParam(string $key, string $val)
{
$this->params[$key] = $val;
}
public function getAllParams(): array
{
return $this->params;
}
public static function getInstance(string $filename): ParamHandler
{
if (preg_match("/\.xml$/i", $filename)) {
return new XmlParamHandler($filename);
}
return new TextParamHandler($filename);
}
abstract public function write(): bool;
abstract public function read(): bool;
}

class XmlParamHandler extends ParamHandler
{
public function write(): bool
{
// write XML
// using $this->params
}
public function read(): bool
{
// read XML
// and populate $this->params
}
}
// listing 06.06
class TextParamHandler extends ParamHandler
{
public function write(): bool
{
// write text
// using $this->params
}
public function read(): bool
{
// read text
// and populate $this->params
}
}

$test = ParamHandler::getInstance(__DIR__ . "/params.xml");
$test->addParam("key1", "val1");
$test->addParam("key2", "val2");
$test->addParam("key3", "val3");
$test->write(); // writing in XML format

$test = ParamHandler::getInstance(__DIR__ . "/params.txt");
$test->read(); // reading in text format
$params = $test->getAllParams();
print_r($params);

# UML

通常要靠程式碼來描繪出整個架構會比較不好表示, 所以可以通過 UML 來描繪整個架構

# Class

# Abstract Class

# Interface

# Class Properties & Methods


# Inheritance

Cd product 以及 BookProduct class 繼承了 ShopProduct

# Implementation

ShopProduct 實作了 Chargeable interface

# Relations

  • Teacher class 與 Pupil class 之間有 relationship, 可能是 Teacher 有 Pupil, 也可能是 Pupil 有 Teacher, 也可能互相有

  • Teacher class 有一個或多個 Pupil class

  • Teacher class 與 Pupil class 互有一個或多個

  • 一個 Teacher class 有多個 Pupil class

  • 一個 Teacher class 有 5~10 個 Pupil class

# Aggregation

SchoolClass class 由 Pupil class 組成, 但刪除 A SchoolClass 時不見得要刪除對應的 Pupil class, 因為 Pupil class 可能被複數的 SchoolClass 所擁有

# Composition

person 由對應的 SocialSecurityData 組成, 每個 SocialSecurityData 只屬於特定的一個 person, 若刪除 person 則必定要刪除 SocialSecurityData

# Use

Report class 使用了 ShopProductWriter

# Use Note

Report class 使用了 ShopProductWriter, 且詳述了使用細節

Leetcode 筆記 資料結構 - Heap - PHP 實作

留言

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×