From simple to deep learning "Factory Design Model"

Source: Internet
Author: User

1,Teaching Problems in Design Patterns

I personally recently studied the factory model in the design model in a deep manner. I learned how to find that there are many design models and there are some problems in the design model process, this makes the design pattern hard to understand. The design pattern itself is very abstract, but these evangelists will abstract an abstract thing in the Process of speaking, so that we are discouraged. Some people do not consider the reader's attention when talking about it. For example, when I was reading "C # design patterns" and talking about the abstract factory patterns, I directly entered an example, which is complex, it involves many concepts, terms, and algorithms. However, these are irrelevant to the core content to be told. If I want to understand his example, I need to understand it all, which will distract my attention. In my personal summary, the method to learn the design pattern is to first find a breakthrough. For example, you can first learn the simple pattern in the constructor pattern and understand and skillfully apply it. After having a deep understanding of the first and second models, it is much easier to learn other complicated models. This is an iterative method used in learning. In addition, the process of learning any design pattern should be specific-abstract-then specific. The meaning of this sentence is to first use a more specific example to help understand the design model, and then extend your understanding to solve this problem, it has risen to a certain theoretical level. Then, let's go to the specifics, that is, the application design model, and apply the theory to solve the actual problems we encounter.

 

2To learn the preparatory knowledge of the factory model:

First, declare that these preparations are not just necessary for the factory model, because I first talk about the factory model, so I will raise these questions before learning the factory model.

 2.1 upcasting:

There are several upcasting Chinese translations, such as up-type conversion, up-transformation, and up-direction modeling. I personally prefer the translation of "upward transformation", which is simple and clear in meaning. I have seen Bruce Eckel's thinking in C ++ and thinking in Java. I am not sure whether this concept was proposed by him. The upward transformation is to use a derived class as its base class. It is safe to convert a special type to a more common type. A derived class is a superset of a base class. It can contain more methods than the base class, but it contains at least the base class methods. The advantage of upward transformation is that we can process different derivation in a unified way. The disadvantage of upward transformation is that our upward transformation process will lose the interface of the derived class. Since there is an upward transformation, there is also a downward transformation, that is, downcasting. We will not discuss it in detail here. The following is an example of upward transformation.

Public class base

{

Public void test ()

{

MessageBox. Show ("OK ");

}

}

Public class derive: Base

{}

Private void button#click (Object sender, system. eventargs E)

{

Base B = new derive ();

B. Test ();

}

One of the well-known Ood design principles is the liskov substitution principle (LSP) principle ). Its essence is to talk about upward transformation. Its content is: Any place that receives the parent type should be able to receive child types. In other words, if a base class is used, it must be applicable to its subclass, in addition, the program cannot detect the difference between the base class object and the subclass object. LSP is the cornerstone of inheritance reuse. Only when the derived class can replace the base class and the functions of the software are not affected can the base class be reused.

 

2.2Polymorphism

I can't imagine what the design pattern looks like when I leave the polymorphism. What is polymorphism? I like to answer this question in summary: "One interface, multiple implementations ". Note that the interface here is not only an interface keyword, but also an interface in a broad sense. There are two ways to implement interfaces in C #: inheritance and interface.

3Factory Design Pattern Theory

 3.1Overview

Factory models include simple factories, factory methods, and abstract factories. They are arranged in a sequence from simple to complex and belong to the creation type in design patterns, the simple factory does not belong to the gof 23 medium mode. But it is a good foundation for understanding other factory models, so many people will mention the simple factory model when talking about the design model. The Creation Mode focuses on Object creation. The Creation Mode abstracts the process of object creation and encapsulates the process of object creation, as a customer program, you only need to use objects, instead of the logic in the object creation process.

 3.2Do not use any Mode

We now have a design like this. videowiring includes DVDs and VCD. The playvideo method is available in the base class videowiring. This method is reloaded by the subclass.

 


How can we call playvideo for playback. We can see that the following code can be implemented.

Public abstract class videowiring
{
Public abstract string playvideo ();
}

Public class VCD: videowiring
{
Public override string playvideo ()
{
Return "playing VCD ";
}
}

Public class DVD: videowiring
{
Public override string playvideo ()
{
Return "playing a DVD ";
}
}

 

The following code calls the object method for playback:

Private void playvideo ()
{
DVD/DVD = new DVD ();
MessageBox. Show (DVD. playvideo ());
VCD = new VCD ();
MessageBox. Show (VCD. playvideo ());
}

The above code can implement functions, but it is not good. Why? Class, but we didn't use polymorphism when calling it. If we have a lot of photo and video products, we need to write a lot of similar

DVD. playvideo.

The following code uses polymorphism to complete the playback function:

Private void playvideo ()
{
Videowiring VW;
Vw = new DVD ();
Play (VW );
Vw = new VCD ();
Play (VW );
}
Private void play (videowiring VW)
{
String STR = VW. playvideo ();
MessageBox. Show (STR );
}

No matter what the photo and video products are, we can use a unified playback method, that is, VW. playvideo ().

 

Let's discuss the problems with the above Code. Although the Code on is very short, there should be no problem, but we should aim at a higher level. We should consider how to achieve good encapsulation effect and reduce the chance of modifying errors. We should naturally consider the issue of object creation. Can we make different photo and video products be created in the same way, and this creation process is encapsulated by users, that is to say, the object creation is as simple and unified as the playback function. If it can be implemented, it will bring greater scalability and minimal modifications to our system. "Wow! That's good ". "Don't envy me. Let's look at the simple factory model. I heard it can be implemented ".

 3.3Simple factory Mode

We continue to improve the above code using a simple factory. Based on the above analysis, we consider encapsulating the object creation step by step. A class is used to encapsulate object creation. This class is called a factory. Because of its single function, classes are generated one by one. The following is a sample code for a factory class:

Public class create
{
Public static videowiring Factory (string videoname)
{
Switch (videoname)
{
Case "DVD ":
Return new DVD ();
Case "VCD ":
Return new VCD ();
}
Return NULL;
}
}

In this way, our client code can be more effective and concise:

Private void playvideo ()
{
Videowiring vw = create. Factory ("DVD ");
VW. playvideo ();
Vw = create. Factory ("VCD ");
VW. playvideo ();
}

 

Note: In the above two code examples, we have used the upward transformation. First, note that the return new DVD () statement is used in the factory method of the create class, but the return value of this function is videowiring, which is the base class of the DVD class. Therefore, our customer program can use statements such as videowiring vw = create. Factory ("DVD. In this way, the customer program does not care about how the creation is completed and what the created object is. We can call the unified interface of the base class to implement their functions. Shows how to use UML:

 


We promote the factory model to the general situation, and its class diagram is as follows:

 


Role description:

Creator: creates a specific product based on the business logic and is directly called by the customer program.

Abstract product: As the base class of a specific product, it provides a unified interface and is also the type to be returned by the factory class.

Concrete Product: the type of the factory class to be created. Only a specific product is shown in. It is similar when there are multiple products.

Next we will summarize the simple factory model. Benefits of using a simple factory: 1. Taking full advantage of polymorphism, No matter what specific product is, abstract products are returned. 2. Make full use of encapsulation. external users will not be affected when internal products change. Its disadvantage is that if a new product is added, the factory must be modified ). Abstract Factory mode provides an interface to the client, so that the client creates product objects in multiple product families without specifying the specific product type. This is the intention of the abstract factory model.

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.