Simple Factory Model Description:
The Simple Factory mode is a Factory mode, also called the Static factory method mode, but not one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.
Structure Pattern diagram:
Character 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 by the outside world to create the desired product object.
Abstract 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 products (concrete product) roles
is the creation target of the simple factory pattern, and all created objects are instances of a specific class that acts as the role.
Introduce the actual situation:
If there is a household management system, the type of household is variable, and there are differences in the rent calculation formula for each tenant type.
A type of household rental amount = days * unit price + performance *0.005
b type of household rental amount = month * (monthly price +performance*0.001)
Analysis:
1. There are common computational methods for stores, which are the behavior of physical stores, however they behave differently in all of our abstract store classes, code 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 the abstraction of the store, we want to create a specific product class, here is the specific type of 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 of store created public class Ashop:ishop {// <summary> ///// a type 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 shop'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>////</summary> public for shop Type B Class Bshop:ishop { //<summary>//b Type store = month * (monthly price +performance*0.001)/// </summary >// <param name= "Month" > Number of months </param>// <param name= "Monthprice" > Monthly price </param> // <param name= "Performance" > Monthly average performance </param>// <returns></returns> public double getrent (int month, double Monthprice, double performance) { Console.WriteLine ("B Shop Rent Algorithm"); Return month * (Monthprice + performance * 0.001);}}
4. After creating the number type store and implementing the method, think about the circumstances under which the object was created, and then the most central part of the Simple factory model: 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 ("The store does not exist");}}}
5. Then, judging by the current store type, which algorithm should be used for that 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 store of type a double total = as.getrent (+, +); 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 of type B total = Bs.getrent (3, 60000); March/$ 4000 average monthly performance is 60000 Console.WriteLine ("The Rent for the B-type store is:" + total); Console.readkey ();}}}
Here we achieve the requirements of the two types of store requirements of the customer demand, but as a good design architecture, but also to consider the subsequent needs of change, if the customer now added C type store and D-type store, their algorithm requirements are different, this time we need to do c,d type store creation, and inherit the Ishop interface, implement the method inside, but also have to continue to modify the factory class in the SWITC add case to capture the creation of the corresponding store object, once this situation, is not conducive to the extension of the program and the maintenance of the project later.
Advantages:
The simple factory model is able to determine which specific class of objects should be created based on the information given by the outside world. Through it, outsiders can get out of the awkward situation of directly creating specific product objects.
The outside is isolated from the specific class, the coupling is low.
Clearly distinguishes the respective responsibility and the power, facilitates the entire software system structure optimization.
Disadvantages:
The factory class centralizes the creation logic of all instances, and it is easy to violate GRASPR's high cohesion responsibility allocation principle
Although the simple factory model can adapt to certain changes, the problem it solves is far from limited. The classes it can create can only be taught in advance, and if you need to add new classes, you need to change the factory class.
The appeal situation that arises, how should solve, be worth thinking about, will get a good solution in next factory method pattern.
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.