Jorge Marquez Jose Gomez

Tag: Patterns

Pattern Factory (Factory)

by to ago.27, 2010, under DoNet

A factory pattern serves to channel all creations of objects in a single point. This is useful for robust design of our system and responsive to change.

The factory pattern can be combined with the singleton pattern as it is more comfortable to use and useful form to use. In this example we will put a factory without singleton.

To say the factory pattern is of the creational patterns, this type of pattern that abstracts the way to create concrete objects, thus giving support to create objects in the abstract.

To start a factory pattern is to perform:
- A function or method which is responsible for creating objects of a given type.
- It is not mandatory but recommended that the function or method returns an abstraction (interface or abstract class).

Imagine a pizza shop requires a software you need to have a number of products (pizzas varied). As designers we are good, the pizza object creation can be done from anywhere in the project, but the problem is that there are many types of pizzas. To create each pizza for each type in different parts of the program would be the first thing you would think, but ... if instead we use common sense and apply the factory?, This would allow us as a new product or change in any of them that only change in one place, taking more controlled this code. Even if you realize the speed of change is much more to go changing all possible appearances of our pizzas by the code.
Well, the factory would be responsible for creating all kinds of pizzas and offer all possible points of the program.

Our example would be:

- The interface IPizza:

     IPizza interface
     {
         WhoAmI void ();
     }

- The individual pizzas:

     public class PizzaBarbacoa: IPizza
     {
         # Region Members IPizza

         public void WhoAmI ()
         {
             Console.WriteLine ("I am a BBQ pizza");
         }

         # Endregion
     }
     public class PizzaMargarita: IPizza
     {
         # Region Members IPizza

         public void WhoAmI ()
         {
             Console.WriteLine ("I am a pizza Margarita");
         }

         # Endregion
     }
     public class PizzaPepperoni: IPizza
     {
         # Region Members IPizza

         public void WhoAmI ()
         {
             Console.WriteLine ("I am a pizza Pepperoni");
         }

         # Endregion
     }

- The plant in question:

     public class PizzaFactory
     {
         public enum tipoPizza
         {
             Pepperoni, Margherita, Barbeque
         }

         IPizza public getPizza (tipoPizza type)
         {
             switch (type)
             {
                 tipoPizza.Barbacoa case:
                     return new PizzaBarbacoa ();
                     break;
                 tipoPizza.Margarita case:
                     return new PizzaMargarita ();
                     break;
                 tipoPizza.Pepperoni case:
                     return new PizzaPepperoni ();
                     break;
                 default:
                     return new PizzaBarbacoa ();
             }
         }
     }

- A little test:

     public class Test
     {
         public void testPizza ()
         {
             / / Example not using factory
             IPizza pizza1 = new PizzaBarbacoa ();
             pizza1.quienSoy ();
             IPizza pizza2 = new PizzaMargarita ();
             pizza2.quienSoy ();
             IPizza pizza3 = new PizzaPepperoni ();
             pizza3.quienSoy ();
             IPizza pizza4 = new PizzaBarbacoa ();
             pizza4.quienSoy ();
             IPizza pizza5 = new PizzaMargarita ();
             pizza5.quienSoy ();

             / / Example using factory
             PizzaFactory PizzaFactory factory = new ();
             IPizza pizza6 = factory.getPizza (PizzaFactory.tipoPizza.Barbacoa);
             pizza6.quienSoy ();
             IPizza pizza7 = factory.getPizza (PizzaFactory.tipoPizza.Margarita);
             pizza7.quienSoy ();
             IPizza pizza8 = factory.getPizza (PizzaFactory.tipoPizza.Pepperoni);
             pizza8.quienSoy ();
         }
     }

As you can see how to use and its advantages are better. I hope you like!. See you later!

más... Leave a comment : , , , more ...

Unique design patterns (Singleton) and Decorator (Abstract or Decorator). NET

by to nov.24, 2009, under DoNet

Well, after my lethargy (sorry not posting earlier, but the university I tapeworm has overwhelmed), I'll talk about some design patterns. NET.

But first a little "history":

Design patterns are used for various things, (taken from wikipedia):
* Providing catalogs of reusable elements in the design of software systems.
* To avoid repetition in the search for solutions to known problems and solved before.
* Formalize a common vocabulary among designers.
* Standardize mode is performed design.
* Facilitating the learning of new generations of designers condensing existing knowledge.

And as we say in wikipedia does not intend to impose anything and abusing them may be a mistake.

Well let's see some patterns. NET.

Singular Patron (Singleton):
This pattern assures that there is only one instance per class avoiding excessive consumption of resources or access inconsistent. Its uses may be several, including access to global settings, logging, etc. ..
The general scheme is:

  Public Class PatronSingular
  Private Shared As PatronSingular instanciaUnica

  Private Sub New ()
  'Private constructor to create the instance's class
  End Sub

  Public Shared Function getInstance () As PatronSingular
  'Control the creation of objects from the class itself.
  Is Nothing Then If instanciaUnica
  New PatronSingular instanciaUnica = ()
  End If
  Return instanciaUnica
  End Function
 End Class 

A Singleton pattern its constructor should be private to the class is to control their own creation and is responsible for it. We must create a method, which is usually called "getInstance ()" which returns this class instance to use.

Decorator pattern (Decorator):
It makes "wrapper" objects, in a formal definition of what it does is set a functionality to objects that inherit from it and thus assign responsibility. In short, gives us the ability to generate classes with functions that are generated dynamically. There are many examples of this type. NET, including file management (FileStream your decorator is Stream). Compared to Java, this would correspond to abstract classes. Example:

  Public Class MustInherit PatronDecorador
  'It offers a base functionality for all children but
  'That work differently:
  'For example:
  'Chocolate will be priced differently and description
  'Butter, but both can melt!.
  Public Function MustOverride Price () As Double
  Public MustOverride Function Description () As Double

  Public Function Melt () As Boolean
  'Melt
  Return True
  End Function

  'It forces their children to be instantiated in a particular way
  Public Sub New (ByVal ModificadorAcceso As String)
  End Sub
 End Class

 Public Class Chocolate
  Inherits PatronDecorador

  Public Overrides Function Description () As Double
  Return "Chocolate"
  End Function

  Public Overrides Function Price () As Double
  10.2 Return
  End Function

  Public Sub New (ByVal param As String)
  MyBase.New (param)
  End Sub
 End Class

 Public Class Butter
  Inherits PatronDecorador

  Public Overrides Function Description () As Double
  Return "Butter"
  End Function

  Public Overrides Function Price () As Double
  3.6 Return
  End Function

  Public Sub New (ByVal param As String)
  MyBase.New (param)
  End Sub
 End Class 

Well, I hope to write more often, but time has drowned me!, In another article I will describe the strategy pattern, adapters and observers. Greetings!

más... 1 Comment : , , , , more ...

Looking for something?

Use the form below to search the site:

Have not found what you're looking for? Leave a comment or contact us to take care.

Visit our friends!

A few highly recommended sites ...