<p>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.</p>

<p>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.</p>

[{'answer_id': 36167, 'author': 'rp.', 'author_id': 2536, 'author_profile': 'https://Stackoverflow.com/users/2536', 'pm_score': 3, 'selected': True, 'text': '<p>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.</p>\n\n<p>The code below shows how Form2 fires an event and any other class registered as an observer get its data. </p>\n\n<p>See this link for a great patterns resource:\n<a href="http://sourcemaking.com/design-patterns-and-tips" rel="nofollow noreferrer">http://sourcemaking.com/design-patterns-and-tips</a></p>\n\n<p>Form1\'s code:</p>\n\n<pre><code>namespace PublishSubscribe\n{\n    public partial class Form1 : Form\n    {\n        Form2 f2 = new Form2();\n\n        public Form1()\n        {\n            InitializeComponent();\n\n            f2.PublishData += new PublishDataEventHander( DataReceived );\n            f2.Show();\n        }\n\n        private void DataReceived( object sender, Form2EventArgs e )\n        {\n            MessageBox.Show( e.OtherData );            \n        }\n    }\n}\n</code></pre>\n\n<p>Form2\'s code</p>\n\n<pre><code>namespace PublishSubscribe\n{\n\n    public delegate void PublishDataEventHander( object sender, Form2EventArgs e );\n\n    public partial class Form2 : Form\n    {\n        public event PublishDataEventHander PublishData;\n\n        public Form2()\n        {\n            InitializeComponent();\n        }\n\n        private void button1_Click( object sender, EventArgs e )\n        {\n            PublishData( this, new Form2EventArgs( "data from form2" ) );    \n        }\n    }\n\n    public class Form2EventArgs : System.EventArgs\n    {\n        public string OtherData;\n\n        public Form2EventArgs( string OtherData )        \n        {\n            this.OtherData = OtherData;\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 36194, 'author': 'Toran Billups', 'author_id': 2701, 'author_profile': 'https://Stackoverflow.com/users/2701', 'pm_score': 2, 'selected': False, 'text': '<p>I use passive view, a flavor of the <a href="http://msdn.microsoft.com/en-us/magazine/cc188690.aspx" rel="nofollow noreferrer">Model View Presenter</a> pattern, with any web forms like development (.NET) to increase testability/maintainability/etc</p>\n\n<p>For example, your code-behind file might look something like this</p>\n\n<pre><code>Partial Public Class _Default\n    Inherits System.Web.UI.Page\n    Implements IProductView\n\n    Private presenter As ProductPresenter\n\n    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)\n        MyBase.OnInit(e)\n        presenter = New ProductPresenter(Me)\n    End Sub\n\n    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load\n        presenter.OnViewLoad()\n    End Sub\n\n    Private ReadOnly Property PageIsPostBack() As Boolean Implements IProductView.PageIsPostBack\n        Get\n            Return Page.IsPostBack\n        End Get\n    End Property\n\n    Public Property Products() As System.Collections.Generic.List(Of Product) Implements Library.IProductView.Products\n        Get\n            Return DirectCast(gridProducts.DataSource(), List(Of Product))\n        End Get\n        Set(ByVal value As System.Collections.Generic.List(Of Product))\n            gridProducts.DataSource = value\n            gridProducts.DataBind()\n        End Set\n    End Property\nEnd Class\n</code></pre>\n\n<p>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.</p>\n\n<pre><code>Public Class ProductPresenter\n    Private mView As IProductView\n    Private mProductService As IProductService\n\n    Public Sub New(ByVal View As IProductView)\n        Me.New(View, New ProductService())\n    End Sub\n\n    Public Sub New(ByVal View As IProductView, ByVal ProductService As IProductService)\n        mView = View\n        mProductService = ProductService\n    End Sub\n\n    Public Sub OnViewLoad()\n        If mView.PageIsPostBack = False Then\n            PopulateProductsList()\n        End If\n    End Sub\n\n    Public Sub PopulateProductsList()\n        Try\n            Dim ProductList As List(Of Product) = mProductService.GetProducts()\n            mView.Products = ProductList\n        Catch ex As Exception\n            Throw ex\n        End Try\n    End Sub\nEnd Class\n</code></pre>\n'}, {'answer_id': 36287, 'author': 'Maximilian', 'author_id': 1733, 'author_profile': 'https://Stackoverflow.com/users/1733', 'pm_score': 2, 'selected': False, 'text': '<p>Use code.google.com</p>\n\n<p>For example the <a href="http://code.google.com/search/#q=Factory" rel="nofollow noreferrer">search result</a> for "Factory" will get you a lot of cases where the factory Pattern is implemented.</p>\n'}, {'answer_id': 36307, 'author': 'Nick Higgs', 'author_id': 3187, 'author_profile': 'https://Stackoverflow.com/users/3187', 'pm_score': 2, 'selected': False, 'text': '<p>The <strong>Chain of Responsibility</strong> pattern is implemented in the handling of DOM events. For example, (and simplifying slightly) when an element is clicked on, that element gets the first opportunity to handle the event, and then each ancestor in tern until the top level <strong>document</strong> is reached or one of them explicitly stops the event "bubbling" any further.</p>\n'}, {'answer_id': 36526, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 1, 'selected': False, 'text': '<p>C#, Java and Python have a standard implementation of the Iterator pattern. In C# and Python this has been intergrated in the language so you can just use yield return statements.</p>\n'}, {'answer_id': 48270, 'author': 'jase', 'author_id': 4216, 'author_profile': 'https://Stackoverflow.com/users/4216', 'pm_score': 0, 'selected': False, 'text': '<p>Composite is used extensively in UI. Components can be leaf components e.g. buttons and labels or composites e.g. panels, that can contain other leaf or composite components. From the point of view of the client, all components are treated the same, which greatly simplifies the client code.</p>\n'}, {'answer_id': 113353, 'author': 'Peter Wone', 'author_id': 1715673, 'author_profile': 'https://Stackoverflow.com/users/1715673', 'pm_score': 1, 'selected': False, 'text': "<p>Template pattern is commonly used in the implementation of dotnet events to set up preconditions and respond to postconditions. The degenerate case is </p>\n\n<pre><code>void FireMyEvent(object sender, EventArgs e) \n{\n  if (_myevent != null) _myEvent(sender, e);\n}\n</code></pre>\n\n<p>in which the precondition is checked. In this case the precondition is that handlers can be invoked only when at least one has been bound. (Please don't tell me I should invoke the handlers asynchronously. I know that. I am illustrating Template pattern, not asynchronous programming technique.)</p>\n\n<p>A more elaborate precondition might involve checking a property that governs the firing of events. </p>\n\n<p>Template pattern is also commonly used to implement hooks, for example </p>\n\n<pre><code>public virtual void BeforeOpenFile(string filepath)\n{\n  //stub\n}\npublic virtual void AfterOpenFile(string filepath)\n{\n  //stub\n}\npublic sealed void OpenFile(string filepath) \n{\n  BeforeOpenFile(filepath); //do user customisable pre-open bits\n  //do standard bits here\n  AfterOpenFile(filepath); //do user customisable post-open bits\n}\n</code></pre>\n"}, {'answer_id': 114464, 'author': 'Martijn', 'author_id': 17439, 'author_profile': 'https://Stackoverflow.com/users/17439', 'pm_score': 0, 'selected': False, 'text': '<p>The Command pattern is used everywhere you have Undo functionality.</p>\n'}, {'answer_id': 114491, 'author': 'Vasil', 'author_id': 7883, 'author_profile': 'https://Stackoverflow.com/users/7883', 'pm_score': 1, 'selected': False, 'text': '<p>If you\'re familiar with Python, check out the Twisted framework.\n<a href="http://twistedmatrix.com/trac/" rel="nofollow noreferrer">http://twistedmatrix.com/trac/</a></p>\n'}, {'answer_id': 1418821, 'author': 'Jahanzeb Farooq', 'author_id': 123250, 'author_profile': 'https://Stackoverflow.com/users/123250', 'pm_score': 1, 'selected': False, 'text': '<p>Perhaps a good example, as pointed out in the <a href="https://rads.stackoverflow.com/amzn/click/com/0596007124" rel="nofollow noreferrer" rel="nofollow noreferrer">Head First Design Patterns</a> too, is the <strong>JAVA Swing API</strong> which implements the <strong>Observer</strong> pattern. More specifically, the JButton (or the superclass AbstractButton) is the Observable class and provides methods to add and remove "Observers", or "Listeners" as they are called in Swing. </p>\n'}]
['https://Stackoverflow.com/questions/36129', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1603/']