This is one of the most common design patterns used throughout software design.
The pattern has interchangeable child classes that derive from an abstract base class or an interface. The interface tells those classes what it is expecting for method signatures and properties.
public class Evaluate
{
public void SubmitForm()
{
List<IProduct> products = new List<IProduct>();
string productToEval = "premium";
switch (productToEval)
{
case "standard":
products.Add(new StandardProduct());
break;
case "premium":
products.Add(new PremiumProduct());
break;
case "substandard":
products.Add(new SubStandardCentralProduct());
products.Add(new SubStandardRemoteProduct());
break;
case "distributed":
products.Add(new DistributedCentralProduct());
products.Add(new DistributedRemoteProduct());
break;
}
products.Add(new IntegratedProduct(false));
products.Add(new ExternalProduct(false));
products.Add(new DiscontinuedProduct(false));
}
}
public interface IProduct
{
string BuildNumber { get; set; }
string ProductName { get; set; }
void AddLicense(UserInfo userInfo, string parentSerialNumber);
int AddLicenseStatus(UserInfo userInfo);
void AddLeadIntoCRM(UserInfo userInfo, string parentSerial);
}
public class PremiumProduct : IProduct
{
private ILicense _license;
private ILogging _logging;
public PremiumProduct()
{
_license = new ParentLicense();
_logging = new ParentLogging();
}
public virtual void AddLicense(UserInfo userInfo, string parentSerialNumber)
{
_license.AddLicense(this, userInfo, parentSerialNumber);
}
public virtual int AddLicenseStatus(UserInfo userInfo)
{
return _license.AddLicenseStatus(this, userInfo);
}
public virtual void AddLeadIntoCRM(UserInfo userInfo, string parentSerial)
{
_logging.AddLeadIntoCRM(this, userInfo, parentSerial);
}
public virtual string BuildNumber
{
get { return WebConfigurationManager.AppSettings["Premium_Build_Number"]; }
set { }
}
public virtual string ProductName
{
get { return WebConfigurationManager.AppSettings["Premium_Product_Name"]; }
set { }
}
}
public class SubStandardRemoteProduct : IProduct
{
private ILicense _license;
private ILogging _logging;
public SubStandardRemoteProduct()
{
_license = new RemoteLicense();
_logging = new RemoteLogging();
}
public virtual void AddLicense(UserInfo userInfo, string parentSerialNumber)
{
_license.AddLicense(this, userInfo, parentSerialNumber);
}
public virtual int AddLicenseStatus(UserInfo userInfo)
{
return _license.AddLicenseStatus(this, userInfo);
}
public virtual void AddLeadIntoCRM(UserInfo userInfo, string parentSerial)
{
_logging.AddLeadIntoCRM(this, userInfo, parentSerial);
}
public virtual string BuildNumber
{
get { return WebConfigurationManager.AppSettings["SubStandard_Remote_Build_Number"]; }
set { }
}
public virtual string ProductName
{
get { return WebConfigurationManager.AppSettings["SubStandard_Remote_Product_Name"]; }
set { }
}
}
public class ExternalProduct : IProduct
{
private ILicense _license;
private ILogging _logging;
private bool _logLead = false;
public ExternalProduct()
{
_license = new PluginLicense();
_logging = new PluginLogging();
}
public ExternalProduct(bool logLead)
{
_license = new PluginLicense();
_logging = new PluginLogging();
_logLead = logLead;
}
public virtual void AddLicense(UserInfo userInfo, string parentSerialNumber)
{
_license.AddLicense(this, userInfo, parentSerialNumber);
}
public virtual int AddLicenseStatus(UserInfo userInfo)
{
return _license.AddLicenseStatus(this, userInfo);
}
public virtual void AddLeadIntoCRM(UserInfo userInfo, string parentSerial)
{
if (_logLead)
_logging.AddLeadIntoCRM(this, userInfo, parentSerial);
}
public virtual string BuildNumber
{
get { return WebConfigurationManager.AppSettings["External_Build_Number"]; }
set { }
}
public virtual string ProductName
{
get { return WebConfigurationManager.AppSettings["External_Product_Name"]; }
set { }
}
}