Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7158859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:08:10+00:00 2026-05-28T13:08:10+00:00

I have this abstract factory pattern that I use in as3 that I am

  • 0

I have this abstract factory pattern that I use in as3 that I am pretty used to and I am trying to implement it in C# but lack some knowledge, here how it goes.

    public class Factory{

        static public ID_LOGIC_CLASS:int = AddClassLink(MyLogicClass);

        static private classLib:Array;

        public Factory(){
            classLib = new Array();
        }


        static public AddClassLink(logicClass:Class):int {
             classLib.push(logicClass);
             return classLib.length-1;
        }

        static public CreateClassFromId(int id, 
                                  constructorData:DataObject):FactoryObject{
             var c:Class = classLib[id];
             var obj:FactoryObject = new c();
             obj.Init(constructorData);
             return obj;
        }
    }

This is the implementation I use and it is very good. It allows you to instantiate objects with a simple int and a data object that holds all of your constructor values.

Only thing that I am missing really is how to make a list of classes object in C# and how to create a new object of that class with a reference to the class type.

I think it could be done by using delegate’s constructors.

Tell me what you think.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:08:10+00:00Added an answer on May 28, 2026 at 1:08 pm

    Here’s a verbatim port:

          // Untested code
    public class Factory
    {
        private static int _id = 0;
    
        // Use Dictionary to store the mapping from int to Type
        // instead of ActionScript "classLib = new Array();"
        private static readonly IDictionary<int, Type> _typeMappings = new Dictionary<int, Type>();
    
        public Factory()
        {
        }
    
        public static int AddClassLink(Type type)
        {
            // To insure uniqueness, increment a static local id and use that
            int localId = ++_id;
    
            // Store the Type of the class to instantiate against the integer ID
            // Similar to ActionScript "classLib.push(logicClass); "
            _typeMappings[localId] = type;          // Set in dictionary
            return localId;
        }
    
        pubilc static FactoryObject CreateClassFromId(int id, DataObject constructorDataObject)
        {
            // Get the class type by integer Id. 
            // You can also use _typeMappings.Contains and _typeMappings.TryGetValue() to 
            // add some checking first
            var type = _typeMappings[id];
    
            // This is equivalent to new DerivedFactoryObject() where Type
            // is the runtime Type of the class registered in AddClassLink
            var obj = (FactoryObject)Activator.CreateInstance(type);
    
            // Initialise and return 
            obj.Init(constructorDataObject);
            return obj;
        }
    }
    

    In this example I’ve used a Dictionary<TKey, TValue instead of Array so that removals from the type dictionary don’t mess up the ID you are returning. In the above example, so long as AddClassLink is not called on multiple threads, you will get a unique ID every time, even if classes are deregistered.

    Regarding instantiation of a type, use the System.Type overload of Activator.CreateInstance. This can construct an object given its runtime type only.

    Regarding usage of this class, if you have a class you want to register with it you would use this notation

    var myFactory = new Factory();
    int id = myFactory.AddClassLink(typeof(SomeClassIWantInstantiated))
    // ... 
    SomeClassIWantInstantiated instance = myFactory.CreateClassFromId(id, new DataObject())
    

    The above should work (Disclaimer: Untested!) and should be an accurate port of your actionscript code. *That is not to say it is a good idea or best practices! *

    I recommend taking a look into Dependency Injection containers and looking at the Factory Pattern in .NET as the power of reflection, runtime typing etc allow this pattern to be implemented elegantly and robustly in C#. In particular, DI Containers will allow you to register and resolve a class as follows

    var myFactory = new DependencyInjectionContainerTm();
    myFactory.Register(typeof(SomeClassIWantInstantiated))
    // ... 
    SomeClassIWantInstantiated instance = myFactory.Resolve<SomeClassIWantInstantiated>();
    

    The main use of the container is to wire up dependencies and allow constructor injection as interfaces, leading to well decoupled, easily testable code, however it is essentially a typed object factory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard recently about the Abstract Factory Pattern and have currently some doubts on
I currently have some DAOs set up using the abstract factory pattern. It looks
I have to implement a factory method pattern in C++. The class (C) that
Having in mind the abstract factory pattern, imagine that you have a class hierarchy
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have a model that looks something like this: public abstract class Parent {
I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden
I have a requirement to create some objects that implement a given interface, where
This is another design pattern in some legacy code that I couldn't find much
I have an abstract factory registered for injection in some controller instances. Can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.