“This article is on the second day of the month watch. For me, this is a challenge. Although I have read design mode for a long time, I have returned it to the book.

  • 📢 welcome to like: 👍 collect ⭐ message 📝 If there is any mistake please correct it, send roses, hand left fragrance!
  • 📢 This article is written by Webmote.
  • 📢 author’s motto: life lies in tossing about, when you do not toss about life, life will start tossing you, let us work together! 💪 💪 💪

Preface 🎏

Pattern: The word pattern can be understood as routine, much like the three batting axe of Cheng Biting gold, according to this routine can get something done. Design patterns: Software/architecture design routines, the most popular ones.

. Some of the following words are omitted and can be referred to the article of day 1.

🎏 01. Explanation of the factory method pattern

Intent: Define an abstract method or interface that delays the creation of an object to a subclass, allowing the subclass to decide what kind of product object to produce.

Problem domain: It is generally used to solve the following problems.

  • You still want to use it when you don’t know the details of the object you created
  • The details of a class are specified by its subclasses
  • The created object can be implemented by extension, but some of the details are predictable.

Solution: We use UML diagrams to describe this.

As can be seen from the figure, Creator defines an abstract interface, and the factory method is implemented by ConcreateCreator to produce the final object ConcreateDestObject.

There can be many implementations of Creator, and the resulting object implementation can have different DestObject subclasses.

Effect:

  • Benefits:
  1. The ConcreateDestObject class implementation can be decoupled in certain scenarios;
  2. More in line with the open closed principle;
  3. One factory doesn’t need to know about the other;
  4. The client knows exactly which object will be created;
  • Limit:
  1. If the subclass construction is relatively simple, then increasing the subclass increases the workload, can be reduced to a factory method with parameters, according to different parameters, return different prefab;

🎏 02. Dotnet Core source code appreciation

Factory method pattern is adopted in HashAlgorithm construction in algorithm implementation.

Here’s how we use these subclasses to construct HashAlgorithm

HashAlgorithm hashAlgorithm = null;
    switch (type)
    {
        case HashType.MD5:
            hashAlgorithm = MD5.Create();
            break;
        case HashType.SHA1:
            hashAlgorithm = SHA1.Create();
            break; }}Copy the code

Its factory method interface is defined as Create, where Create interface returns concrete class, which is inherited from HashAlgorithm, and finally generates prefab.

🎏 03.Dotnet generator implementation

Here is an example of implementing a vehicle factory method. The interface is defined as follows:

One type of vehicle is decided by the user. We have three different factories, which produce different vehicles. Of course, the production process may be different, but since the factories are involved, we don’t worry about the details.

 public abstract class Vehicle  
{  
    internal int capacity;  
    public abstract string GetData();  
  
    public int GetCapacity()  
    {  
        return capacity;  
    }  
    public void AddPassengers(int passengers)  
    {  
        if (capacity < passengers)  
        {  
            throw new Exception(this.GetData() + " reached max capacity");  
        }  
        elsecapacity -= passengers; }}public abstract class VehicleCreator  
{  
    protected abstract Vehicle MakeVehicle();  
  
    public Vehicle CreateVehicle()  
    {  
        return this.MakeVehicle(); }}public class BoatCreator : VehicleCreator  
{  
    protected override Vehicle MakeVehicle()  
    {  
        Vehicle vehicle = new Boat();  
        vehicle.capacity = 150;  
        returnvehicle; }}Copy the code

The implementation is relatively simple, so I won’t list it here, and will put it on Github later.

Callers, which can be used directly by type of call.

switch (vehicleType) { case VehicleType.Boat: vehicle = new BoatCreator().CreateVehicle(); break; . default: vehicle = null; break; }Copy the code

🎏 04. Summary

Yes, I feel that I lost an article. I got off work late today and wrote too late. I feel that I can’t make an article without 2-3 hours.

Important, no gift, no power! Haha, come on!

To develop a good habit requires constant encouragement and encouragement. Writing ability may be improved through continuous writing, and of course, my own capacity. In the process of continuous output, I find my own deficiencies and consolidate my knowledge.

30 days non-stop, the goal is very ambitious, today is the second day, come on, brothers!

Routine summary, rational view!

Knot is what, knot is I want you to like but can not get the loneliness. 😳 😳 😳

👓 all see this, still care to point a like?

👓 all points like, still care about a collection?

Do you care about a comment when you have collected 👓?