. Net brief introduction to the design model (Abstract Factory Model)

Source: Internet
Author: User

We will continue to learn the series of design patterns.

Today we will talk about the "factory model" that is often used in design patterns. The so-called factory model refers to handing over the creation of objects to an object called a factory for unified processing. It mainly decouples object dependencies to eliminate direct coupling between objects. How can the factory be used? It depends on our understanding of the factory. The factory is a logical name of the object to be created. The factory can be a method, a static class, and so on. As long as the object is created, it can be assigned the name of the previous factory.

So what is an abstract factory? I think many people do not have a thorough understanding of it. Some people even think that the factory must be abstract. I don't understand it. An abstract class does not even have an abstract or virtual Member. I want to ask what this class is for. Is it just an example of imitating design patterns. I want to use this article to explain the various application methods of the factory model clearly so that unclear friends can correctly understand the nature of OOP.

First, let's clarify what the essence of the factory model is and what its purpose is. Only by having a correct understanding of it can we guide us to a correct understanding. [Wang qingpei has all rights reserved. For more information, please sign it.]

Factory mode: Provides interfaces for object creation.

This short sentence outlines the true intention of the factory. When using the model, we need to make appropriate choices. The model is well-developed and summed up by our predecessors with the experience of a lifetime. We have to admit that it is perfect.

Let's first introduce how the "simple factory model" is going on. Simple factory is the first abstract representation of a factory. It is also relatively simple. The complicated abstract factory is the highest abstract application of the factory. We will explain it in detail at the end.

Simple factory introduction:A simple factory is to extract the Object Instantiation to form an independent object dedicated to object creation. This is called a simple factory. This is actually a bit of programming experience, friends will think, you can not regard it as a design model, but the book mentioned, therefore, in order not to cause the group to be sprayed, I have to write it out. [Wang qingpei has all rights reserved. For more information, please sign it.]

Factory method: extends the creation of objects to sub-classes.

In fact, from my personal point of view, I do not think of the factory method as a design mode. When we define an abstract class, there will always be some abstract methods in it, some of these methods may be used to create objects, or some other things. This design is common sense. abstract methods must be implemented by sub-classes. I think Abstract Factory is a real design model.

Abstract Factory: Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode provides an interface to the client to create product objects in multiple product families without having to specify the specific product.

Such a definition may be clear to friends who are familiar with abstract factories. Beginners may be confused. Let's use the image to represent it;

1:

This figure indicates that there are many types of fruit sellers, each of which wants to sell their own fruit. We focus on the factory side, where many factories produce different fruits. We need to abstract the factory. It seems that we have guided us to extract the concept of the abstract factory. The factory is a specific class and is responsible for producing a certain type of fruit, following the principles advocated by the design model, we need to abstract the changes. Therefore, we need to abstract the specific factory to form an abstract factory. The Abstract Factory defines a uniform interface for getting fruit. Any customer dealer uses this uniform interface for Fruit wholesale.

2:

All customers get fruit through the abstract interface defined by the factory. The following code is used to explain it.

Abstract Factory Code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // fruit abstraction factory /// </Summary> public abstract class fruitabstractfactory {// <summary> // fruit name, indicates the fruit type, such as apple, banana, and orange. /// </Summary> Public String fruitname {Get; Set ;}/// <summary> /// specifies the abstract method, get the fruit to be wholesale /// </Summary> /// <returns> the fruit to be wholesale </returns> public abstract string getcurrentfruit ();}}

Apple Factory Code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {/// <summary> /// Apple factory //</Summary> public class applefactory: fruitabstractfactory {public applefactory () {fruitname = "apple ";} /// <summary> /// override the method for getting fruit from the base class /// </Summary> /// <returns> the fruit to be wholesale </returns> Public override string getcurrentfruit () {return "to you:" + base. fruitname ;}}}

Banana Factory Code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {// <summary> // banana factory /// </Summary> public class bananafactory: fruitabstractfactory {public bananafactory () {fruitname = "banana";} public override string getcurrentfruit () {return "to you:" + base. fruitname ;}}}

 

Orange Factory Code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {/// <summary> // orange factory // </Summary> public class tangerinefactory: fruitabstractfactory {public tangerinefactory () {fruitname = "orange";} public override string getcurrentfruit () {return "to you:" + base. fruitname ;}}}

Simulated call code:

Using system; using system. collections. generic; using system. text; namespace consoleapplication1 {class program {static void main (string [] ARGs) {fruitactactfactory fruitfactory = new applefactory (); // Apple factory console. writeline (fruitfactory. getcurrentfruit (); console. readline (); fruitfactory = new bananafactory (); // banana factory console. writeline (fruitfactory. getcurrentfruit (); console. readline (); fruitfactory = new tangerinefactory (); // orange factory console. writeline (fruitfactory. getcurrentfruit (); console. readline ();}}}

Summary: The general principle of the abstract factory is finished. In fact, here we can abstract the fruit dealer to form a factory method, and then call the fruit factory in the specific dealer. I will not write it because of the time.

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.