Transferred from: Model-engineering implementation and expansion (Design Mode C)
Http://www.cnblogs.com/callwangxiang/
Modify the one completed at the end of this chapter that is suitable for engineering applications.Factory typeTo support constructors with Parameters
Analysis:
1. In line with the OCP principle, we do not modify the original factory <t>, but extend it through inheritance.
2. continue to use the activator provided by. NET Framework to provide support for constructors with parameters.
3. to distinguish it from the method signature of the original factory <t>, We must explicitly declareObject []Is used as the constructor parameter.
Reference answer
1. To avoid ambiguity, first modify the referenced namespace
Using System;
Using F = Marvellousworks. practicalpattern. factorymethod;
Using Microsoft. visualstudio. testtools. unittesting;
2. define new factory interfaces and specific factory types
Interface Ifacloud: F. ifacloud
{
# Region Factory method
Ttarget create < Ttarget > ( Object [] Parametes );
Ttarget create < Ttarget > ( String Name, Object [] Parametes );
# Endregion
}
Class Factory: F. Factory, ifacloud
{
Public Ttarget create < Ttarget > ( Object [] Parametes)
{
Return(Ttarget) activator. createinstance (registry [Typeof(Ttarget)], parametes );
}
Public ttarget create ttarget > ( string name, Object [] parametes)
{< br> return (ttarget) activator. createinstance (registry [ typeof (ttarget), name], parametes);
}< BR >}
3. Write unit test verification
[Testmethod]
Public Void Createinstance ()
{
VaR Factory = New Factory ()
. Registertype < Ifruit, Apple > ()
. Registertype < Ifruit, orange > ( " O " )
. Registertype < Ivehicle, bicycle > ()
. Registertype < Ivehicle, bicycle > ( " A " )
. Registertype < Ivehicle, train > ( " B " )
. Registertype < Ivehicle, car > ( " C " )
. Registertype < Ientry, entrywithname > ()
. Registertype < Ientry, entrywithname > ( " N " )
. Registertype < Ientry, entrywithnameandageandtitle > ( " Nat " );
# RegionNo parameter type for the constructor
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 ));
# Endregion
# RegionType of the constructor with Parameters
//Convert to the new extended interface form
VaR F=(Ifacloud) factory;
// Use extended features
VaR E1 = F. Create < Ientry > ( New Object [] { " Joe " });
Assert. isinstanceoftype (E1, Typeof (Entrywithname ));
Assert. areequal < String > ( " Joe " , (Entrywithname) E1). Name );
VaR E2 = F. Create < Ientry > ( " Nat " , New Object [] { " Joe " , 20 });
Assert. isinstanceoftype (E2, Typeof (Entrywithnameandageandtitle ));
Assert. areequal < String > ( " Joe " , (Entrywithname) E2). Name );
Assert. areequal < Int > ( 20 , (Entrywithnameandageandtitle) E2). Age );
Assert. areequal < String > (Entrywithnameandageandtitle. defaulttitle, (entrywithnameandageandtitle) E2). Title );
# Endregion
}