在软件中使用的设计模式的实际例子有哪些?
目前,我正在阅读首批设计模式,尽管这本书非常出色,但我也想看看它们在现实世界中是如何实际使用的。
如果我们知道设计模式用法的一个很好的例子(最好是在OSS程序中,所以我们可以看一下:),那么请在下面列出。
解决方案
回答
对我来说,观察者模式的一个啊哈时刻是要意识到它与事件之间的紧密联系。考虑一个Windows程序,该程序需要实现两种形式之间的宽松通信。这可以通过观察者模式轻松实现。
下面的代码显示Form2如何触发事件,注册为观察者的任何其他类都将获取其数据。
请参阅此链接以获取出色的模式资源:
http://sourcemaking.com/design-patterns-and-tips
Form1的代码:
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; } } }
回答
我使用被动视图,这是Model View Presenter模式的一种形式,它与任何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
后面的代码充当零逻辑的非常薄的视图。相反,此逻辑被推送到可以进行单元测试的presenter类中。
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具有Iterator模式的标准实现。在Cand Python中,该语言已经集成到该语言中,因此我们可以只使用yield return语句。
回答
复合在UI中广泛使用。组件可以是叶子组件,例如按钮和标签或者复合材料面板,可以包含其他叶子或者复合组件。从客户端的角度来看,所有组件都被视为相同,这大大简化了客户端代码。
回答
模板模式通常用于实现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/
回答
正如Head First设计模式中所指出的那样,也许一个很好的例子是实现Observer模式的JAVA Swing API。更具体地说,JButton(或者超类AbstractButton)是Observable类,并且提供添加和删除"观察者"或者"侦听器"的方法,这些方法在Swing中被调用。