Code Monkey home page Code Monkey logo

factorysupporter's Introduction

FactorySupporter

Module for supporting the implementation of Factory Pattern in .NET. The FactorySupporter essentially uses System.Reflection and metadata annotating by Attributes to interpret and instantiate the implementations of your Factory Pattern.

The usage of StrategySupporter

Step 1

Define your custom Attribute derived from IdentifierAttribute. For example:

    public class CarAttribute : IdentifierAttribute
    {
        private string _carType;

        public CarAttribute(string carType)
        {
            _carType = carType;
        }

        public string GetCarType()
        {
            return _carType;
        }
    }

Step 2

Implement your own Factory Pattern and tag the implementations' classes with your derived IdentifierAttribute. For example:

    public interface ICar
    {
        void Beep();
    }
    
    [CarAttribute("sportCar")]
    public class SportCar : ICar
    {
        public void Beep()
        {
            Console.WriteLine("A sport car beeped.");
        }
    }
    
    [CarAttribute("miniVan")]
    public class MiniVan : ICar
    {
        public void Beep()
        {
            Console.WriteLine("A mini van beeped.");
        }
    }
    
    [CarAttribute("bus")]
    public class MiniBus : ICar
    {
        public void Beep()
        {
            Console.WriteLine("A mini bus beeped.");
        }
    }
    
    public class CarContext
    {
        private IImplementationFactory _implementationFactory;

        private ICar _car;

        public CarContext(Assembly executingAssembly, IdentifierFunc<CarAttribute> identifierFunc)
        {
            _implementationFactory = new ImplementationFactory(executingAssembly);
            _car = _implementationFactory.Create<ICar, CarAttribute>(identifierFunc);
        }

        public void Beep()
        {
            _car.Beep();
        }
    }

Step 4

Instantiate the CarContext, and pass the required parameters. For example:

    public static void Main(string[] args)
    {
        // The Assembly, where your implenetations are.
        Assembly executingAssembly = Assembly.GetExecutingAssembly();

        // define the CarContext
        CarContext carContext = new CarContext(executingAssembly, MiniVanIdentifierFunc);
        
        // The output (in this case: "A mini van beeped.")
        carContext.Beep();
    }
    
    // Define the function as a IdentifierFunc, which implements your base logic
    // for determining the required implementation.
    public static bool MiniVanIdentifierFunc(CarAttribute carAttribute)
    {
        // By the passed Attribute you can define the condition for searching the implementation. 
        return carAttribute.GetCarType() == "miniVan";
    }
    
    

Step 5

Verify your output

Output:

"A mini van beeped."

factorysupporter's People

Contributors

martinmarkus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.