我目前正在阅读头条设计模式,虽然这本书很棒,但我也想看看它们在现实世界中是如何实际使用的。

如果您知道设计模式使用的一个很好的示例(最好是在 OSS 程序中,这样我们就可以看看:),那么请在下面列出它。

有帮助吗?

解决方案

对我来说,观察者模式的一个令人惊叹的时刻是意识到它与事件的关联有多么紧密。考虑一个需要在两种窗体之间实现松散通信的 Windows 程序。这可以通过观察者模式轻松实现。

下面的代码显示了 Form2 如何触发事件以及注册为观察者的任何其他类如何获取其数据。

请参阅此链接以获取出色的模式资源:http://sourcemaking.com/design-patterns-and-tips

表格1的代码:

namespace PublishSubscribe
{
    public partial class Form1 : Form
    {
        Form2 f2 = new Form2();

        public Form1()
        {
            InitializeComponent();

            f2.PublishData += new PublishDataEventHander( DataReceived );
            f2.Show();
        }

        private void DataReceived( object sender, Form2EventArgs e )
        {
            MessageBox.Show( e.OtherData );            
        }
    }
}

Form2的代码

namespace PublishSubscribe
{

    public delegate void PublishDataEventHander( object sender, Form2EventArgs e );

    public partial class Form2 : Form
    {
        public event PublishDataEventHander PublishData;

        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click( object sender, EventArgs e )
        {
            PublishData( this, new Form2EventArgs( "data from form2" ) );    
        }
    }

    public class Form2EventArgs : System.EventArgs
    {
        public string OtherData;

        public Form2EventArgs( string OtherData )        
        {
            this.OtherData = OtherData;
        }
    }
}

其他提示

我使用被动的观点,一种 模型视图演示者 模式,使用任何 Web 表单(例如开发 (.NET))来提高可测试性/可维护性等

例如,您的代码隐藏文件可能如下所示

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Implements IProductView

    Private presenter As ProductPresenter

    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        MyBase.OnInit(e)
        presenter = New ProductPresenter(Me)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        presenter.OnViewLoad()
    End Sub

    Private ReadOnly Property PageIsPostBack() As Boolean Implements IProductView.PageIsPostBack
        Get
            Return Page.IsPostBack
        End Get
    End Property

    Public Property Products() As System.Collections.Generic.List(Of Product) Implements Library.IProductView.Products
        Get
            Return DirectCast(gridProducts.DataSource(), List(Of Product))
        End Get
        Set(ByVal value As System.Collections.Generic.List(Of Product))
            gridProducts.DataSource = value
            gridProducts.DataBind()
        End Set
    End Property
End Class

这段代码背后充当了一个非常薄的视图,具有零逻辑。相反,该逻辑被推送到可以进行单元测试的演示者类中。

Public Class ProductPresenter
    Private mView As IProductView
    Private mProductService As IProductService

    Public Sub New(ByVal View As IProductView)
        Me.New(View, New ProductService())
    End Sub

    Public Sub New(ByVal View As IProductView, ByVal ProductService As IProductService)
        mView = View
        mProductService = ProductService
    End Sub

    Public Sub OnViewLoad()
        If mView.PageIsPostBack = False Then
            PopulateProductsList()
        End If
    End Sub

    Public Sub PopulateProductsList()
        Try
            Dim ProductList As List(Of Product) = mProductService.GetProducts()
            mView.Products = ProductList
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class

使用 code.google.com

例如 搜索结果 对于“工厂”将为您带来很多实施工厂模式的案例。

责任链 模式是在 DOM 事件的处理中实现的。例如,(稍微简化一下)当单击一个元素时,该元素将首先获得处理事件的机会,然后是每个祖先,直到顶层 文档 达到或其中之一明确停止事件进一步“冒泡”。

C#、Java 和 Python 都有迭代器模式的标准实现。在 C# 和 Python 中,这已集成到语言中,因此您可以只使用yield return 语句。

模板模式通常用于 dotnet 事件的实现中,以设置前置条件并响应后置条件。退化的情况是

void FireMyEvent(object sender, EventArgs e) 
{
  if (_myevent != null) _myEvent(sender, e);
}

其中检查前提条件。在这种情况下,前提条件是只有在至少绑定了一个处理程序时才能调用处理程序。(请不要告诉我应该异步调用处理程序。我知道。我正在说明模板模式,而不是异步编程技术。)

更详细的前提条件可能涉及检查控制事件触发的属性。

模板模式也常用于实现钩子,例如

public virtual void BeforeOpenFile(string filepath)
{
  //stub
}
public virtual void AfterOpenFile(string filepath)
{
  //stub
}
public sealed void OpenFile(string filepath) 
{
  BeforeOpenFile(filepath); //do user customisable pre-open bits
  //do standard bits here
  AfterOpenFile(filepath); //do user customisable post-open bits
}

如果您熟悉 Python,请查看 Twisted 框架。http://twistedmatrix.com/trac/

也许是一个很好的例子,正如 首先设计模式 也是 JAVA 摆动 API 它实现了 观察者 图案。更具体地说,JButton(或超类 AbstractButton)是 Observable 类,并提供添加和删除“观察者”或“监听器”(在 Swing 中称为“监听器”)的方法。

复合材料在 UI 中被广泛使用。组件可以是叶组件,例如按钮和标签或复合材料,例如面板,可以包含其他叶子或复合组件。从客户端的角度来看,所有组件都被同等对待,这大大简化了客户端代码。

命令模式用于任何具有撤消功能的地方。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top