Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 结构设计模式和行为设计模式的区别?_Design Patterns - Fatal编程技术网

Design patterns 结构设计模式和行为设计模式的区别?

Design patterns 结构设计模式和行为设计模式的区别?,design-patterns,Design Patterns,我在阅读设计模式基础知识时,遇到了结构模式和行为模式的两个基本定义,如下所示: 结构设计模式:通常处理实体之间的关系,使这些实体更容易协同工作。 行为设计模式:用于实体之间的通信,使这些实体的通信更容易、更灵活。 读了这本书,我无法区分它们,有人能给我一些最简单的例子来说明它们的不同吗?我很不确定我的解释和例子是否真的涵盖了最重要的原则 将行为视为结构外部的场景。某个数据结构可以在多个行为/场景中“使用” 另一方面,将结构相关逻辑视为结构的内部逻辑。结构会受到各种更改的影响,因此会执行一些操作

我在阅读设计模式基础知识时,遇到了结构模式和行为模式的两个基本定义,如下所示:

结构设计模式:通常处理实体之间的关系,使这些实体更容易协同工作。

行为设计模式:用于实体之间的通信,使这些实体的通信更容易、更灵活。

读了这本书,我无法区分它们,有人能给我一些最简单的例子来说明它们的不同吗?

我很不确定我的解释和例子是否真的涵盖了最重要的原则
将行为视为结构外部的场景。某个数据结构可以在多个行为/场景中“使用”


另一方面,将结构相关逻辑视为结构的内部逻辑。结构会受到各种更改的影响,因此会执行一些操作

尽管如此,我们可以用以下例子来说明:

结构设计模式将通过将其组成部分定义为更高级别的业务对象(如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
行为设计模式将通过使用较低级别的业务对象(如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();

TL;博士
如果存储对象是智能的(包含逻辑),那就是结构设计。如果存储对象是哑的(它们只能存储数据并将数据传输到数据库),则需要行为设计来管理它们。

结构模式用于定义系统的静态属性(请参阅)

示例:工厂模式可用于创建构成系统的实体。您可以让对象
按钮
在Windows和OS X上具有不同的图形属性。工厂模式将创建
按钮
,而不管操作系统是什么,并且创建的对象在两个操作系统上都具有相同的界面,尽管内部结构不同,但暴露出相同的行为

行为模式用于定义系统的动态行为(参见图表等)


示例:可以在运行时使用适配器模式透明地允许两个实体之间不共享接口的接口。它有效地改变了运行时对象的行为。

最好的解释方法是从两个类别中选取两个例子

来自结构模式的复合定义了树状结构,因此重点关注关系。一对多,有一种关系,所以整体和部分可以被同等对待

另一方面,观察者模式从行为设计模式着眼于交流。我们如何让相关方知道对象的任何更改。从发布者到订阅者的排序。不定义严格的结构,但强制实施方法,即通信渠道


希望有用。

很抱歉,我的解释会用C#

观察者是一种行为模式:呈现界面,允许对象在没有任何关于彼此的具体知识的情况下进行通信。也称为发布-订阅模式。对象通知其他对象其状态,而不知道这些对象是什么

适配器是结构模式:适配器将给定类的接口转换为客户端请求的另一个类。使用新接口包装现有类。阻抗将旧部件与新系统匹配。当由于接口不兼容而无法协同工作时,允许类协同工作

适配器示例:

interface ITarget
{
  List<string> GetProducts();
}


public class VendorAdaptee
{
   public List<string> GetListOfProducts()
   {
      List<string> products = new List<string>();
      products.Add("Gaming Consoles");
      products.Add("Television");
      products.Add("Books");
      products.Add("Musical Instruments");
      return products;
   }
}


class VendorAdapter:ITarget
{
   public List<string> GetProducts()
   {
      VendorAdaptee adaptee = new VendorAdaptee();
      return adaptee.GetListOfProducts();
   }
}


class ShoppingPortalClient
{
   static void Main(string[] args)
   {
      ITarget adapter = new  VendorAdapter();
      foreach (string product in adapter.GetProducts())
      {
        Console.WriteLine(product);
      }
      Console.ReadLine();
   }
}

将行为视为结构外部的场景。某个数据结构可以在多个行为/场景中“使用”。另一方面,将结构相关逻辑视为结构的内部逻辑。结构会受到各种更改的影响,并因此执行一些操作。需要详细说明此解释…仍然不理解不同的工厂模式是创作模式不是结构模式也适配器是结构模式不是行为模式适配器模式是结构模式而不是行为。
using System;
namespace wildert
{
    public class Metronome
    {
        public event TickHandler Tick;
        public EventArgs e = null;
        public delegate void TickHandler(Metronome m, EventArgs e);
        public void Start()
        {
            while (true)
            {
                System.Threading.Thread.Sleep(3000);
                if (Tick != null)
                {
                    Tick(this, e);
                }
            }
        }
    }
        public class Listener
        {
            public void Subscribe(Metronome m)
            {
                m.Tick += new Metronome.TickHandler(HeardIt);
            }
            private void HeardIt(Metronome m, EventArgs e)
            {
                System.Console.WriteLine("HEARD IT");
            }

        }
    class Test
    {
        static void Main()
        {
            Metronome m = new Metronome();
            Listener l = new Listener();
            l.Subscribe(m);
            m.Start();
        }
    }
}