首頁 >後端開發 >php教程 >圖層超級類型模式:將共同實現封裝在多層系統中

圖層超級類型模式:將共同實現封裝在多層系統中

William Shakespeare
William Shakespeare原創
2025-02-26 11:51:17375瀏覽

The Layer Supertype Pattern: Encapsulating Common Implementation in Multi-Tiered Systems

核心要點

  • 層超類型模式在多層系統中至關重要,它能夠封裝不同類中的公共實現,從而促進代碼重用並減少重複。
  • 實現層超類型模式涉及創建共享基類,該基類抽像出公共邏輯和屬性,然後由更具體的子類擴展。
  • 此模式有助於維護更清晰的代碼架構,因為它允許在一個地方修改共享功能,從而增強可維護性和可擴展性。
  • 層超類型模式不僅簡化了代碼庫,而且還與單一責任原則很好地對齊,因為它將公共行為與特定於類的行為分隔開來。
  • 雖然該模式在減少樣板代碼和冗餘代碼方面提供了許多好處,但必須謹慎應用它,以避免創建過於復雜或龐大的超類結構,這些結構可能難以管理。

繼承作為面向對象編程的基石之一,就像一把雙刃劍,它既能帶來強大的代碼復用機制,避免使用組合模式帶來的複雜性,也能導致混亂的繼承體系,子類型與基類型的行為差異巨大,以至於“IS-A”關係名存實亡。儘管繼承存在諸多陷阱,但大部分可以通過合理和適度使用來減輕。代碼重用是繼承存在的根本原因,在多層系統抽像中添加樣板實現時,繼承可以發揮巨大作用。繼承提供了一種簡單的方法來輕鬆生成大量語義上相互關聯的對象,而無需重複代碼。其概念非常簡單但功能強大:首先在基類型的邊界內(通常是抽像類,但也可以是具體類)放入盡可能多的邏輯,然後根據更具體的需要開始派生細化的子類型。此過程通常以“每層”為基礎進行,從而為每一層提供其自身的一組超類型,其核心功能依次由相應的子類型提煉和擴展。毫不奇怪,這種重複的封裝/派生循環遵循了稱為“層超類型”的設計模式(是的,雖然有點天真,但它確實有一個真正的學術名稱),在接下來的幾行中,我將深入探討其內部工作原理,您將能夠看到將其功能連接到領域模型是多麼容易。

層超類型的需求——定義臃腫的領域模型

可以說,層超類型是“公共”基類型的自然和選擇性演變,只是後者存在於特定層的範圍內。這在多層設計中佔據著重要的地位,在多層設計中,利用超類型功能通常是必要需求,而不僅僅是隨意決定。通常,理解該模式背後實用性的最有效方法是通過一些實踐示例。因此,假設我們需要從頭開始構建一個簡單的領域模型,負責定義一些博客文章及其相應評論之間的一些基本交互。粗略地說,該模型可以輕鬆地概述為一個貧血層,其中只包含幾個骨架類,用於建模文章和評論。第一個領域類及其契約可能如下所示:

<code class="language-php"><?php namespace Model;

interface PostInterface
{
    public function setId($id);
    public function getId();

    public function setTitle($title);
    public function getTitle();

    public function setContent($content);
    public function getContent();

    public function setComment(CommentInterface $comment);
    public function setComments(array $comments);
    public function getComments();
}</code>
<code class="language-php"><?php namespace Model;

class Post implements PostInterface
{
    protected $id;
    protected $title;
    protected $content;
    protected $comments = array();

    public function __construct($title, $content, array $comments = array()) {
        $this->setTitle($title);
        $this->setContent($content);
        if (!empty($comments)) {
           $this->setComments($comments); 
        }
    }

    public function setId($id) {
        if ($this->id !== null) {
            throw new BadMethodCallException(
                "The ID for this post has been set already.");
        }
        if (!is_int($id) || $id             throw new InvalidArgumentException(
                "The post ID is invalid.");
        }
        $this->id = $id;
        return $this;
    }

    public function getId() {
        return $this->id;
    }

    public function setTitle($title) {
        if (!is_string($title) 
            || strlen($title)             || strlen($title) > 100) {
            throw new InvalidArgumentException(
                "The post title is invalid.");
        }
        $this->title = htmlspecialchars(trim($title),
            ENT_QUOTES);
        return $this;
    }

    public function getTitle() {
        return $this->title;
    }

    public function setContent($content) {
        if (!is_string($content) || strlen($content)             throw new InvalidArgumentException(
                "The post content is invalid.");
        }
        $this->content = htmlspecialchars(trim($content),
            ENT_QUOTES);
        return $this;
    }

    public function getContent() {
        return $this->content;
    }

    public function setComment(CommentInterface $comment) {
        $this->comments[] = $comment;
        return $this;
    }

    public function setComments(array $comments) {
        foreach ($comments as $comment) {
            $this->setComment($comment);
        }
        return $this;
    }

    public function getComments() {
        return $this->comments;
    }
}</code>

Post 類的驅動是簡單的邏輯,歸結為定義一些基本帖子條目的數據和行為。它應該很容易理解。現在讓我們通過向其中添加一個類來使模型稍微胖一些,該類生成與特定博客條目關聯的評論。它的契約和實現如下所示:

<code class="language-php"><?php namespace Model;

interface CommentInterface
{
    public function setId($id);
    public function getId();

    public function setContent($content);
    public function getContent();

    public function setAuthor($author);
    public function getAuthor();
}</code>
<code class="language-php"><?php namespace Model;

class Comment implements CommentInterface
{
    protected $id;
    protected $content;
    protected $author;

    public function __construct($content, $author) {
        $this->setContent($content);
        $this->setAuthor($author);
    }

    public function setId($id) {
        if ($this->id !== null) {
            throw new BadMethodCallException(
                "The ID for this comment has been set already.");
        }
        if (!is_int($id) || $id             throw new InvalidArgumentException(
                "The comment ID is invalid.");
        }
        $this->id = $id;
        return $this;
    }

    public function getId() {
        return $this->id;
    }

    public function setContent($content) {
        if (!is_string($content) || strlen($content)             throw new InvalidArgumentException(
                "The content of the comment is invalid.");
        }
        $this->content = htmlspecialchars(trim($content),
            ENT_QUOTES);
        return $this;
    }

    public function getContent() {
        return $this->content;
    }

    public function setAuthor($author) {
        if (!is_string($author) || strlen($author)             throw new InvalidArgumentException(
                "The author is invalid.");
        }
        $this->author = $author;
        return $this;
    }

    public function getAuthor() {
        return $this->author;
    }
}</code>

與 Post 一樣,Comment 類也很簡單。但是現在有了這兩個類,我們可以使用該模型。例如:

<code class="language-php"><?php use LibraryLoaderAutoloader,   
    ModelPost,
    ModelComment;

require_once __DIR__ . "/Library/Loader/Autoloader.php";
$autoloader = new Autoloader;
$autoloader->register();

$post = new Post(
    "A sample post.",
    "This is the content of the post."
);

$post->setComments(array(
    new Comment(
        "One banal comment for the previous post.",
        "A fictional commenter"),
    new Comment(
        "Yet another banal comment for the previous post.",
        "A fictional commenter")
));

echo $post->getTitle() . " " . $post->getContent() . "<br>";

foreach ($post->getComments() as $comment) {
    echo $comment->getContent() . " " . $comment->getAuthor() .
        "<br>";
}</code>

這確實像魅力一樣有效!使用該模型是一個相當簡單的過程,需要您首先創建一些 Post 對象,然後使用相關的評論對其進行填充。是的,生活甜蜜美好。好吧,到目前為止是這樣,但情況肯定可以更好!我不是想破壞如此美好的時刻的魔力,但我必須承認,每次看到 Post 和 Comment 類的實現時,我都會感到一陣輕微的寒意。雖然這本身並不是一個嚴重的問題,但某些方法(例如 setId() 和 setContent())表現出代碼重複的典型症狀。由於一些邏輯問題,在不粗心大意的情況下解決這個問題並不像乍一看那樣直觀。首先,儘管它們彼此之間存在語義關係,但每個類實際上都對不同類型的對象進行建模。其次,它們實現不同的接口,這意味著很難抽像出邏輯,而不會最終得到一個笨拙的層次結構,其中“IS-A”條件永遠不成立。特別是在這種情況下,我們可以採取更寬鬆的方法,並將 Post 和 Comment 視為高度通用的 AbstractEntity 超類型的子類型。這樣做,將共享實現放在抽像類的邊界內會非常簡單,因此使子類型的定義更加精簡。由於整個抽象過程只在領域層進行,因此假設的 AbstractEntity 將被視為……是的,您猜對了,一個層超類型。簡單但不錯,對吧?

(由於篇幅限制,此處省略了剩餘代碼和解釋。 請注意,原文的代碼示例很長,翻譯和概括所有代碼會使答案過於冗長。 核心思想是通過創建AbstractEntity 超類來提取PostComment 類中重複的代碼,從而減少代碼冗餘並提高可維護性。)

總結

儘管繼承通常被認為是過高估計和濫用的機制,但我希望現在很少有人會不同意,繼承是一種強大的機制,當在多層系統中巧妙地使用時,它可以有效地防止代碼重複。使用像層超類型這樣的簡單模式是繼承在創建彼此共享大量樣板實現的子類型時提供的眾多引人入勝的優點的一個例子。

(此處也省略了原文的FAQ 部分,因為其內容是對文章核心思想的重複和擴展,翻譯全部內容會使答案過於冗長。 核心思想已在以上翻譯中充分體現。)

以上是圖層超級類型模式:將共同實現封裝在多層系統中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn