In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the definition and example usage of .NET factory method pattern". In daily operation, I believe that many people have doubts about the definition and example usage of .NET factory method pattern. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "definition and example usage of .NET factory method pattern". Next, please follow the editor to study!
Introduction to the factory method model:
The meaning of the factory method (Factory Method) pattern is to define a factory interface to create product objects, deferring the actual creation to subclasses. The core factory class is no longer responsible for product creation, so the core class becomes an abstract factory role, only responsible for the interfaces that must be implemented by the specific factory subclass. the advantage of this further abstraction is that the factory method pattern enables the system to introduce new products without modifying the specific factory role.
Factory method pattern structure diagram:
Role classification:
Abstract factory role: is the core of the factory method pattern, independent of the application. The factory class of any object created in the schema must implement this interface.
Concrete factory role: this is a concrete factory class that implements an abstract factory interface, contains logic that is closely related to the application, and is called by the application to create a product object
Abstract product role: the supertype of the object created by the factory method pattern, that is, the common parent class or common interface of the product object. In the figure above, the character is Light.
Specific product role: this role implements the interface defined by the abstract product role. A specific product is created by a specific factory, and they often correspond to each other one by one.
Introduce practical examples:
In the previous blog article, the simple factory model was used to implement the following: if there is a household management system, the type of households in it is variable. There are differences in the rent calculation formula for each type of tenant, for example: a type of household rent amount = days * unit price + performance * 0.005 B type household rent amount = month * (monthly price + performance*0.001) here, although we have realized the customer's demand, if the customer needs to add C type store and D type store in the later stage, and their algorithm requirements are not the same, then we need to create a C type D store, inherit the Ishop interface, and implement the methods in it. At the same time, we have to continue to modify the factory class to add case in switc to capture and create corresponding store objects. Once this happens, it is not conducive to the expansibility of the program and the maintenance of the later stage of the project.
1. Analysis: stores have common behavioral characteristics, and they all have to calculate the store rent. We abstract the Ishop, which is the method of calculating the store rent.
Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FactoryEntiy {public interface Ishop {double Getrent (int days, double dayprice, double performance);}}
two。 We implement the methods in the Isho interface to create a type B store.
Using FactoryEntiy;using System;using System.Collections.Generic;using System.Linq;using System.Text Namespace ProductEnity {/ inherits the store interface and implements the behavior method in it, that is, the algorithm / / public class Ashop:Ishop {/ A type store rental amount Days * Unit Price + performance * 0.005 / Daily Unit Price / average Daily performance / public double Getrent (int days, double dayprice, double performance) {Console.WriteLine ("A Store's Rent algorithm") Return days * dayprice + performance * 0.01;} using FactoryEntiy;using System;using System.Collections.Generic;using System.Linq;using System.Text Namespace ProductEnity {/ inherits the store interface and implements the behavior methods in it That is, the algorithm / / public class Bshop:Ishop {/ B type store rent = month * (monthly price + performance*0.001) / months / / monthly unit price / monthly average performance / public double Getrent (int month, double monthprice, double performance) {Console.WriteLine ("B Store Rent algorithm") Return month * (monthprice + performance * 0.001);}
3. Now consider under what circumstances to create a store entity, calculate the rent for different stores, and facilitate future additions and modifications. So we create the IFactroy interface, which is the method to be implemented to create the store object.
Using FactoryEntiy;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FactoryMethod {/ factory class, create store type entity / public interface IFactory {Ishop CreateShop ();}}
4. Now we can inherit from IFactory and create the corresponding store object in the implementation.
Using FactoryEntiy;using FactoryMethod;using ProductEnity;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ProductEnity {/ inherits the factory class and creates a type A store entity / public class CreateBshop: IFactory {public Ishop CreateShop () {return new Ashop ();} using FactoryEntiy;using FactoryMethod;using ProductEnity;using System;using System.Collections.Generic;using System.Linq;using System.Text Namespace ProductEnity {/ inherits the factory class and creates a B-type store entity / public class CreateAshop: IFactory {public Ishop CreateShop () {return new Bshop ();}
5. Then, based on the current store type, which algorithm should be carried out for this type of store:
Using FactoryEntiy;using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Reflection;using System.Text;namespace FactoryMethod.App {class Program {static void Main (string [] args) {string shopname = ConfigurationManager.AppSettings ["CreateShopClassName"]; / / shopname is the name of the creation store class, where = CreateAshop IFactory af = (IFactory) Assembly.Load ("ProductEnity") .CreateInstance ("ProductEnity." + shopname) / / the first ProductEnity is the name of dll, and the second ProductEnity is the namespace of the project. Ishop As = af.CreateShop (); double total = As.Getrent (30,300,100); / / 30 days / 100RMB daily average performance is 2000 Console.WriteLine ("rent for this type A store is: + total); Console.WriteLine (" = "); IFactory bf = (IFactory) Assembly.Load (" ProductEnity "). CreateInstance (" ProductEnity. "+" CreateBshop ") / / CreateBshop can be saved as configuration or stored in the database. / / Note that the saved string should be the same as the class name created in the project, / / otherwise the class under the project will not be found in the way of reflection. Ishop Bs = bf.CreateShop (); total = Bs.Getrent (30,300, 2000); / / 30 days / 100RMB daily average performance is 2000 Console.WriteLine ("rent for this type A store:" + total);}
Here we use reflection to create objects, replacing the previous factory class's way of creating objects through switch, which is conducive to the addition of new types of stores and algorithm modifications and maintenance after the project requirements are changed. we just need to re-add Cpene D-type stores to the project, inherit the methods in the Ishop implementation, and at the same time, add inheritance IFactroy interfaces Create the corresponding store object to compile the ProductEnity.dll of the project, and then calculate the store algorithm of the type of Cforce D, which can be calculated by reflection without the need to modify the original engineering class code.
The structure of the whole project is as follows:
At this point, the study on the definition and instance usage of the .NET factory method pattern is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.