Pit design mode – factory method mode (Factory)

The factory method pattern is also called the factory pattern. It is one of 23 design patterns. It solves the problem of creating objects in software design. It can better solve the change of users’needs.

Problem: In the simple factory pattern, we put all the instantiated objects in Factory. CS (factory class), and we can instantiate objects in our predictions, but our predictions are limited, and the customer’s requirements are unlimited, so there is a problem, but the customer’s needs are too complex.Miscellaneous, we have to modify the source code, which is not allowed by the design mode.

Definition: in factory mode, the parent class is responsible for defining the interface to create objects, and the subclass is responsible for new specific objects.

 

Practical example: let’s take an example. This example is somewhat different from that of a simple factory.

  It is said that there was an emperor in the Qing Dynasty, who was very extravagant, and each dress had a palace maid to take charge of it. Thus, for each additional dress, one more palace maid would be required, but each of them had his or her own duties and did not interfere with each other.    

 

Analysis: The function can be realized, according to the needs of the emperor, to create a palace girl to take the corresponding clothes, if the emperor is too luxurious, this clothes have not, just need to add a palace girl can meet his needs, each palace girl as long as there is a kind of clothes (high cohesion), to increase clothes, for the original Palace girl andClothes, no one will affect who.

 

Commodity series

 

 Commodity interface ICoat.cs

public interface ICoat
{
        void ShowCoat();
}

Specific products: business jacket

 public class BusinessCoat : ICoat
    {
        public void ShowCoat()
        {
            Console.Write("This is a business coat.");
            //throw new NotImplementedException();
        }
    }

Specific products: Fashion tops

 public class BusinessFactory : IFactory
    {
        public ICoat CreateCoat()
        {
            return new BusinessCoat();
            ///throw new NotImplementedException();
        }
    }

 

The following is the factory series.

General workshop:

public interface IFactory
    {
        ICoat CreateCoat();
    }

Fashion jacket factory

public class FashionFactory : IFactory
    {
        public ICoat CreateCoat()
        {
            return new FashionCoat();
        }
    }

Business jackets factory

public class BusinessFactory : IFactory
    {
        public ICoat CreateCoat()
        {
            return new BusinessCoat();
            ///throw new NotImplementedException();
        }
    }

 

Emperor:

static void Main(string[] args)
        {
            BusinessCoat coat = new BusinessCoat();
            coat.ShowCoat();
            Console.ReadLine();
        }
 
Architecture diagram:

 

 
The factory method pattern uses multiple subclasses inherited from the abstract factory role instead of the “God class” in the simple factory pattern. As mentioned above, this shares the pressure on the object; and it makes the structure flexible — as long as there is an abstract product role, abstraction, when new products are producedThe contract provided by the factory role is generated so that it can be used by the customer without having to modify any existing code. It can be seen that the structure of the factory’s role is also in line with the principle of opening and closing.
 
Disadvantage: Adding a product involves adding a factory class, which makes the program architecture very responsible and makes it very difficult for the system to read.

Leave a Reply

Your email address will not be published. Required fields are marked *