コアポイント
レイヤースーパータイプは、特定のレイヤーの範囲内に存在することを除いて、「共通」ベースタイプの自然で選択的な進化であると言えます。これは、任意の決定だけでなく、スーパータイプの機能を利用することが必要な要件であることが多いマルチレイヤー設計で重要な役割を果たします。一般に、このパターンの背後にある実用性を理解する最も効果的な方法は、いくつかの実用的な例です。したがって、いくつかのブログ投稿とそれに対応するコメントの間にいくつかの基本的な相互作用を定義することを担当するゼロから単純なドメインモデルを構築する必要があると仮定します。大まかに言えば、このモデルは、記事やコメントをモデル化するためのいくつかのスケルトンクラスのみを含む貧血の層として簡単に概説できます。最初のドメインクラスとその契約は次のようになる場合があります: ポストクラスのドライバーは単純なロジックであり、いくつかの基本的なポストエントリのデータと動作の定義に要約されます。理解しやすいはずです。次に、特定のブログエントリに関連付けられたコメントを生成するクラスを追加することにより、モデルを少し太くしましょう。その契約と実装は次のとおりです
(残りのコードと説明はスペースの制限のためにここで省略されています。元のテキストのコードの例は長いことに注意してください。すべてのコードを翻訳して一般化すると、答えが冗長になります。 クラスを作成して、 継承は過大評価と虐待のメカニズムと見なされることが多いが、多層システムできちんと使用される場合に機能する強力なメカニズムがコードの重複を防ぐことができる強力なメカニズムであることが今、反対する人はほとんどいないことを願っています。レイヤースーパータイプのような単純なパターンを使用することは、多数のボイラープレートの実装を互いに共有するサブタイプを作成するときに、継承が提供する多くの魅力的な利点の例です。 (元のテキストのFAQ部分もここでは省略されています。コンテンツは記事のコアアイデアの繰り返しと拡張です。すべてのコンテンツを翻訳すると、答えが長くなりすぎます。コアのアイデアはあります。上記の翻訳に完全に反映されています<?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();
}
<?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;
}
}
<?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();
}
<?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;
}
}
<?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></br>";
foreach ($post->getComments() as $comment) {
echo $comment->getContent() . " " . $comment->getAuthor() .
"<br></br>";
}
AbstractEntity
およびPost
クラスで重複したコードを抽出し、コードの冗長性を削減し、保守性を向上させます
Comment
概要
以上がレイヤースーパータイプパターン:マルチ層システムでの一般的な実装のカプセル化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。