Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some real life examples of Design Patterns used in software [closed]

I'm reading through head first design patterns at the moment and while the book is excellent I also would like to see how these are actually used in the real world.

If you know of a good example of design pattern usage (preferably in a OSS program so we can have a look :) then please list it below.

like image 374
Almond Avatar asked Aug 30 '08 16:08

Almond


People also ask

What is an example of a design pattern?

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.

What is factory design pattern with real time example?

The factory design pattern says that define an interface ( A java interface or an abstract class) for creating object and let the subclasses decide which class to instantiate. The factory method in the interface lets a class defers the instantiation to one or more concrete subclasses.

What are the design patterns in software engineering?

In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code.

Which design pattern is most useful in software architecture?

One of the most popular design patterns used by software developers is a factory method. It is a creational pattern that helps create an object without the user getting exposed to creational logic. The only problem with a factory method is it relies on the concrete component.


3 Answers

An ah-ha moment for me for the observer pattern was to realize how closely associated it is with events. Consider a Windows program that needs to acheive loosely communications between two forms. That can easily be accomplished with the observer pattern.

The code below shows how Form2 fires an event and any other class registered as an observer get its data.

See this link for a great patterns resource: http://sourcemaking.com/design-patterns-and-tips

Form1's code:

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's code

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;
        }
    }
}
like image 81
rp. Avatar answered Oct 04 '22 12:10

rp.


I use passive view, a flavor of the Model View Presenter pattern, with any web forms like development (.NET) to increase testability/maintainability/etc

For example, your code-behind file might look something like this

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

This code behind is acting as a very thin view with zero logic. This logic is instead pushed into a presenter class that can be unit tested.

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
like image 42
Toran Billups Avatar answered Oct 04 '22 13:10

Toran Billups


Use code.google.com

For example the search result for "Factory" will get you a lot of cases where the factory Pattern is implemented.

like image 20
Maximilian Avatar answered Oct 04 '22 13:10

Maximilian