[design pattern series] simple factory

Andy a Hui 2021-09-15 09:55:13

Factory mode

Factory mode : As the name suggests, it makes this class work like a factory , Production of products , Use it here , We are the production class . The factory model is production , Creating patterns for classes .

It provides a method to create objects in the parent class , Allow subclasses to determine the type of instantiated object .

Binding code to specific classes makes code more vulnerable , More inflexible , extensible , It's difficult to modify .

Programming to an interface , It can isolate a lot of changes that may happen to the system later , extensible .

Used to deal with change , And help us “ Find out what's going to change , Separate them from the same parts ”

Demo

[ Design pattern series ] Simple factory _ Code specification
For example, there is a logistics company , Before, the business was very single , Only do land logistics , as time goes on , Changes in the market , There's also maritime logistics , So how to design an implementation to deal with this uncertainty in business logic , If a kind of air logistics is added in the back , So how will it be designed .

How to only road logistics and maritime logistics , We can just declare it in a separate class , Back through if/eles The way to judge is the logistics , To achieve it . There is no problem in doing so , It can solve the problems facing the business , But it's not the best way to implement it in software development , If you pass if/eles To achieve , So their coupling is too high , Later, if the land logistics is modified , There are too many things that can lead to changes , And it's not easy to scale .

Simple implementation logic

 /// <summary>
/// Marine logistics
/// </summary>
public class AtSeaLogistics
{
/// <summary>
/// Shipping by sea
/// </summary>
public void SendSeaGoods(string goodName)
{
Console.WriteLine(" The sea Send:"+goodName);
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
 /// <summary>
/// Land logistics
/// </summary>
public class LandLogistics
{
/// <summary>
/// Land delivery
/// </summary>
public void SendLandGoods(string goodName)
{
Console.WriteLine(" land Send:"+goodName);
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
 static void Main(string[] args)
{
int logisticsType = 0; // The default is land transportation
Console.WriteLine(" Start shipping ");
if (logisticsType==0)
{
LandLogistics land = new LandLogistics();
land.SendLandGoods("Iphone 13");
}
else
{
AtSeaLogistics atSea = new AtSeaLogistics();
atSea.SendSeaGoods(" Sea fish ");
}
Console.WriteLine(" Delivery complete ");
Console.ReadKey();
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

After operation , Due to the default land delivery , Call the land delivery module , Make delivery .
[ Design pattern series ] Simple factory _C#_02
This is actually a very simple , It's also the most common way to write simple logic , There's no problem realizing the function , However, this kind of implementation has great hidden trouble and is not extensible , If a mode of transportation needs to be added later , It may have to be in Main() Method , The coupling between transportation mode and logistics is too high , And it's not easy to expand .

Here's an implementation using the simple factory pattern , Through the interface to achieve their own , They are responsible for their own affairs , Logistics companies don't need to know which way to deliver goods , Just deliver the goods to the delivery factory , The delivery factory decides which method to use according to the goods itself .

Factory mode

 public interface ISendInterface
{
void SendGoods(string goodName);
}

  • 1.
  • 2.
  • 3.
  • 4.
 /// <summary>
/// Land logistics
/// </summary>
public class LandLogistics:ISendInterface
{
/// <summary>
/// Land delivery
/// </summary>
public void SendGoods(string goodName)
{
Console.WriteLine(" land Send:" + goodName);
}
///// <summary>
///// Land delivery
///// </summary>
//public void SendLandGoods(string goodName)
//{
// Console.WriteLine(" land Send:"+goodName);
//}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
 /// <summary>
/// Marine logistics
/// </summary>
public class AtSeaLogistics:ISendInterface
{
/// <summary>
/// Shipping by sea
/// </summary>
public void SendGoods(string goodName)
{
Console.WriteLine(" The sea Send:" + goodName);
}
//public void SendSeaGoods(string goodName)
//{
// Console.WriteLine(" The sea Send:"+goodName);
//}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
 /// <summary>
/// Physical distribution management , Mainly used for delivery
/// </summary>
public class LogisticsStoreFactory
{
ISendInterface sendGood = null;
public ISendInterface GetSendInterface(int type)
{
switch (type)
{
case 1:
sendGood = new LandLogistics();
break;
case 2:
sendGood = new AtSeaLogistics();
break;
default:
break;
}
return sendGood;
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

Call implementation logic

 LogisticsStoreFactory logisticsStore = new LogisticsStoreFactory();
// land transportation
ISendInterface loadSend = logisticsStore.GetSendInterface(1);
loadSend.SendGoods("Iphone 13");
// sea transport
ISendInterface atSeaSend = logisticsStore.GetSendInterface(2);
atSeaSend.SendGoods(" Sea fish ");
Console.WriteLine(" Delivery complete ");
Console.ReadKey();

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Now we can see that , Simple factory in the process of implementation , There is no need to focus on how to achieve their own transportation , Only by LogisticsStoreFactory Factory class to get the implementation , Just use it as you like . So decoupling is achieved , Split the call and Implementation , There's no need to pay too much attention to how it's implemented , Just use it .

Applicable scenario

  • During coding , When you can't predict the object category and its specific dependencies , Can use simple factory .
  • Separate the creation and Implementation , Without affecting other products , You can create it yourself , Low coupling , Easy to expand .

Realization way

  • Define each product class , Let each class do its own work , No interference with each other .
  • It's a common interface , Let all products implement this interface .
  • Create an empty factory class , Let it have the ability to acquire products , And return this common interface .
  • Find a reference to the product constructor in the creator code , Change it to a reference to the factory method , At the same time, migrate the code that created the product to the factory method .

Little message

A man's struggle , It's like being pregnant , It's been a long time , It will always be seen .

Life is short , I don't want to pursue what I can't see , I just want to catch what I can see .

I am a A Hui , Thank you for reading , If it helps you , Please like it , forward thank you .

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15