Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Engineering Concepts (http://www.go4expert.com/articles/engineering-concepts-tutorials/)
-   -   Design Pattern in Simple Examples (http://www.go4expert.com/articles/design-pattern-simple-examples-t5127/)

yogeshchaudhari 28May2010 15:50

Re: Design pattern in simple examples
 
Quote:

Originally Posted by shabbir (Post 68483)
Download it once again because there are many files in the zip file

Hello Shabbir,

You explanined it the way it should be. But, I think the zip file is corrupted. I did several attempts of download and found that zip is not getting downloaded completely. Can you please upload it once again.

Yogesh

shabbir 28May2010 16:32

Re: Design pattern in simple examples
 
Quote:

Originally Posted by yogeshchaudhari (Post 68534)
Hello Shabbir,

You explanined it the way it should be. But, I think the zip file is corrupted. I did several attempts of download and found that zip is not getting downloaded completely. Can you please upload it once again.

Yogesh

No and I again downloaded it and extracted it. Works fine for me. I am not sure how can I send you so many files without zipping them.

johngate2100 16Aug2010 22:14

Re: Design pattern in simple examples
 
This is very nice post and helpful for other people.

dhanoopnambiar 29Aug2010 23:42

Re: Design pattern in simple examples
 
Thanks

mcc_prabha 14Sep2010 12:58

Re: Design pattern in simple examples
 
The Code sample zip file, seems to be having some issue. If I download, I am getting only the size 97KB and it is not archivable. Can you please check and share the zip file again.

shabbir 14Sep2010 13:06

Re: Design pattern in simple examples
 
I tried downloading it once again now and it works fine for me and try it once again to see if it works. Size shown in the zip file should match your file size or else there might be some download errors.

eckt 22Nov2010 07:24

Re: Design pattern in simple examples
 
Hi!
I'm doing all the pre work for developing a feature for a FOSS program and I need help on which design pattern to use. The feature(s) is the ability to add a range of filters to and image (like in photoshop). It has to be easily extendable as I might come up with new filters along the way but the code for applying each of these filters is largely the same (the only thing that's different is the filter values).
Based on this do you know a design pattern that would be appropriate?
Thanks for your help!
-Emma

drai82@gmail.com 14Dec2010 05:47

Re: Design pattern in simple examples
 
Hi,

Great Article.
I have one question. If I have to develop a IVRS, which Design Pattern should be used?

shabbir 14Dec2010 08:30

Re: Design pattern in simple examples
 
Quote:

Originally Posted by drai82@gmail.com (Post 75887)
Hi,

Great Article.
I have one question. If I have to develop a IVRS, which Design Pattern should be used?

The one (or combination of few) that best suits your need

Hi528 18Dec2010 19:04

Re: Design pattern in simple examples
 
Good Article...Thanks

Nathim 24Jan2011 01:31

Re: Design pattern in simple examples
 
Really very good job Mr. shabbi
thank U

nsures 10Feb2011 17:45

Re: Design pattern in simple examples
 
Hello Shabbir,
Excellent article, however i have trouble getting the attachment project (97 KB when i DLed) , could you please look into this

Suresh

shabbir 10Feb2011 18:44

Re: Design pattern in simple examples
 
Quote:

Originally Posted by nsures (Post 79086)
Hello Shabbir,
Excellent article, however i have trouble getting the attachment project (97 KB when i DLed) , could you please look into this

Suresh

Hi suresh, I re-downloaded it and don't see any issues. If you still have issues getting the file email me and I will send you the file as an attachment.

ashwinparmar 16May2011 16:55

Re: Design pattern in simple examples
 
I have solved your problem.
-- Ashwin Parmar


Code:

<?php
  abstract class Control {
      abstract function Show();
  }

  class WinButton extends Control {
      function Show() {
        echo "I'm a WinButton: ";
      }
  }

  class MacButton extends Control {
      function MacButton() {
        echo "MacButton";
      }
      function Show() {
        echo "I'm an MacButton: ";
      }
  }

  class WinLabel extends Control {
      function Show() {
        echo "I'm a WinLabel: ";
      }
  }

  class MacLabel extends Control {
      function Show() {
        echo "I'm an MacLabel: ";
      }
  }

    abstract class GUIFactory {
      static function GetFactory() {
            if (TRUE) return new MacFactory();
            else return new WinFactory();
      }
      abstract function CreateButton();
      abstract function CreateLabel();
  } 
   
  class WinFactory extends GUIFactory{
       
        public function WinFactory() {
            $btn = new WinButton();
            $lbl = new WinLabel();
        }
 
        function CreateButton() {
            print_r($btn);
            return $btn;
        }
        function CreateLabel() {
            return $lbl;
        }
  }

  class MacFactory extends GUIFactory {
      // Its there just to have the Classdiagram going
       
      function CreateButton() {
        return new MacButton();
      }

      function CreateLabel() {
        return new MacLabel();
      }
  }

 
    $factory = GUIFactory::GetFactory(); // Return MacFactory
    $button = $factory->CreateButton();  // Return MacButton
    echo $button->Show();
   
    $label = $factory->CreateLabel();
    echo $label->Show();
   
?>

================================================== =======
Quote:

Originally Posted by ngungo (Post 26985)
This is my first post. I try to convert the Abstract Factory class in c# to php. Of course, it does not work. Can you correct it for me, or give me a hint. Thanks.
--ngungo

Code:

<?php
  class GUIFactory {
      function GetFactory() {
            if (TRUE) return new MacFactory();
            else return new WinFactory();
      }
      public function CreateButton() {};
      public function CreateLabel() {};
  }

  class WinFactory extends GUIFactory {
      // Its there just to have the Classdiagram going
      $btn = new WinButton();
      $lbl = new WinLabel();
      public override function CreateButton() {
        return $btn;
      }

      public override function CreateLabel() {
        return $lbl;
      }
  }

  class MacFactory function GUIFactory {
      // Its there just to have the Classdiagram going
      $btn = new MacButton();
      $lbl = new MacLabel();
      public override function CreateButton() {
        return $btn;
      }

      public override function CreateLabel() {
        return $lbl;
      }
  }

  abstract class Control {
      public abstract function Show();
  }

  class WinButton extends Control {
      public override function Show() {
        echo "I'm a WinButton: ";
      }
  }

  class MacButton extends Control {
      public override function Show() {
        echo "I'm an MacButton: ";
      }
  }

  class WinLabel extends Control {
      public override function Show() {
        echo "I'm a WinLabel: ";
      }
  }

  class MacLabel extends Control {
      public override function Show() {
        echo "I'm an MacLabel: ";
      }
  }
 
    $factory = GUIFactory.GetFactory();
    $button = factory.CreateButton();
    echo $button->Show();
   
    $label = factory.CreateLabel();
    echo $label->Show();
?>



ashwinparmar 16May2011 16:56

Re: Design pattern in simple examples
 
PHP Code:

<?php
  
abstract class Control {
      abstract function 
Show();
   }

   class 
WinButton extends Control {
      function 
Show() {
         echo 
"I'm a WinButton: ";
      }
   }

   class 
MacButton extends Control {
      function 
MacButton() {
        echo 
"MacButton";
      }
      function 
Show() {
         echo 
"I'm an MacButton: ";
      }
   }

   class 
WinLabel extends Control {
      function 
Show() {
         echo 
"I'm a WinLabel: ";
      }
   }

   class 
MacLabel extends Control {
      function 
Show() {
         echo 
"I'm an MacLabel: ";
      }
   }

    abstract class 
GUIFactory {
      static function 
GetFactory() {
            if (
TRUE) return new MacFactory();
            else return new 
WinFactory(); 
      }
      abstract function 
CreateButton();
      abstract function 
CreateLabel();
   }   
    
   class 
WinFactory extends GUIFactory{
        
        public function 
WinFactory() {
            
$btn = new WinButton();
            
$lbl = new WinLabel();
        }
   
        function 
CreateButton() {
            
print_r($btn);
            return 
$btn;
        }
        function 
CreateLabel() {
            return 
$lbl;
        }
   }

   class 
MacFactory extends GUIFactory {
      
// Its there just to have the Classdiagram going
        
      
function CreateButton() {
         return new 
MacButton();
      }

      function 
CreateLabel() {
         return new 
MacLabel();
      }
   }

 
    
$factory GUIFactory::GetFactory(); // Return MacFactory
    
$button $factory->CreateButton();  // Return MacButton
    
echo $button->Show();
    
    
$label $factory->CreateLabel();
    echo 
$label->Show();
    
?>


Lulugreen 23May2011 15:13

Re: Design pattern in simple examples
 
Nice article! It is very helpful for me! Thanks!

r0o0cky 5Jul2011 01:06

Re: Design pattern in simple examples
 
Which pattern is best for an ASP.NET website with SQL server database and .NET framework 2.0, Thanks.

shabbir 5Jul2011 08:08

Re: Design pattern in simple examples
 
Quote:

Originally Posted by r0o0cky (Post 84695)
Which pattern is best for an ASP.NET website with SQL server database and .NET framework 2.0, Thanks.

The application of pattern depends on lot of factors and in your case the type of website you are creating. An ecommerce site in ASP and a CMS site in ASP may not have the same pattern applied.

r0o0cky 5Jul2011 10:49

Re: Design pattern in simple examples
 
Quote:

Originally Posted by shabbir (Post 84699)
The application of pattern depends on lot of factors and in your case the type of website you are creating. An ecommerce site in ASP and a CMS site in ASP may not have the same pattern applied.

Thanks, Shabbir. I know that we have to take in account the type of application we are developing. We often hear of MVC or MVP, are these design patterns or something else.

I wanted to know what basic pattern a new bie should follow, which is easy to maintain and work on. Or Are there some frameworks we should use.

Mostly ASP.NET websites are developed using the code-behind page logic, or some develop interfaces implemented by the code-behind page and than pass the page instance to a controller or presenter.

Any help from Shabir or other members, Thanks.

shabbir 5Jul2011 13:51

Re: Design pattern in simple examples
 
Quote:

Originally Posted by r0o0cky (Post 84701)
Thanks, Shabbir. I know that we have to take in account the type of application we are developing. We often hear of MVC or MVP, are these design patterns or something else.

I wanted to know what basic pattern a new bie should follow, which is easy to maintain and work on. Or Are there some frameworks we should use.

Mostly ASP.NET websites are developed using the code-behind page logic, or some develop interfaces implemented by the code-behind page and than pass the page instance to a controller or presenter.

Any help from Shabir or other members, Thanks.

To be honest I cannot name one pattern that every newbie should start with.

r0o0cky 10Jul2011 16:34

Re: Design pattern in simple examples
 
I found awnser to my query, so i thought to share it in this forum so others can also know this. Bydefault ASP.NET implements Page Controller pattern through out the ASP.NET Page life cycle which is the code-behind page.

For beginners who have just started working on ASP.NET the page controller pattern can serve as entry level. The drawback is lack of unit testing and repetition of logic as the logic resides inside the code behind file.

Next level after this could be the MVP pattern, which is best for UI applications like ASP.NET, Windows, Mobile, etc. This pattern is easy to learn and very manageable. But it is recommended only if you need unit testing, you need to reuse the logic, or your interface can be different like Web, Windows or Mobile application.

Shabbir you can correct me if I am incorrect anywhere, Thanks.

shabbir 10Jul2011 20:55

Re: Design pattern in simple examples
 
Quote:

Originally Posted by r0o0cky (Post 84896)
I found awnser to my query, so i thought to share it in this forum so others can also know this. Bydefault ASP.NET implements Page Controller pattern through out the ASP.NET Page life cycle which is the code-behind page.

For beginners who have just started working on ASP.NET the page controller pattern can serve as entry level. The drawback is lack of unit testing and repetition of logic as the logic resides inside the code behind file.

Next level after this could be the MVP pattern, which is best for UI applications like ASP.NET, Windows, Mobile, etc. This pattern is easy to learn and very manageable. But it is recommended only if you need unit testing, you need to reuse the logic, or your interface can be different like Web, Windows or Mobile application.

Shabbir you can correct me if I am incorrect anywhere, Thanks.

I am not ASP.NET expert and so cannot comment on what you are saying but I think if you have tested it would right only. Yes MVP pattern is used at many places including in MFC classes.

sigfin2004 22Aug2011 20:31

Re: Design pattern in simple examples
 
may i ask the sources for the main UML diagram, they look like they were built using sparx enterprise architect. would be most grateful sir.

Thanks for any consideration.

shabbir 22Aug2011 21:11

Re: Design pattern in simple examples
 
Quote:

Originally Posted by sigfin2004 (Post 86267)
may i ask the sources for the main UML diagram, they look like they were built using sparx enterprise architect. would be most grateful sir.

Thanks for any consideration.

Nope I have used Visual Studio and EA.

sigfin2004 22Aug2011 21:20

Re: Design pattern in simple examples
 
Quote:

Originally Posted by shabbir (Post 86269)
Nope I have used Visual Studio and EA.

EA is sparx enterprise architect.

: ) i would appreciate it if you still have the eap file?

shabbir 22Aug2011 21:49

Re: Design pattern in simple examples
 
Quote:

Originally Posted by sigfin2004 (Post 86270)
EA is sparx enterprise architect.

: ) i would appreciate it if you still have the eap file?

No I don't have them.

simple 13Sep2011 13:32

Re: Design pattern in simple examples
 
Flyweight = use the same existing one instead of creating new one. in your example how its achieved?

Trimegisto 22Sep2011 16:34

Re: Design pattern in simple examples
 
Very useful info. Thanks!

Md Tariq ul Islam 4Oct2011 14:32

Re: Design pattern in simple examples
 
source code download link is not working

shantanudesai2009 4Dec2011 00:51

Re: Design pattern in simple examples
 
Hey.. thats really nice and helpful post for the newbies in DP.. Can u please suggest me where I can find excercises on Design patterns.. like case studies or something..??

Example : Some situation be given , and asked for which design pattern will suit this..?? or some situation be given, and asked how "particular" DP is used here??

It shall really be helpful.. thanks in advanced.. :)

mitendra 18Dec2011 01:00

Re: Design pattern in simple examples
 
very nice work. Thanks!

raymondc 30Dec2011 00:41

Re: Design pattern in simple examples
 
Hi Shabbir,
Am not able to open the downloaded zip file. Complains it is corrupt. Can you please verify.

Cheers,
RayC

shabbir 30Dec2011 09:02

Re: Design pattern in simple examples
 
Its verified multiple times and yet again today. I see no issues in the files when downloaded.

Trimegisto 30Dec2011 20:09

Re: Design pattern in simple examples
 
I can confirm that; the files are ok!

jandrecosta 19May2012 21:43

Re: Design pattern in simple examples
 
Hi Shabbir,

I need some help, I am trying to identify the design patterns existents in the Enterprise Library in the Exception code.
Im a student with some programming dificulties, and the more that I read, more confused I get.

Can you give me a help here?

Thank you

shabbir 20May2012 00:27

Re: Design pattern in simple examples
 
Quote:

Originally Posted by jandrecosta (Post 95052)
Hi Shabbir,

I need some help, I am trying to identify the design patterns existents in the Enterprise Library in the Exception code.
Im a student with some programming dificulties, and the more that I read, more confused I get.

Can you give me a help here?

Thank you

Not sure I am able to understand what issues you are having. Can you explain a bit more.

jandrecosta 20May2012 00:30

Re: Design pattern in simple examples
 
I am studying tha Enterprise library 5.0 and I must identify in the exception block which of the design patterns are implemmented. I would like to ask you if you have something on this that may help me with my quest.

pradeeptc 18Jun2012 08:39

Re: Design pattern in simple examples
 
The downloaded sample file doesnt have a valid extension (inside the zip file). Any idea what is the problem?

shabbir 18Jun2012 09:26

Re: Design pattern in simple examples
 
Quote:

Originally Posted by pradeeptc (Post 95779)
The downloaded sample file doesnt have a valid extension (inside the zip file). Any idea what is the problem?

What extension do you have and what is the expected extension of the file?

pradeeptc 18Jun2012 10:40

Re: Design pattern in simple examples
 
Quote:

Originally Posted by shabbir (Post 95781)
What extension do you have and what is the expected extension of the file?

Sorry . It was mistake. When I downloaded the fie from the link (in ur explanatio at the beginning u have mentioned about the sample file), it was extracted as a file without any extension. May be my machine problem. When I have downloaded from the bottom, I got the proper file. Do u have the C++ version of these files?

Great work.

-Pradeep


All times are GMT +5.5. The time now is 15:50.