locked
S/W Design Patterns RRS feed

  • Question

  • Hi, I want to learn 'Design Patterns' (like FACADE, DECORATOR, STRATEGY, ADAPTER etc..)with coding examples for better illustrations. Can anybody guide me Please!!!

     

     

     


    Nata...
    Tuesday, November 1, 2011 4:10 PM

Answers

All replies

  • Wikipedia is a great reference for this: http://en.wikipedia.org/wiki/Design_pattern_(computer_science)   Most patterns have code (or pseudo-code) examples.

     

    Here's another good site showing design patterns implemented with code samples in .NET: http://www.dofactory.com/Patterns/Patterns.aspx

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Natraj A V Wednesday, November 2, 2011 12:14 PM
    Tuesday, November 1, 2011 4:30 PM
    • Marked as answer by Natraj A V Wednesday, November 2, 2011 12:14 PM
    Wednesday, November 2, 2011 9:37 AM
  • Hi, Reed Copsey

    I am trying to implement "Strategy Pattern"(in Win Forms) & i am getting trouble in working with Rich TextBox.

    The concept is "User has to choose specific method for Sorting or Searching or Both, Specify the Number of Elements & Elements(in Rich TextBox). And the user doesn't know about implementation & expect exact result of his selection".How to Solve this Problem

    (Please refer the follwing code to understand my view)

    Hope U'll Reply...

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    namespace StrategyPattern
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                lblElements.Visible = lblEle.Visible = txtNo.Visible =lblenter.Visible= rtxtEle.Visible= false;
            }   
            
            private void btnReset_Click(object sender, EventArgs e)
            {            
                txtNo.Text = "";
                chkSearch.Checked=chkSort.Checked=rtxtEle.Visible=lblenter.Visible = false;             
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void chkSort_CheckedChanged(object sender, EventArgs e)
            {
                if (chkSort.Checked == true && chkSearch.Checked == true)
                {
                    pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible =lblenter.Visible= rtxtEle.Visible= true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible = pnlSearch.Visible = pnlSort.Visible=rtxtEle.Visible=lblenter.Visible = false;
                }
                if (chkSearch.Checked == false && chkSort.Checked == true)
                {
                    pnlSearch.Visible = false;                
                    pnlSort.Visible=rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == true && chkSort.Checked == false)
                {
                    pnlSearch.Visible=rtxtEle.Visible=lblenter.Visible = true;
                    pnlSort.Visible = false;              
                }
            }
    
            private void chkSearch_CheckedChanged(object sender, EventArgs e)
            {
                if (chkSort.Checked == true && chkSearch.Checked == true)
                {
                    pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible= true;
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible=rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible = pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible= false;
                }
                if (chkSearch.Checked == true && chkSort.Checked == false)
                {
                    pnlSort.Visible = false;
                    pnlSearch.Visible = rtxtEle.Visible =lblenter.Visible = true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == true)
                {
                    pnlSearch.Visible = false;
                    pnlSort.Visible = rtxtEle.Visible=lblenter.Visible = true;
                }
            } 
    
            private void btnClickMe_Click(object sender, EventArgs e)
            {
                #region Validation
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    MessageBox.Show("Please Select atleast one Option","User Error");
                }
                Regex rx = new Regex("[^0-9]+");
                if (rx.IsMatch(txtNo.Text))
                {          
                    MessageBox.Show("Enter Correct Number (>0)", "User Error");
                    txtNo.Clear();
                    txtNo.Focus();  
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    if (string.IsNullOrEmpty(txtNo.Text)|| string.IsNullOrEmpty(rtxtEle.Text))
                    {
                        MessageBox.Show("Enter Number", "User Error");
                        txtNo.Focus();
                    }
                }            
                #endregion                                               
                //Here i am getting trouble to get the i/p and passing appropriate objects usinf STRATEGY PATTERN
                //Sorting & Searching Algorithms can be implemented in derived classes
    
                //I Hope I'll get out of this Problem
            }
    
            abstract class SortStrategy
            {           
                public abstract void Sort();
            }
    
            abstract class SearchStrategy
            {
                public abstract void Search();
            }
    
            class QuickSort : SortStrategy
            {
                public override void Sort()
                {                
                    MessageBox.Show("QuickSort Implemented", "Operation Success");
                }
            }
    
            class BubbleSort : SortStrategy
            {
                public override void Sort()
                {               
                   MessageBox.Show("BubbleSort Implemented", "Operation Success");
                }    
            }
            class MergeSort : SortStrategy
            {
                public override void Sort()
                {                
                    MessageBox.Show("Merge Sort Implemented", "Operation Success");
                } 
            }   
            class SeqSearch : SearchStrategy
            {
                public override void Search()
                {
                    MessageBox.Show("Sequential Search Implemented", "Operation Success");
                }
            }
            class BinSearch : SearchStrategy
            {
                public override void Search()
                {
                    MessageBox.Show("Binary Search Implemented", "Operation Success");
                }
            }                                   
        }
    }
    
    



    Nata...
    Monday, November 14, 2011 10:46 AM
  • Hi, Mishra

    I am trying to implement "Strategy Pattern"(in Win Forms) & i am getting trouble in working with Rich TextBox.

    The concept is "User has to choose specific method for Sorting or Searching or Both, Specify the Number of Elements & Elements(in Rich TextBox). And the user doesn't know about implementation & expect exact result of his selection".How to Solve this Problem

    (Please refer the follwing code to understand my view)

    Hope U'll Reply...

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    namespace StrategyPattern
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                lblElements.Visible = lblEle.Visible = txtNo.Visible =lblenter.Visible= rtxtEle.Visible= false;
            }   
            
            private void btnReset_Click(object sender, EventArgs e)
            {            
                txtNo.Text = "";
                chkSearch.Checked=chkSort.Checked=rtxtEle.Visible=lblenter.Visible = false;             
            }
    
            private void btnExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void chkSort_CheckedChanged(object sender, EventArgs e)
            {
                if (chkSort.Checked == true && chkSearch.Checked == true)
                {
                    pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible =lblenter.Visible= rtxtEle.Visible= true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible = pnlSearch.Visible = pnlSort.Visible=rtxtEle.Visible=lblenter.Visible = false;
                }
                if (chkSearch.Checked == false && chkSort.Checked == true)
                {
                    pnlSearch.Visible = false;                
                    pnlSort.Visible=rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == true && chkSort.Checked == false)
                {
                    pnlSearch.Visible=rtxtEle.Visible=lblenter.Visible = true;
                    pnlSort.Visible = false;              
                }
            }
    
            private void chkSearch_CheckedChanged(object sender, EventArgs e)
            {
                if (chkSort.Checked == true && chkSearch.Checked == true)
                {
                    pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible= true;
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible=rtxtEle.Visible=lblenter.Visible = true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    lblEle.Visible = lblElements.Visible = txtNo.Visible = pnlSearch.Visible = pnlSort.Visible =rtxtEle.Visible=lblenter.Visible= false;
                }
                if (chkSearch.Checked == true && chkSort.Checked == false)
                {
                    pnlSort.Visible = false;
                    pnlSearch.Visible = rtxtEle.Visible =lblenter.Visible = true;
                }
                if (chkSearch.Checked == false && chkSort.Checked == true)
                {
                    pnlSearch.Visible = false;
                    pnlSort.Visible = rtxtEle.Visible=lblenter.Visible = true;
                }
            } 
    
            private void btnClickMe_Click(object sender, EventArgs e)
            {
                #region Validation
                if (chkSearch.Checked == false && chkSort.Checked == false)
                {
                    MessageBox.Show("Please Select atleast one Option","User Error");
                }
                Regex rx = new Regex("[^0-9]+");
                if (rx.IsMatch(txtNo.Text))
                {          
                    MessageBox.Show("Enter Correct Number (>0)", "User Error");
                    txtNo.Clear();
                    txtNo.Focus();  
                }
                if (chkSearch.Checked == true || chkSort.Checked == true)
                {
                    if (string.IsNullOrEmpty(txtNo.Text)|| string.IsNullOrEmpty(rtxtEle.Text))
                    {
                        MessageBox.Show("Enter Number", "User Error");
                        txtNo.Focus();
                    }
                }            
                #endregion                                               
                //Here i am getting trouble to get the i/p and passing appropriate objects usinf STRATEGY PATTERN
                //Sorting & Searching Algorithms can be implemented in derived classes
    
                //I Hope I'll get out of this Problem
            }
    
            abstract class SortStrategy
            {           
                public abstract void Sort();
            }
    
            abstract class SearchStrategy
            {
                public abstract void Search();
            }
    
            class QuickSort : SortStrategy
            {
                public override void Sort()
                {                
                    MessageBox.Show("QuickSort Implemented", "Operation Success");
                }
            }
    
            class BubbleSort : SortStrategy
            {
                public override void Sort()
                {               
                   MessageBox.Show("BubbleSort Implemented", "Operation Success");
                }    
            }
            class MergeSort : SortStrategy
            {
                public override void Sort()
                {                
                    MessageBox.Show("Merge Sort Implemented", "Operation Success");
                } 
            }   
            class SeqSearch : SearchStrategy
            {
                public override void Search()
                {
                    MessageBox.Show("Sequential Search Implemented", "Operation Success");
                }
            }
            class BinSearch : SearchStrategy
            {
                public override void Search()
                {
                    MessageBox.Show("Binary Search Implemented", "Operation Success");
                }
            }                                   
        }
    }
    
    

     


    Nata...
    Monday, November 14, 2011 10:48 AM
  • Nata,

     

    In order to do this, you would need to have a member that was the strategy to use.  In this case, you could add a member for searching, like so:

    private SearchStrategy searchStrategy;
    


    When the user changes the selection for the search strategy, you'd set that element, ie:

    // Subscribe to the radio box searching here...
    private void radioBoxSearch_CheckedChanged(object sender, EventArgs e)
    {
        if ( // ... Check for "Sequential Search checked"
        {
            // Set the strategy element...
            this.searchStrategy = new SeqSearch();
        }
        else
        {
            // Otherwise, set this to binary search
            this.searchStrategy = new BinSearch(); 
        }
    }
    
    
    
    

    Later, when the user hits "Click me", you would just call:

     

    // You can just call the method directly, without care of which "strategy" is in use...
    this.strategySearch.Search();
    

     

    (Note that it's also a good idea to instantiate with a "default" strategy for each type...)

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Natraj A V Thursday, November 17, 2011 2:12 PM
    Monday, November 14, 2011 5:53 PM
  • OfCourse it works for any one strategy but, how can the two strategies works simultaneously on single object?

    With Regards...


    Nata...
    Tuesday, November 15, 2011 2:42 AM
  • OfCourse it works for any one strategy but, how can the two strategies works simultaneously on single object?

    With Regards...


    Nata...

    That's not the purpose of the strategy pattern...  The strategy pattern is intended to allow you to swap out strategy with any other strategy.

     

    What you're describing is more like a variation on a decorator or a chain of responsibility, depending on how you want it to work...


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 15, 2011 7:25 PM