Introduction to the simple factory pattern:
The simple factory pattern is a creational pattern, also called the static factory method (Static Factory Method) pattern, but it is not one of the 23 GOF design patterns. The simple factory pattern uses a factory object to decide which product class instance to create. The simple factory pattern is the simplest and most practical pattern in the factory pattern family and can be understood as a special implementation of different factory patterns.
Structural pattern diagram:
Role classification:
Factory (Creator) role
The core of the simple factory pattern, it is responsible for implementing the internal logic of creating all instances. The method of creating a product class in the factory class can be directly called by the outside world to create the required product object.
Abstract Product Role
The parent class of all objects created by the simple factory pattern. It is responsible for describing the public interface common to all instances.
The Concrete Product role
is the creation target of the simple factory pattern. All objects created are instances of a specific class that plays this role.
Introducing the actual situation:
If there is a tenant management system, the tenant types in it are variable, and the rent calculation formula for each tenant type is different
The rent amount of type A tenant = number of days * unit price + Performance*0.005
B-type household rent amount = month*(monthly price+performance*0.001)
Analysis:
1. There is a common calculation method for stores. This is the behavior of physical stores, but their behavior is different. The method is different, so we abstract the store class, the code is as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleFactory.App.IFactroy { public interface Ishop { double Getrent(int days, double dayprice, double performance); } }
2. After abstracting the store, we need to create a specific product class, here is the specific type store, which implements the behavior method of the store . Create a store of type A
using SimpleFactory.App.IFactroy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleFactory.App.product { //A类型的商店的创建 public class Ashop:Ishop { /// <summary> /// /// A类型商店租金额,天数*单价+绩效*0.005 /// </summary> /// <param name="days">天数</param> /// <param name="dayprice">每天单价</param> /// <param name="performance">日平均绩效</param> /// <returns></returns> public double Getrent(int days, double dayprice, double performance) { Console.WriteLine("A商店的租金算法"); return days * dayprice + performance * 0.01; } } }
3. Create a store of type B:
using SimpleFactory.App.IFactroy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleFactory.App.product { /// <summary> /// B类型的商店的创建 /// </summary> public class Bshop:Ishop { /// <summary> /// B类型商店的租金=月份*(每月价格+performance*0.001) /// </summary> /// <param name="month">月数</param> /// <param name="monthprice">月单价</param> /// <param name="performance">月平均绩效</param> /// <returns></returns> public double Getrent(int month, double monthprice, double performance) { Console.WriteLine("B商店的租金算法"); return month * (monthprice + performance * 0.001); } } }
4. After creating a type store and implementing the method, think about how to create that kind of object under what circumstances, So the core part of the simple factory pattern: the factory class came out
using SimpleFactory.App.IFactroy; using SimpleFactory.App.product; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleFactory.App.factoryMethod { public class factorymethod { public Ishop CreateShow(string show) { switch (show.Trim().ToLower()) { case"ashop": return new Ashop(); case "bshop": return new Ashop(); default: throw new Exception("该商店不存在"); } } } }
5. Then based on the current store type, it is judged which algorithm should be used for this type of store:
using SimpleFactory.App.factoryMethod; using SimpleFactory.App.IFactroy; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SimpleFactory.App { class Program { static void Main(string[] args) { Ishop As; factorymethod afm = new factorymethod(); As = afm.CreateShow("ashop"); //a 类型的某商店 double total = As.Getrent(30, 300, 2000); //30 天/100元 日平均绩效为2000 Console.WriteLine("该A类型商店的租金为:" + total); Console.WriteLine("============="); Ishop Bs; factorymethod bfm = new factorymethod(); Bs = bfm.CreateShow("bshop"); //b 类型的某商店 total = Bs.Getrent(3, 3000, 60000); //3 月/4000元 月平均绩效为60000 Console.WriteLine("该B类型商店的租金为:" + total); Console.ReadKey(); } } }
Here we have implemented the algorithm requirements for the two types of stores required by the customer, but as a good design architecture, we should also consider the subsequent demand changes. If the customer now adds C-type stores and D-type stores, Their algorithm requirements are different. At this time, we need to create C and D type stores, inherit the Ishop interface, and implement the methods inside. At the same time, we must continue to modify the factory class and add cases in switch to capture and create the corresponding stores. Object, once such a situation occurs, it is not conducive to the scalability of the program and the maintenance of the later project.
Advantages:
The simple factory pattern can decide which specific class of objects should be created based on the information given by the outside world. Through it, the outside world can get rid of the embarrassing situation of directly creating specific product objects.
The outside world is isolated from the specific class, and the coincidence is low.
Clearly distinguish their respective responsibilities and powers, which is conducive to the optimization of the entire software architecture.
Disadvantages:
The factory class concentrates the creation logic of all instances, which easily violates GRASPR's high cohesion responsibility allocation principle
Although the simple factory pattern can adapt to certain changes, the problems it can solve are far from the limited. The classes it can create can only be considered in advance. If you need to add a new class, you need to change the factory class.
How to solve the appeal situation that arises is worth thinking about and will be well solved in the next factory method pattern.
The above is the entire content of this article. I hope it will be helpful to everyone’s learning. I also hope that everyone will support the PHP Chinese website.