. NET simple factory mode and. net Factory

Source: Internet
Author: User

. NET simple factory mode and. net Factory

Simple factory mode introduction:

The simple Factory mode is a creation mode, also called the Static Factory Method mode, but not one of the 23 GOF design modes. The simple factory mode is determined by a factory object to create a product instance. The simple factory model is the simplest and Practical Model in the factory model family. It can be understood as a special implementation of different factory models.

Structure Pattern:

Role category:

Factory (Creator) Role
The core of the simple factory mode is to implement the internal logic for creating all instances. The method for creating a product class of the factory class can be directly called by the outside world to create the desired product object.

Abstract Product role
The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.

Product (Concrete Product) Role
Is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as this role.

Introduce the actual situation:

If there is a household management system, the household type is variable, and the rental calculation formula for each tenant type is different

A type of household Rental Amount = days * Unit Price + performance * 0.005

Rent amount of B type households = month * (monthly price + performance * 0.001)

Analysis:

1. The store has a common calculation method. This is the behavior of the physical store. However, their behavior is different. All the abstract store classes are shown in the code below:

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 need to create a specific product class. Here is the specific type store, which implements the behavior of the store. Create A store

Using SimpleFactory. app. IFactroy; using System. collections. generic; using System. linq; using System. text; namespace SimpleFactory. app. public class Ashop: Ishop creation of product {// A type store, days * Unit Price + performance * 0.005 // </summary> // <param name = "days"> days </param> /// <param name = "dayprice"> unit price per day </param> /// <param name = "performance"> average daily performance </param> /// <returns> </returns> public double Getrent (int days, double dayprice, double performance) {Console. writeLine ("Shop A's rental algorithm"); return days * dayprice + performance * 0.01 ;}}}

3. Create a B-type store:

Using SimpleFactory. app. IFactroy; using System. collections. generic; using System. linq; using System. text; namespace SimpleFactory. app. product {// <summary> // create a store of type B /// </summary> public class Bshop: ishop {// <summary> // rent of type B store = month * (monthly price + performance * 0.001) /// </summary> /// <param name = "month"> Number of months </param> /// <param name = "monthprice"> unit price per month </param> /// <param name = "performance"> average monthly performance </param> /// <returns> </returns> public double Getrent (int month, double monthprice, double performance) {Console. writeLine ("B shop rental algorithm"); return month * (monthprice + performance * 0.001 );}}}

4. After creating a number-type store and implementing the method, I thought about how to create that type of object. So the most important part of the simple factory model is that the factory class has come out.

Using SimpleFactory. app. IFactroy; using SimpleFactory. app. product; 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 ("this store does not exist ");}}}}

5. Then, based on the current store type, determine which algorithm the store type should adopt:

Using SimpleFactory. app. factoryMethod; using SimpleFactory. app. IFactroy; using System. collections. generic; using System. linq; using System. text; namespace SimpleFactory. app {class Program {static void Main (string [] args) {Ishop As; factorymethod Atom Force = new factorymethod (); As = Atom Force (AES. createShow ("ashop"); // double total =. getrent (30,300,200 0); // 30 days/100 yuan daily average performance is 2000 Console. writeLine ("the rent of the-type store is:" + total); Console. writeLine ("=============="); Ishop Bs; factorymethod bfm = new factorymethod (); Bs = bfm. createShow ("bshop"); // a B-type store total = Bs. getrent (3, 3000,600 00); // The average performance in 4000/60000 is Console. writeLine ("the rent for this B-type store is:" + total); Console. readKey ();}}}

At this point, we have met the needs of the algorithms of the two types of stores that customers require. However, as a good design architecture, we should also consider the subsequent demand changes, if a customer adds another C-type store and D-type store, their algorithm requirements are different. In this case, we need to create a C-and D-type store and inherit the Ishop interface, in the implementation, we also need to modify the factory class and add case in switc to capture and create the corresponding store object. Once this happens, it is not conducive to program scalability and the maintenance of projects in the future.

Advantages:

  • The simple factory mode determines the object of a specific class based on the information given by the outside world. With this feature, the outside world can get rid of the embarrassing situation of directly creating specific products.
  • The outside world is isolated from specific classes, with low coupling.
  • Clearly differentiate their respective responsibilities and powers, which is conducive to the optimization of the entire software architecture.

Disadvantages:

  • The factory class integrates the creation logic of all instances, which is easy to violate the GRASPR High Cohesion responsibility allocation principle.
  • Although the simple factory model can adapt to certain changes, the problems it can solve are far from limited. The class it can create can only be taught in advance, if you need to add a new class, you need to change the factory class.

How should we solve the appeal situation? It is worth thinking that it will be well solved in the next factory method model.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.