我正在阅读设计模式基础知识,并遇到了结构模式和行为模式的两个基本定义,例如:
结构设计模式:一般处理实体之间的关系,使这些实体更容易协同工作。
行为设计模式:用于实体之间的通信,使这些实体之间的通信更容易、更灵活。
阅读它,我无法区分它们,有人可以通过给出一些最简单的例子来指导我它们的不同之处吗?
我正在阅读设计模式基础知识,并遇到了结构模式和行为模式的两个基本定义,例如:
结构设计模式:一般处理实体之间的关系,使这些实体更容易协同工作。
行为设计模式:用于实体之间的通信,使这些实体之间的通信更容易、更灵活。
阅读它,我无法区分它们,有人可以通过给出一些最简单的例子来指导我它们的不同之处吗?
最好的解释方法是从两个类别中举两个例子。
Composite from Structural patterns 定义了一个树状结构,所以关注关系。一对多,并具有一种关系,因此整体和部分可以被同等对待。
另一方面,行为设计模式的观察者模式专注于沟通。我们如何让相关方知道对象的任何更改。发布者到订阅者的排序。没有定义严格的结构,而是强制实施方法,即沟通渠道。
希望它有用。
将行为视为结构外部的场景。可以在多种行为/场景中“使用”某种数据结构。
另一方面,将结构相关的逻辑视为结构的内部。该结构受到各种变化的影响,并因此执行一些操作。
话虽如此,我们可以举例说明:
结构设计模式将通过将其组成部分定义为更高级别的业务对象(例如文章/图像/评论)来定义博客。三方成员都知道彼此以及如何相互联系。
$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)的用例(场景)来定义博客。业务对象不知道彼此,而是由场景逻辑“操纵”到位。
$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();
如果存储对象是智能的(包含逻辑),那就是结构设计。如果存储对象是愚蠢的(它们只能存储数据并将其传输到数据库),那么您需要一个行为设计来管理它们。
对不起,我的解释将在 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();
        }
    }
}