Giter Site home page Giter Site logo

dingpingzhang / simplefactorygenerator Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 7.0 151 KB

A simple factory source generator that enables the pattern to not violate the open-close principle.

License: MIT License

C# 100.00%
design-pattern design-patterns design-principles simple-factory simple-factory-pattern architecture factory-method factory-method-pattern

simplefactorygenerator's Introduction

SimpleFactoryGenerator version

English | δΈ­ζ–‡

This library is used to assist in the implementation of the Simple Factory Pattern by automatically generating conditional branch structure in the factory class at compile time, thus solving the problem of the pattern violating the "open-close principle".

1. Why?

The introduction and use of the Simple Factory Pattern will not be described here. One of the drawbacks of this pattern is the need to manually maintain a (potentially huge) conditional branch structure for creating concrete instances of a given enumeration type (including strings). As a result, the pattern violates the "open to extensions, closed to modifications" design principle, which is solved by this library. The idea is very simple: design principles are a set of rules of thumb to facilitate the maintenance of code by Humans, to compensate for some of the limitations of human thinking. Therefore, there is no violation of the Design Principles by simply leaving this difficult-to-maintain part of the code to the compiler.

2. How?

Let's start by looking at a simple factory implemented in the traditional way, in order to facilitate comparison of the parts replaced by this library.

public interface IProduct
{
}

public class Product1 : IProduct
{
}

public class Product2 : IProduct
{
}

public class SimpleFactory
{
    public IProduct Create(string type)
    {
        // Here is the branch judgment that violates the open-close principle,
        // for example, when adding `Product3`, you need to manually add
        // a branch here (`"product_c" => new Product3(),`).
        return type switch
        {
            "product_a" => new Product1(),
            "product_b" => new Product2(),
            _ => throw new IndexOutOfRangeException(),
        }
    }
}

// Using

var factory = new SimpleFactory();
IProduct product = factory.Create("product_a");

After using this library, the writing of SimpleFactory will be omitted and instead, a ProductAttribute<K, T> needs to be declared on the concrete Product type. You have already noticed: the Attribute uses generics, which requires C# 11 support, for which you need to use Visual Studio 2022 and configure it in the *.csproj file: <LangVersion>preview</LangVersion>.

If your project cannot be configured for C# 11 or uses Visual Studio 2022, which prevents you from directly using Generic-Attribute, you can refer to section 3.1 and customize an Attribute to inherit from ProductAttribute<K, T> (the Generic-Attribute definitions were allowed before C# 11, just not directly available).

// It can also be an abstract class, or a normal class, and it is not mandatory to be an interface.
public interface IProduct
{
}

[Product<string, IProduct>("product_a")]
public class Product1 : IProduct
{
}

[Product<string, IProduct>("product_b")]
public class Product2 : IProduct
{
}

// Using

// The SimpleFactory static class are provided by this library.
var factory = SimpleFactory
    .For<string, IProduct>()
    // .WithCache() is optional decorator
    // that caches created instances (i.e. instances with the same key are created multiple times
    // and the same instance is returned.)
    .WithCache();
IProduct product = factory.Create("product_a");

3. Advanced

It's not really that advanced, it's such a simple requirement, what are you expecting? :)

3.1 Custom Attribute

If you think the ProductAttribute<K, T> declaration too long, too ugly, too cumbersome (or can't use C# 11's Generic-Attribute syntax), you can customize an Attribute to inherit it.

public class FruitAttribute : ProductAttribute<string, IProduct>
{
    public FruitAttribute(string name) : base(name)
    {
    }
}

[Fruit("apple")]
public class Apple : IProduct
{
}

3.2 Mapping to multiple target interfaces

For the same concrete product type, it is allowed to supply to multiple different target interfaces.

[Animal("mouse")]
[Food("duck_neck")]
public class Mouse : IAnimal, IFood
{
}

// Using
var animalFactory = SimpleFactory.For<string, IAnimal>();
IProduct mouse = animalFactory.Create("mouse");

var foodFactory = SimpleFactory.For<string, IFood>();
IProduct duckNeck = foodFactory.Create("duck_neck");

3.3 Passing arguments to the constructor

If a constructor of type Product has parameters, they can be passed as follows:

_ = factory.Create("key", arg1, arg2, ...);

Since it is often not possible to determine the constructor that creates the type when using factory, it is recommended that all Product constructors have a consistent argument list.

If there is a need to make the constructor arguments for each Product indeterminate, it is recommended that it be created in an Ioc container:

var factory = SimpleFactory
    .For<Key, Product>()
    .WithCreator((type, args) => (Product)container.Resolve(type, args));

_ = factory.Create(key);

Note: If you use .WithCreator() after .WithCache(), it will cause the previous cache to be cleared.

simplefactorygenerator's People

Contributors

dingpingzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.