我正在阅读设计模式的基础知识,并且遇到了结构模式和行为模式的两个基本定义,如:
结构设计模式:通常处理实体之间的关系,使这些实体更容易协同工作.
行为设计模式:用于实体,并使其更容易,更灵活的对这些实体进行通信之间的通信.
阅读它,我无法区分它们,有人可以通过给出一些最简单的例子来指导我们它们的不同之处吗?
xel*_*ber 19
解释的最佳方式是从两个类别中取两个例子.
结构模式的复合定义了树状结构,因此关注于关系.一对多,并且具有一种关系,以便整体和部分可以被相同地对待.
另一方面,观察者模式来自行为设计模式,侧重于沟通.我们如何让感兴趣的各方知道对象的任何变化.发布者到订阅者的排序.没有定义严格的结构,而是强制实施方法,即通信渠道.
希望它有用.
将行为视为结构外部的场景.可以在多个行为/场景中"使用"某种数据结构.
另一方面,将结构相关逻辑视为结构内部.结构受到各种变化的影响,并因此执行一些操作.
话虽如此,我们可以举例说明如下:
结构设计模式将通过将其组成部分定义为更高级别的业务对象(如Article/Image/Comment)来定义Weblog.选民们彼此了解以及如何相互联系.
$image = new Image;
$image->title = 'Image title';
$image->url = 'http://example.com/file.ext';
$image->save(); // will save the image to a DB
$article->title = 'The title i have set';
/* $article->url_key = 'the_title_i_have_set'; */
// this ^^ element of logic will be done automatically by the article
$article->addImage($image); // will save the connection between the
// article and the image to DB
Run Code Online (Sandbox Code Playgroud)
行为设计模式将使用较低级别的业务对象(例如Article/ArticleToImage/Image/ArticleToComment)通过其用例(场景)定义Weblog.业务对象彼此不了解并且由场景逻辑"操纵"到位.
$image = new Image;
$image->title = 'Image title';
$image->url = 'http://example.com/file.ext';
$image->save(); // will save the image to a DB
$article->title = 'The title i have set';
$article->url_key = $controller->getURlKey($article->title);
$article->save(); // saves article to DB
$article_to_image = new ArticleToImage;
$article_to_image->article = $article;
$article_to_image->image = $image;
$article_to_image->save();
Run Code Online (Sandbox Code Playgroud)
如果存储对象是智能的(包含逻辑)结构设计.如果存储对象很笨(它们只能存储数据并将其传输到数据库),那么您需要一个行为设计来管理它们.