Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

.net simple factory model explanation

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is ".NET simple factory model explanation", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn ".NET simple factory model explanation" bar!

Introduction to the simple factory model:

The simple factory pattern is a creative pattern, also known as the static factory method (Static Factory Method) pattern, but not one of the 23 GOF design patterns. The simple factory pattern is for a factory object to decide which instance of a product class to create. Simple factory pattern is the simplest and most practical pattern in the family of factory patterns, which 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, which is responsible for implementing the internal logic for creating all instances. The method of creating the product class of the factory class can be called directly by the outside world to create the desired product object.

Abstract product (Product) role

The parent class of all objects created by the simple factory pattern, which is responsible for describing the common interface common to all instances.

Specific product (Concrete Product) role

Is the creation goal of the simple factory pattern, and all the objects created are instances of a specific class that plays this role.

Introduce the actual situation:

If there is a household management system, the types of households in it are variable, and the rent calculation formulas for each type of tenant are different.

Type A household rent = days * unit price + performance * 0.005

Type B household rent = month * (monthly price + performance*0.001)

Analysis:

1. Stores have a common calculation method, which is the behavior of physical stores, but their behavior is different, so we abstract store classes, 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);}}

two。 After abstracting the store, we need to create a concrete product class, here is the concrete type store, which implements the behavior of the store. Create a type A store

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 {/ / 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;}

3. Create a store of type B:

Using SimpleFactory.App.IFactroy;using System;using System.Collections.Generic;using System.Linq;using System.Text Creation of namespace SimpleFactory.App.product {/ B type store / public class Bshop:Ishop {/ B type store rental = month * (monthly price + performance*0.001) / number of months / / monthly unit price / monthly average performance / public double Getrent (int month, double monthprice Double performance) {Console.WriteLine ("rent algorithm for store B") Return month * (monthprice + performance * 0.001);}

4. After creating the number type store and implementing the method, think about when and how to create that object, so the core part of the simple factory pattern: the factory class comes 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 judge which algorithm should be carried out for this type of store according to the current store type:

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 double total = As.Getrent (30,300,2000) / / 30 days / 100RMB daily average performance is 2000 Console.WriteLine ("the rent for this 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, 3000, 60000) / / March / 4000 yuan monthly average performance is 60000 Console.WriteLine ("the rent for this type B store is:" + total); Console.ReadKey ();}

So far, we have realized the algorithm requirements of the two types of stores required by customers, but as a good design architecture, we should also take into account the later demand changes. If customers now add C-type stores and D-type stores, and their algorithm requirements are different, at this time, we need to create a C-type 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.

Advantages:

The simple factory pattern can determine 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 awkward situation of directly creating specific product targets.

The outside world is isolated from the concrete class, and the coupling is low.

Clearly distinguish their respective responsibilities and powers, which is conducive to the optimization of the whole software architecture.

Disadvantages:

The factory class centralizes the creation logic of all instances, so it is easy to violate GRASPR's high cohesion principle of responsibility allocation.

Although the simple factory model can adapt to certain changes, the problems it can solve are far limited. The classes 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.

At this point, I believe you have a deeper understanding of the ".NET simple factory model explanation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report