Here is an example of better understanding:
1. If a back garden only grows vegetables, you can use a simple factory.
2. If there are a wide variety of vegetables in the back garden, you must use the factory method to abstract the common things.
3. if we want to expand the scale of the back garden, such as one in the north and one in the south, the factory method will not be implemented, so we should use abstract factories to put all kinds of plants, it also forms a back garden.
Therefore, I personally think that a simple factory is a factory that produces only one type of products and faces specific classes. The factory method can produce different products and abstract public methods, then create a variety of products. abstract Factory separates several products into common items and abstracts mutually dependent objects. as long as these interfaces are implemented, different products can be obtained.
Example:
1. Simple Factory:
Copy codeThe Code is as follows: using System;
Public interface ICar
{
Void run ();
}
Public class BMWCar: ICar
{
Public void run ()
{
Console. WriteLine ("BMWCar run ");
}
}
Public class BenzCar: ICar
{
Public void run ()
{
Console. WriteLine ("BenzCar run ");
}
}
Public class Driver
{
Public static ICar DriverCar (string carType)
{
Switch (carType)
{
Case "BMWCar": return new BMWCar ();
Case "BenzCar": return new BenzCar ();
Default: throw new Exception ();
}
}
}
Public class Client
{
Public static void Main ()
{
ICar myCar = Driver. DriverCar ("BenzCar ");
MyCar. run ();
Console. Read ();
}
}
Experience: The advantage is that different vehicle running methods can be implemented only by implementing the common interface, but the disadvantage is to determine which type of vehicle, resulting in the need to modify the Driver class.
2. Factory method:Copy codeThe Code is as follows: using System;
Public interface ICar
{
Void run ();
}
Public class BMWCar: ICar
{
Public void run ()
{
Console. WriteLine ("BMWCar run ");
}
}
Public class BenzCar: ICar
{
Public void run ()
{
Console. WriteLine ("BenzCar run ");
}
}
Public abstract class Driver
{
Public abstract ICar DriverCar ();
}
Public class BMWDriver: Driver
{
Public override ICar DriverCar ()
{
Return new BMWCar ();
}
}
Public class BenzDriver: Driver
{
Public override ICar DriverCar ()
{
Return new BenzCar ();
}
}
Class Client
{
Public static void Main ()
{
Driver myDriver = new BenzDriver ();
ICar myCar = myDriver. DriverCar ();
MyCar. run ();
Console. Read ();
}
}
Experience: The advantage is that it complies with the open-closed principle (OCP) and does not show any disadvantages as a whole.
3. Abstract Factory:
Copy codeThe Code is as follows: using System;
Public interface IBusinessCar
{
Void run ();
}
Public interface ISportCar
{
Void run ();
}
Public class BMWBusinessCar: IBusinessCar
{
Public void run ()
{
Console. WriteLine ("BMWCar run ");
}
}
Public class BenzBusinessCar: IBusinessCar
{
Public void run ()
{
Console. WriteLine ("BenzBusinessCar run ");
}
}
Public class BMWSportCar: ISportCar
{
Public void run ()
{
Console. WriteLine ("BMWSportCar run ");
}
}
Public class BenzSportCar: ISportCar
{
Public void run ()
{
Console. WriteLine ("BenzSportCar run ");
}
}
Public interface IDriver
{
IBusinessCar BusinessCarDriver ();
ISportCar SportCarDriver ();
}
Public class BMWDriver: IDriver
{
Public IBusinessCar BusinessCarDriver ()
{
Return new BMWBusinessCar ();
}
Public ISportCar SportCarDriver ()
{
Return new BMWSportCar ();
}
}
Public class BenzDriver: IDriver
{
Public IBusinessCar BusinessCarDriver ()
{
Return new BenzBusinessCar ();
}
Public ISportCar SportCarDriver ()
{
Return new BenzSportCar ();
}
}
Class Client
{
Public static void Main ()
{
IDriver myDriver = new BenzDriver ();
ISportCar myCar = myDriver. SportCarDriver ();
MyCar. run ();
Console. Read ();
}
}
Experience: abstract methods seem to have reached the perfect realm. abstract The public methods of drivers and drivers of BMW, and create different classes for different drivers. Then, you can add drivers of different cars as needed. the only thing they have in common is driving.