Recently, we are learning the design model. Record each learning here
The factory model is one of the most frequently heard and used design models. I have used it before, but I haven't sorted it out each time.
The benefits of the factory model are explained on the Internet. It mainly Reduces Program coupling, facilitating maintenance and expansion.
When a program grows and new requirements emerge, maintainability becomes more important.
Factory mode: Generally, the base class or interface is assigned to a subclass object, but the principle of both the interface and the base class is polymorphism. Inheritance also plays an important role in the factory model, and code encapsulation.
Assume that a company wants to Manage payroll
All employees in the company are employees and there should be an employee, which should contain basic information and the most important wage calculation method.
public abstract class Employee{ public int JobNumber { get; set; } public string Name { get; set; } public Employee(int number, string name) { this.JobNumber = number; this.Name = name; } public abstract double CalcularSalar();}
There is a project manager. The salary calculation method is as follows: 5000 (basic salary) + project bonus. The project manager is also an employee, so it inherits the employee class
public class Manager : Employee{ private readonly double BASESALE = 0.0; private readonly double REWARDSALE = 0.0; public Manager(double baseSale, int number, string name) : base(number, name) { this.BASESALE = baseSale; this.REWARDSALE = 2000; } public override double CalcularSalar() { return this.BASESALE + this.REWARDSALE; }}
There are also programmers who calculate their salaries by 3000 (Basic bonuses ). Similarly, programmers should inherit employee classes.
class Programer : Employee { private readonly double BASESALE = 0.0; public Programer(double baseSale, int number, string name) : base(number, name) { this.BASESALE = baseSale; } public override double CalcularSalar() { return this.BASESALE; } }
Of course there are also the most important factory categories
public class Factory{ public static object CreateObject(string assembly,string objName,params object[] args) { try { Assembly asm = Assembly.Load(assembly); if (asm == null) return null; string fullName = assembly + "." + objName; Type type = asm.GetType(fullName); Object obj = Activator.CreateInstance(type, args); if (obj == null) return null; return obj; } catch (Exception ex) { LogHelper.WriteLog("[Factory].CreateObject(string objName)", ex.Message); return null; } } }
After everything is done, you get to pay.
Generally we are
Manager manager = new Manager(5000, 2000, 1, "Jack");manager.CalcularSalar();Programer programer = new Programer(3000, 2, "Mike");
programer.CalcularSalar();
This is easy to understand, but not easy to maintain and expand. When new employees appear, the changes will be large.
Factory model:
object[] paramers = new object[3];paramers[0] = double.Parse(this.cmbBaseSale.Text);paramers[1] = int.Parse(this.cmbJobNumber.Text);paramers[2] = this.cmbName.Text;Employee employee = Factory.CreateObject(GlobalValue.Model, this.cmbType.Text, paramers) as Employee;MessageBox.Show(employee.CalcularSalar().ToString());
When the company needs a tester, programmers need to write a tester to calculate the salary (Basic Salary: 2500)
The Project Manager cannot modify the entire project to the programmer (In case that the programmer modifies the programmer's salary payment method, the Project Manager will give the programmer more than 1000 yuan each month)
At this time, the factory model plays a role. The Project Manager only needs to tell you that there is an employee class, and you can inherit it to implement the tester class.
Can be written soon
public class Tester:Employee { private readonly double BASESALE = 0.0; public Tester(double baseSale, int number, string name) : base(number, name) { this.BASESALE = baseSale; } public override double CalcularSalar() { return this.BASESALE; } }
You only need to join this class, and the program will be able to send the tester's salary