Simple Factory Model Introduction:
The simple factory pattern belongs to the creation pattern, also called the Static Factory method (static Factory methods) pattern, but does not belong to 23 kinds of GOF design patterns. A simple factory pattern is a factory object that determines which instances of the product class are created. The simple factory pattern is the simplest and most practical pattern in the factory model family, and can be understood as a special implementation of different factory patterns.
Structure Pattern diagram:
Role Categories:
Factory (Creator) role
The core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The method of creating a product class for a factory class can be called directly from outside to create the desired product object.
Abstract products (product) role
The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interfaces common to all instances.
Specific product (concrete product) role
is the goal of creating a simple factory pattern, and all created objects are instances of a specific class that acts as the role.
Introduction of the actual situation:
If there is a household management system in which the type of household is variable, the rent calculation formula for each tenant type is different.
Type a household rent = days * Unit price + performance *0.005
Type B Household rent = month * (monthly price +performance*0.001)
Analysis:
1. There is a common calculation method for stores, which is the behavior of the entity store, however, they behave in a different way, all of us abstract 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 want to create a specific product class, here is the specific type store, which implements the store's behavior method. 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-type store creation public
class Ashop:ishop
{
///<summary >
//////Type a store rental amount, days * unit price + performance
*0.005///</summary>
///<param name= "Days" > Day </param >
///<param name= "Dayprice" > Daily price </param>
///<param name= "Performance" > daily average performance </ param>
///<returns></returns> public
double getrent (int days, double dayprice, double Performance)
{
Console.WriteLine ("A store's rent algorithm");
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-type store creation
///</summary>
public class Bshop:ishop
{
///<summary>
///B-type store rent = month * (monthly price +performance*0.001)
/// </summary>
///<param name= "month" > Months </param>
///<param name= "Monthprice" > Monthly Unit Price </param>
///<param name= "Performance" > Monthly average performance </param>
///<returns></returns > Public
double getrent (int month, double Monthprice, double performance)
{
Console.WriteLine (" b store's rent algorithm ");
Return month * (Monthprice + performance * 0.001);}}
4. After creating the type store and implementing the method, consider under what circumstances how to create that object, and then the most central part of the Simple factory model: The factory class is 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 to
new AShop ();
Case "Bshop": Return to
new AShop ();
Default:
throw new Exception ("The store does not exist");}}
5. Then, judging by the current store type, 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 type of store double total = as.getrent (30, 300, 2000);
30 days/100 Yuanri average performance is Console.WriteLine ("The Rent for the type a store is:" + total);
Console.WriteLine ("=============");
Ishop Bs;
FactoryMethod BFM = new FactoryMethod (); Bs = BFM. Createshow ("Bshop"); A store total = bs.getrent (3, 3000, 60000) of type B;
March/4000 yuan monthly average performance of 60000 Console.WriteLine ("The B-type store rent is:" + total);
Console.readkey (); }
}
}
Here we realize the requirements of the two types of store algorithms that customers require, but as a good design architecture, you should also take into account the needs of the following changes, if customers now add C-type stores and D-type stores, their algorithm requirements are different, this time we need to do c,d type store creation, and inherits the Ishop interface, realizes inside the method, simultaneously also must continue to modify the factory class in SWITC to add the case to capture creates the corresponding store object, once appears this kind of situation, is unfavorable to the program Expansibility and the project later maintenance.
Advantages:
- A simple factory model can determine which specific class objects should be created based on the information given by the outside world. Through it, outsiders can get rid of embarrassing situations that directly create specific product objects.
- The outside and the concrete class isolation, the coupling is low.
- Clearly distinguishes the respective responsibility and the power, is advantageous to the entire software system structure optimization.
Disadvantages:
- The factory class centralizes the creation logic of all instances, easily violating GRASPR's high cohesion principle of responsibility allocation
- Although the simple factory model can adapt to certain changes, but it can solve the problem is far from limited. The classes that it can create can only be considered in advance, and if you need to add a new class, you need to change the factory class.
The appearance of the appeal situation, how should be resolved, worth thinking, will be in the next factory method model to be well resolved.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.