C # typical factory implementation example

Source: Internet
Author: User

Factory Interface Definition

Copy codeThe Code is as follows: // <summary>
/// Factory Interface Definition
/// </Summary>
/// <Remarks>
/// TTarget: abstract product type
/// TSource: concrete product type
/// </Remarks>
Public interface ifacloud
{
# Region config and register type mapping

/// <Summary>
/// If you need to load the ing relationships defined in the configuration file at the same time, you can define an independent configuration type according to the SRP principle.
/// This configuration type calls these two interfaces to load configuration information for the Factory
/// </Summary>

IFactory RegisterType <TTarget, TSource> (); // fluent interface
IFactory RegisterType <TTarget, TSource> (string name); // fluent interface

# Endregion

# Region factory method

TTarget Create <TTarget> ();
TTarget Create <TTarget> (string name );

# Endregion
}

Registration type

Copy codeThe Code is as follows: public sealed class TypeRegistry
{
Readonly string DefaultNmae = Guid. NewGuid (). ToString ();
IDictionary <Type, IDictionary <string, Type> registry = new Dictionary <Type, IDictionary <string, Type> ();
Public void RegisterType (Type targetType, Type sourceType)
{
RegisterType (targetType, sourceType, DefaultNmae );
}
Public void RegisterType (Type targetType, Type sourceType, string name)
{
If (targetType = null) throw new ArgumentNullException ("targetType ");
If (sourceType = null) throw new ArgumentNullException ("sourceType ");
If (string. IsNullOrEmpty (name) throw new ArgumentNullException ("name ");
IDictionary <string, Type> subDictionary;

If (! Registry. TryGetValue (targetType, out subDictionary ))
{
SubDictionary = new Dictionary <string, Type> ();
SubDictionary. Add (name, sourceType );
Registry. Add (targetType, subDictionary );
}
Else
{
If (subDictionary. ContainsKey (name ))
Throw new DuplicateKeyException (name );
SubDictionary. Add (name, sourceType );
}
}
Public Type this [Type targetType, string name]
{
Get
{
If (targetType = null) throw new ArgumentNullException ("targetType ");
If (string. IsNullOrEmpty (name) throw new ArgumentNullException ("name ");
If (registry. Count () = 0)
Return null;

Return (registry
. Where (x => x. Key = targetType). FirstOrDefault (). Value
. Where (x => string. Equals (name, x. Key ))
. FirstOrDefault (). Value;
}
}

Public Type this [Type targetType]
{
Get {return this [targetType, DefaultNmae];}
}

}

Factory

Copy codeThe Code is as follows: public class Factory: ifacloud
{
Protected TypeRegistry registry = new TypeRegistry ();

# Region IFactory Members

Public IFactory RegisterType <TTarget, TSource> ()
{
Registry. RegisterType (typeof (TTarget), typeof (TSource ));
Return this;
}

Public IFactory RegisterType <TTarget, TSource> (string name)
{
Registry. RegisterType (typeof (TTarget), typeof (TSource), name );
Return this;
}

Public TTarget Create <TTarget> ()
{
Return (TTarget) Activator. CreateInstance (registry [typeof (TTarget)]);
}

Public TTarget Create <TTarget> (string name)
{
Return (TTarget) Activator. CreateInstance (registry [typeof (TTarget), name]);
}

# Endregion
}

Call

Copy codeThe Code is as follows: [TestMethod]
Public void CreateInstance ()
{
Var factory = new Factory ()
. RegisterType <IFruit, Apple> ()
. RegisterType <IFruit, Orange> ("o ")
. RegisterType <IVehicle, Bicycle> ()
. RegisterType <IVehicle, Bicycle> ("")
. RegisterType <IVehicle, Train> ("B ")
. RegisterType <IVehicle, Car> ("c ");

Assert. IsInstanceOfType (factory. Create <IFruit> (), typeof (Apple ));
Assert. IsInstanceOfType (factory. Create <IFruit> ("o"), typeof (Orange ));

Assert. IsInstanceOfType (factory. Create <IVehicle> (), typeof (Bicycle ));
Assert. IsInstanceOfType (factory. Create <IVehicle> ("a"), typeof (Bicycle ));
Assert. IsInstanceOfType (factory. Create <IVehicle> ("B"), typeof (Train ));
Assert. IsInstanceOfType (factory. Create <IVehicle> ("c"), typeof (Car ));
}

In fact, the essence is that a similar assembly function of the registration class is encapsulated in a dictionary mode, and then compared and implemented through generics, or a new instantiation is realized through passing parameters in the configuration file.

It must be consistent interface, generic, and other operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.