Brief introduction of three factory modes : Simple factory model , Factory approach model , Abstract factory pattern
1. Simple factory model
This is a small factory , Everything is made by ourselves , Others call their own tools to produce what they want
It has three characteristics : Specific factory A, Specific production tools B, Abstract production object C
namely A Call tool B To produce C, There can be a variety of tools ( I became a class B tool ), Corresponding products for each production ( class C product ,C Is the parent interface of all products ), Let's look at the code
// Abstract product role : Vehicle class C public abstract class Car { public virtual void GoToWork() {
} }// Specific product roles : Bicycle tool B public class Bike : Car { public override void GoToWork() {
Console.WriteLine(" Go to work by bike "); } } // Specific product roles : bus tool B public class Bus : Car {
public override void GoToWork() { Console.WriteLine(" Go to work by bus "); } } // Factory role : Simple factory class
public class SimpleFactory { public static Car CreateSimple(string str) { Car
simple =null; switch (str) { case "Bike": simple = new Bike(); break; case "Bus"
: simple =new Bus(); break; //…… } return simple; } } Car car =
SimpleFactory.CreateSimple("Bus"); car.GoToWork();
2. Factory approach model
It's like a company growing up , More business is involved , Management is not convenient , At this time, it often becomes a subsidiary , Let each subsidiary be responsible for a piece of business , I call it the parent company of the management subsidiary , It doesn't involve specific business ( abstract )
So there are four Characters : parent company -> Abstract factory , subsidiary -> Specific factory , Products -> Abstract interface , Production tools -> Specific methods
// Abstract product role : Vehicle public abstract class Car { public virtual void GoToWork() { } }
// Specific product roles : Bicycle public class Bike : Car { public override void GoToWork() {
Console.WriteLine(" Go to work by bike "); } } // Specific product roles : bus public class Bus : Car { public
override void GoToWork() { Console.WriteLine(" Go to work by bus "); } } // Abstract factory role : Factory interface
public interface IFactory { Car ByWhatWay(); } // Specific factory category : Bicycles public class
BikeFactory : IFactory {public Car ByWhatWay() { return new Bike(); } }
// Specific factory category : Buses public class BusFactory : IFactory { public Car ByWhatWay() {
return new Bus(); } } // How to go to work depends on the factory instantiated by the client IFactory factory = new BusFactory();
// Conventional writing Car car = factory.ByWhatWay(); car.GoToWork();
3. Abstract factory pattern , This pattern is a combination of the previous models , That is to say, it has become bigger , Became the parent company , And the subsidiary company has also become bigger , Can do a lot of business . Looking at the overall situation , It's the factory approach model , From the local point of view , It's a simple factory model , That's it. , Look at the code directly
// Abstract vehicle public abstract class Car { // name public string CarName{ get; set; }
public virtual string GoToWork() { return CarName; } } // Abstract breakfast class public abstract
class BreakFast { // Name of breakfast public string FoodName { get; set; } public virtual
string Eat() { return FoodName; } } // Bicycle public class Bike : Car { public
override string GoToWork() { CarName = " Riding a bicycle "; return CarName; } } // bus public
class Suv: Car { public override string GoToWork() { CarName = " It's on SUV automobile "; return
CarName; } }// Breakfast by Gao Baoge : Sandwich milk public class SandWichAndMilk : BreakFast { public
override string Eat() { FoodName = " Sandwiches and milk "; return FoodName; } } // Forced breakfast : Steamed bun and soybean milk
public class BaoziAndDoujiang : BreakFast { public override string Eat() {
FoodName =" Steamed bun and soybean milk "; return FoodName; } } // Abstract factory class public abstract class
AbstractFactory {// Methods of creating vehicles public abstract Car CreatCar(); // Create breakfast method public
abstract BreakFast CreateBreakFast(); } // Specific factory category : Night Programmers public class LowFactory :
AbstractFactory {public override Car CreatCar() { return new Bike(); } public
override BreakFast CreateBreakFast() { return new BaoziAndDoujiang(); } }
// Specific factory category : High power programmer public class HighFactory : AbstractFactory { public override Car
CreatCar() { return new Suv(); } public override BreakFast CreateBreakFast() {
return new SandWichAndMilk(); } } public class CoderLove { private Car car;
private BreakFast breakFast; public CoderLove (AbstractFactory fac) {
car=fac.CreatCar(); breakFast=fac.CreateBreakFast(); }public void GetCoderLove
() { Console.WriteLine(" After breakfast "+breakFast.Eat()+","+car.GoToWork()+" Go to work "); } } //
Choose which subsidiary to send AbstractFactory factory=new HighFactory(); // Subsidiaries and business contacts CoderLove
coder=new CoderLove(factory); // work coder.GetCoderLove();
The abstract factory pattern isolates the generation of concrete classes , So that customers don't need to know what is being created .
Because of this isolation , It is relatively easy to replace a specific factory . All concrete factories implement the common interfaces defined in the abstract factory , Therefore, it is only necessary to change the specific factory examples , It can change the behavior of the whole software system to some extent .
In addition to the above hard pressed programmers , High power programmer , You can also add a specific drive SUV Specific factories eating steamed bun and soybean milk , You can also add specific factories that ride bicycles and eat sandwich milk , Change to a different subsidiary , There are different results
Technology