问题描述
我正在阅读设计模式的基础知识,并遇到了结构模式和行为模式的两个基本定义,例如:
I was reading on the design patterns basics and came across the two basic definition of Structural Patterns and Behavioral Pattern which is like:
结构设计模式:通常处理实体之间的关系,使这些实体更容易协同工作。
行为设计模式:用于实体之间的通信和make
阅读它,我无法区分它们,有人可以通过给出一些最简单的示例?
reading it, I cannot differentiate between them, can someone guide me how they are different, by giving some simplest example?
推荐答案
我不确定我的解释和示例是否真的涵盖了最重要的原理
将行为视为结构外部的场景。可以在多种行为/场景中使用某种数据结构。
I am pretty unsure if my explanation and example really cover the most important principles
Think of behavior as scenarios external to the structures. A certain data structure can "be used" in multiple behaviors/scenarios.
另一方面,将与结构相关的逻辑视为结构内部的逻辑。结构受到各种更改的影响,并因此执行一些操作。
On the other hand think of structure related logic as being internal to the structure. The structure gets affected by various changes and executes some actions as a consequence.
话虽如此,我们可以举例说明以下内容:
That being said we can exemplify with the following:
结构设计模式将通过将Weblog的组成部分定义为更高级别的业务对象(如Article / Image / Comment)来定义Weblog。三方成员知道彼此之间以及如何相互联系。
Structure design patterns will define a weblog by defining its constituents as higher level business objects such as Article/Image/Comment. The constituents are aware of one another and how to connect to each other.
$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
行为设计模式将使用低级业务对象(例如Article / ArticleToImage / Image / ArticleToComment)通过用例(场景)来定义Weblog。业务对象彼此之间不了解,而是被场景逻辑操纵到位。
Behavior design patterns will define a weblog by its use cases (scenarios) using lower level business objects such as Article/ArticleToImage/Image/ArticleToComment. The business objects are not aware of each other and are "maneuvered" into place by the scenario logic.
$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();
TL; DR
如果存储对象是智能的(包含逻辑),则这是结构设计。如果存储对象很笨拙(它们只能存储数据并将其传输到DB),则需要进行行为设计来管理它们。
TL;DR
If the storage objects are smart (contain logic) that's structural design. If the storage objects are dumb (they can only store data and transfer it to the DB) you then need a behavioral design to manage them.
这篇关于结构设计模式和行为设计模式之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!