Removed ConcreteType property from IAbstractFactory<> interface (it was a really strange decision to add it in the first place, luckily it hasn't crept anywhere so far); provided a non-generic abstract factory interface; the factories returned by the factory employer now implement the non-generic factory interface
git-svn-id: file:///srv/devel/repo-conversion/nusu@139 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
41dcfa34d0
commit
4e2beb71b8
|
@ -69,6 +69,23 @@ namespace Nuclex.Support.Plugins {
|
||||||
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
|
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the factory employer can use the non-generic IAbstractFactory
|
||||||
|
/// interface instead of its generic variant
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestNonGenericCreateInstance() {
|
||||||
|
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
|
||||||
|
testEmployer.Employ(typeof(Derived));
|
||||||
|
|
||||||
|
Assert.That(testEmployer.Factories.Count, Is.AtLeast(1));
|
||||||
|
|
||||||
|
IAbstractFactory factory = testEmployer.Factories[0] as IAbstractFactory;
|
||||||
|
Assert.IsNotNull(factory);
|
||||||
|
|
||||||
|
Assert.IsInstanceOf<Derived>(factory.CreateInstance());
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Tests whether the factory employer throws an exception when it is asked to
|
/// Tests whether the factory employer throws an exception when it is asked to
|
||||||
/// employ an abstract class
|
/// employ an abstract class
|
||||||
|
@ -105,7 +122,6 @@ namespace Nuclex.Support.Plugins {
|
||||||
testEmployer.Employ(typeof(Derived));
|
testEmployer.Employ(typeof(Derived));
|
||||||
|
|
||||||
Assert.AreEqual(1, testEmployer.Factories.Count);
|
Assert.AreEqual(1, testEmployer.Factories.Count);
|
||||||
Assert.AreEqual(typeof(Derived), testEmployer.Factories[0].ConcreteType);
|
|
||||||
Assert.IsInstanceOf<Derived>(testEmployer.Factories[0].CreateInstance());
|
Assert.IsInstanceOf<Derived>(testEmployer.Factories[0].CreateInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +136,6 @@ namespace Nuclex.Support.Plugins {
|
||||||
testEmployer.Employ(typeof(Unrelated));
|
testEmployer.Employ(typeof(Unrelated));
|
||||||
|
|
||||||
Assert.AreEqual(1, testEmployer.Factories.Count);
|
Assert.AreEqual(1, testEmployer.Factories.Count);
|
||||||
Assert.AreEqual(typeof(Unrelated), testEmployer.Factories[0].ConcreteType);
|
|
||||||
Assert.IsInstanceOf<Unrelated>(testEmployer.Factories[0].CreateInstance());
|
Assert.IsInstanceOf<Unrelated>(testEmployer.Factories[0].CreateInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,31 +41,33 @@ namespace Nuclex.Support.Plugins {
|
||||||
/// a human-readable name, capabilities or an icon.
|
/// a human-readable name, capabilities or an icon.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public class FactoryEmployer<ProductType> : Employer
|
public class FactoryEmployer<ProductType> : Employer where ProductType : class {
|
||||||
where ProductType : class {
|
|
||||||
|
|
||||||
#region class ConcreteFactory
|
#region class ConcreteFactory
|
||||||
|
|
||||||
/// <summary>Concrete factory for the types in a plugin assembly</summary>
|
/// <summary>Concrete factory for the types in a plugin assembly</summary>
|
||||||
private class ConcreteFactory : IAbstractFactory<ProductType> {
|
private class ConcreteFactory : IAbstractFactory<ProductType>, IAbstractFactory {
|
||||||
|
|
||||||
/// <summary>Initializes a factory and configures it for the specified product</summary>
|
/// <summary>
|
||||||
|
/// Initializes a factory and configures it for the specified product
|
||||||
|
/// </summary>
|
||||||
/// <param name="type">Type of which the factory creates instances</param>
|
/// <param name="type">Type of which the factory creates instances</param>
|
||||||
public ConcreteFactory(Type type) {
|
public ConcreteFactory(Type type) {
|
||||||
this.concreteType = type;
|
this.concreteType = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The concrete type as produced by the factory</summary>
|
/// <summary>Create a new instance of the type the factory is configured to</summary>
|
||||||
public Type ConcreteType {
|
|
||||||
get { return this.concreteType; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Create a new instance of the type that the factory is configured to</summary>
|
|
||||||
/// <returns>The newly created instance</returns>
|
/// <returns>The newly created instance</returns>
|
||||||
public ProductType CreateInstance() {
|
public ProductType CreateInstance() {
|
||||||
return (ProductType)Activator.CreateInstance(this.concreteType);
|
return (ProductType)Activator.CreateInstance(this.concreteType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Create a new instance of the type the factory is configured to</summary>
|
||||||
|
/// <returns>The newly created instance</returns>
|
||||||
|
object IAbstractFactory.CreateInstance() {
|
||||||
|
return Activator.CreateInstance(this.concreteType);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Concrete product which the factory instance creates</summary>
|
/// <summary>Concrete product which the factory instance creates</summary>
|
||||||
private Type concreteType;
|
private Type concreteType;
|
||||||
|
|
||||||
|
|
|
@ -24,13 +24,25 @@ using System.Collections.Generic;
|
||||||
namespace Nuclex.Support.Plugins {
|
namespace Nuclex.Support.Plugins {
|
||||||
|
|
||||||
/// <summary>Abstract factory for a concrete type</summary>
|
/// <summary>Abstract factory for a concrete type</summary>
|
||||||
/// <typeparam name="ProductType">Interface or base class of the product of the factory</typeparam>
|
public interface IAbstractFactory {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of the type to which the factory is specialized
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The newly created instance</returns>
|
||||||
|
object CreateInstance();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Abstract factory for a concrete type</summary>
|
||||||
|
/// <typeparam name="ProductType">
|
||||||
|
/// Interface or base class of the product of the factory
|
||||||
|
/// </typeparam>
|
||||||
public interface IAbstractFactory<ProductType> {
|
public interface IAbstractFactory<ProductType> {
|
||||||
|
|
||||||
/// <summary>The concrete type as implemented by the factory instance</summary>
|
/// <summary>
|
||||||
Type ConcreteType { get; }
|
/// Creates a new instance of the type to which the factory is specialized
|
||||||
|
/// </summary>
|
||||||
/// <summary>Creates a new instance of the type to which the factory is specialized</summary>
|
|
||||||
/// <returns>The newly created instance</returns>
|
/// <returns>The newly created instance</returns>
|
||||||
ProductType CreateInstance();
|
ProductType CreateInstance();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user