begin process at 2010 02 10 01:32:35
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

.NET

 > ABSTRACT FACTORY

ABSTRACT FACTORY


 Description

Abstract Factory pattern est un motif Création. Patterns Création traité meilleure façon de créer un objet.
Abstract Factory est une Factory qui retourne un objet de plusieurs Factorys.
Dans cette example, l'interface Factory dispose de deux implémentations concrètes, ConcreteFactory1 et ConcreteFactory2. Le GetObject (), l'intérieur de ces classes concrètes Derived1 et retours Derived2 objets, respectivement.
Le client peut décider de la classe ConcreteFactory qui doit être utilisé pendant « Run Time ».

Source

  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • interface IBrickObjects
  • {
  • void Rotate();
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • interface ISelect
  • {
  • IBrickObjects GetBrickObject();
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class ObjectH : IBrickObjects
  • {
  • public void Rotate()
  • {
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class ObjectHFactory:ISelect
  • {
  • public IBrickObjects GetBrickObject()
  • {
  • return (new ObjectH());
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class Rectangle : IBrickObjects
  • {
  • public void Rotate()
  • {
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class RectangleFactory : ISelect
  • {
  • public IBrickObjects GetBrickObject()
  • {
  • return (new Rectangle());
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class ReverseH : IBrickObjects
  • {
  • public void Rotate()
  • {
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class ReverseHFactory : ISelect
  • {
  • public IBrickObjects GetBrickObject()
  • {
  • return (new ReverseH());
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class Square : IBrickObjects
  • {
  • public void Rotate()
  • {
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class SquareFactory : ISelect
  • {
  • public IBrickObjects GetBrickObject()
  • {
  • return (new Square());
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class Straightline : IBrickObjects
  • {
  • public void Rotate()
  • {
  • }
  • }
  • }
  • using System;
  • using System.Collections.Generic;
  • using System.Text;
  • namespace BrickGame4AbsctractFactory
  • {
  • class StraightlineFactory : ISelect
  • {
  • public IBrickObjects GetBrickObject()
  • {
  • return (new Straightline());
  • }
  • }
  • }
  • Client Code
  • using System;
  • using System.Collections.Generic;
  • using System.ComponentModel;
  • using System.Data;
  • using System.Drawing;
  • using System.Text;
  • using System.Windows.Forms;
  • namespace BrickGame4AbsctractFactory
  • {
  • public partial class Main : Form
  • {
  • ISelect m_ObjSelect;
  • public Main()
  • {
  • InitializeComponent();
  • }
  • private IBrickObjects ActivateObject(string ObjectName)
  • {
  • switch (ObjectName)
  • {
  • case "Square":
  • m_ObjSelect = new SquareFactory();
  • break;
  • case "Straightline":
  • m_ObjSelect = new StraightlineFactory();
  • break;
  • case "Rectangle":
  • m_ObjSelect = new RectangleFactory ();
  • break;
  • }
  • return m_ObjSelect.GetBrickObject() ;
  • }
  • private void Main_Load(object sender, EventArgs e)
  • {
  • IBrickObjects l_objBrickObject = ActivateObject("Square");
  • l_objBrickObject.Rotate();
  • }
  • }
  • }
using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    interface IBrickObjects
    {
        void Rotate();
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    interface  ISelect
    {
        IBrickObjects GetBrickObject();
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class ObjectH : IBrickObjects
    {
        public void Rotate()
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class ObjectHFactory:ISelect 
    {
        public  IBrickObjects GetBrickObject()
        {
            return (new ObjectH()); 
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class Rectangle : IBrickObjects
    {
        public  void Rotate()
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class RectangleFactory : ISelect 
    {
        public IBrickObjects GetBrickObject()
        {
            return (new Rectangle());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class ReverseH : IBrickObjects 
    {
        public void Rotate()
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class ReverseHFactory : ISelect 
    {
        public IBrickObjects GetBrickObject()
        {
            return (new ReverseH());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class Square : IBrickObjects
    {
        public void Rotate()
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class SquareFactory : ISelect 
    {
        public IBrickObjects GetBrickObject()
        {
            return (new Square());
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class Straightline : IBrickObjects
    {
        public void Rotate()
        {
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace BrickGame4AbsctractFactory
{
    class StraightlineFactory : ISelect 
    {
        public IBrickObjects GetBrickObject()
        {
            return (new Straightline());
        }
    }
}


Client Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BrickGame4AbsctractFactory
{
    public partial class Main : Form
    {
        ISelect m_ObjSelect;
        public Main()
        {
            InitializeComponent();
        }

        private IBrickObjects ActivateObject(string ObjectName)
        {
            switch (ObjectName)
            {
                case "Square":
                    m_ObjSelect = new SquareFactory();
                    break;
                case "Straightline":
                    m_ObjSelect = new StraightlineFactory();
                    break;
                case "Rectangle":
                    m_ObjSelect = new RectangleFactory ();
                    break;
                
            
            }
            return m_ObjSelect.GetBrickObject() ;

        }
        private void Main_Load(object sender, EventArgs e)
        {
	
            IBrickObjects l_objBrickObject = ActivateObject("Square"); 
            l_objBrickObject.Rotate();
 
        }
    }
}

 Conclusion

L'interface a IBrickObjects,la méthode qui doit être appliquée par tous les objets de la brique.
Les formes ISelect interface de l'interface de l'usine de tous les objets de Brick objet qui sera l'objet exact pour appeler la méthode GetObject.


 Sources de la même categorie

Source avec Zip CHAT SERVER-CLIENT par abderrahmenbilog
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMULATION DE CONSOLE POUR WINDOWS MOBILE par originalcompo
Source avec Zip Source .NET (Dotnet) BASE DE DONNÉES EN XML par DanMor498
Source avec Zip Source avec une capture Source .NET (Dotnet) SIMPLECONV - APPLICATION DE CONVERSION MONÉTAIRE AVEC TAUX E... par Jeffrey_
Source avec Zip Source .NET (Dotnet) TRAITEUR D'IMAGE (MINI) par ycyril

 Sources en rapport avec celle ci

Source avec Zip Source .NET (Dotnet) EXTENSION DES LISTES GÉNÉRIQUES (DESIGN PATTERN "DECORATEUR"... par yoannd

Commentaires et avis

Commentaire de Warny le 14/07/2009 11:21:19

Salut,
Je ne vois pas du tout l'interêt de ton code. Le système que tu présente ici est efficacement remplacé par la reflexion (namespace System.Reflection) qui permet même de créer des objets dont les classes sont définies dans des librairies externes et non référencées dans le projet.

Commentaire de sebmafate le 14/08/2009 09:02:24 administrateur CS

En fait, c'est intéressant quand c'est maitrisé... par exemple, on peut imaginer que tu développes toute la logique d'accés aux données de ton application (select one, select all, insert, delete, update)... ensuite tu définis dans une autre assembly les spécialisations pour chaque objet (ou table).
Ensuite, il ne te reste plus qu'à charger dynamiquement (Reflection) cette assembly et l'utiliser via la factory. Avantage, tu peux modifier l'implémentation sans toucher à l'application.

J'ai un article (et une source) en cours sur ce concept.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,624 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales