Home > Backend Development > C#.Net Tutorial > A brief explanation of C# factory pattern

A brief explanation of C# factory pattern

巴扎黑
Release: 2017-09-06 14:56:37
Original
1319 people have browsed it

1. Simple Factory (Simple Factory) mode
Simple Factory mode returns an instance of one of several possible classes based on the data provided to it. Usually the class it returns has a common parent class and public methods.

Simple Factory pattern is not actually one of the 23 design patterns in GoF.
2. Simple Factory pattern roles and structures:
Factory class role Creator (LightSimpleFactory): The factory class creates product objects under the direct control of the client (Create method).
Abstract product role Product (Light): Defines the parent class of objects created by a simple factory or the interface they share. Can be a class, abstract class or interface.
Concrete product role ConcreteProduct (BulbLight, TubeLight): Define the specific objects processed by the factory.
3. Program example:

using System; 
public abstract class Light
{
   public abstract void TurnOn();
   public abstract void TurnOff();
}
 
public class BulbLight : Light
{
   public override void TurnOn()
    {
      Console.WriteLine("Bulb Light is Turned on");
   }
 
   public override void TurnOff()
    {
      Console.WriteLine("Bulb Light is Turned off");
   }
}
 
public class TubeLight : Light
{
   public override void TurnOn()
    {
      Console.WriteLine("Tube Light is Turned on");
   }
 
   public override void TurnOff()
    {
      Console.WriteLine("Tube Light is Turned off");
   }
}
 
public class LightSimpleFactory
{
   public Light Create(string LightType)
    {
      if(LightType == "Bulb")
         return new BulbLight();
      else if(LightType == "Tube")
         return new TubeLight();
      else
         return null;
   }
}
 
public class Client
{
   public static void Main()
    {
      LightSimpleFactory lsf = new LightSimpleFactory();
 
      Light l = lsf.Create("Bulb");
      l.TurnOn();
      l.TurnOff();
 
      Console.WriteLine("-----------------");
 
      l = lsf.Create("Tube");
      l.TurnOn();
      l.TurnOff();
   }
}
Copy after login

5. Advantages and disadvantages:
Advantages:
The factory class contains necessary judgment logic, which can decide when to create an instance of which product class. The client is relieved of the responsibility of creating product objects directly and merely "consumes" the product. The simple factory pattern achieves the separation of responsibilities through this approach.
Disadvantages:
When the product has a complex multi-layer hierarchical structure, the factory class only has itself, and it can adapt to changes in the same way, which is the shortcoming of the model. Because the factory class centralizes all product creation logic, once it fails to work properly, the entire system will be affected.
At the same time, it is difficult to expand the system. Once a new product is added, the factory logic has to be modified, which may cause the factory logic to be too complex.
In addition, the simple factory pattern usually uses static factory methods, which makes it impossible to be inherited by subclasses, causing the factory role to be unable to form a hierarchical structure based on inheritance.

The above is the detailed content of A brief explanation of C# factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template