using System;
using System.Collections.Generic;
using System.Text;
namespace Nuclex.Support.Plugins {
#if !NO_CLONING
/// Factory that creates instances by cloning a prototype
/// Type of product created by the factory
/// Type of the prototype that will be cloned
public class PrototypeFactory :
IAbstractFactory, IAbstractFactory, IDisposable
where TProduct : class
where TConcreteType : class, ICloneable {
/// Initializes a new prototype based factory
/// Prototype instance that will be cloned
public PrototypeFactory(TConcreteType prototype) {
this.prototype = prototype;
}
///
/// Creates a new instance of the type to which the factory is specialized
///
/// The newly created instance
public TProduct CreateInstance() {
return (TProduct)this.prototype.Clone();
}
///
/// Creates a new instance of the type to which the factory is specialized
///
/// The newly created instance
object IAbstractFactory.CreateInstance() {
return this.prototype.Clone();
}
/// Immediately releases all resources owned by the instance
public void Dispose() {
if(this.prototype != null) {
IDisposable disposablePrototype = this.prototype as IDisposable;
if(disposablePrototype != null) {
disposablePrototype.Dispose();
}
this.prototype = null;
}
}
/// The prototype object
private TConcreteType prototype;
}
#endif // !NO_CLONING
} // namespace Nuclex.Support.Plugins