Abstract Factory, one of the well-known design patterns, I just made an example to record it, first class diagram
Code
NamespaceAbstractFactoryTest
{
ClassProgram
{
Static VoidMain (String[] Args)
{
//The specific factory type can be read from the configuration file
IFactory carFactory=CarFactory. CreateFacory ("ThreadTest. AudiFactory");
ICar car=CarFactory. CreateCar ();
Car. Run ();
}
}
Public ClassCarFactory
{
Public StaticIFactory CreateFacory (StringFactoryType)
{
Type t=Type. GetType (factoryType );
ReturnActivator. CreateInstance (t)AsIfacloud;
//Another method is to create the object factory object Assembly. Load (Assembly name). CreateInstance (namespace. Class Name)
//IFactory carFactory = Assembly. Load ("leleapplication2 ").CreateInstance (factoryType) as ifacloud;
//Return carFactory;
}
}
Public InterfaceIfacloud
{
ICar CreateCar ();
}
Public ClassAudiFactory: ifacloud
{
# RegionIfacloud Member
PublicICar CreateCar ()
{
Return NewAudiCar ();
}
# Endregion
}
Public ClassDasAutoFactory: ifacloud
{
# RegionIfacloud Member
PublicICar CreateCar ()
{
Return NewDasautoCar ();
}
# Endregion
}
Public InterfaceICar
{
VoidRun ();
}
Public ClassAudiCar: ICar
{
# RegionICar Member
Public VoidRun ()
{
Console. WriteLine ("Audi .........");
}
# Endregion
}
Public ClassDasautoCar: ICar
{
# RegionICar Member
Public VoidRun ()
{
Console. WriteLine ("Dasauto .............");
}
# Endregion
}
}