Sixth ~ Chapter 9 simple factory, factory method, abstract factory and reflection Factory

Source: Internet
Author: User

The names of simple factory mode, factory method, and abstract factory mode are all named "Factory. These three models have both similarities and characteristics. So I learned these three models together.

6 simple factory Mode
6.1 Overview
6.1.1 Intention
The role of a simple factory is to instantiate an object without the customer's understanding of the specific subclass of the object.

6.1.2 usage scenarios
Classes instantiated in a simple factory have the same interface. When the subclass is limited and basically do not need to be extended, you can use a simple factory. (The traditional implementation method is described here. In the. NET development environment, you can use the Reflection Method to simplify the implementation. This will be learned later)

6.1.3 Effect
The advantage of using a simple factory is that users can obtain corresponding class instances based on parameters, avoiding direct instantiation of classes and reducing coupling. The disadvantage is that the instantiated type has been determined during compilation. If a new type is added, You need to modify the factory.
A simple factory needs to know all types to be generated. It is not suitable for use when there are too many child classes or too many child classes.

6.2. Net implementation
6.2.1 implementation highlights
* Generally, a simple factory does not need to be instantiated, but uses a static method.
* Abstract classes and interfaces can be abstracted to specific classes. If the subclass to be instantiated has a large numberCodeAbstract classes can be considered to reduce code duplication. However, the disadvantage of doing so is poor scalability and potential coupling between sub-classes. Therefore, you can usually consider using interfaces instead of abstract classes. A specific class can achieve the same effect through implementing interfaces.

6.2.2 Structure
6.2.3 code framework
6.3.3 comprehensive application of simple factories
The most common applicationProgramInstantiate the corresponding database category by reading the database type information in the configuration file (Web. config or app. config. When the database type used changes, you only need to provide the corresponding database category. You can modify the parameters in the configuration file without modifying the original program.

6.3.4 related modes
A simple factory is not included in the 23 Design Patterns of gof, but is classified as a form of factory method to make parameter factory methods.

7 factory method mode
Introduction
The factory method is a design pattern with a small granularity, because the representation of the pattern is only an abstract method. The factory method is often used to create instances of classes related to a class. The database connection object in. NET is the factory that generates the database command object, and the createcommand method is the factory method.

The createcommand factory method for generating idbcommand objects is defined in idbconnection. A specific command object is created by a specific connection object. It is related to a specific database. Different databases create different types of command objects.
Idbconnection defines the createcommand interface:
Idbconnection createcommand ();
The specific connection class defines the corresponding implementation.

7.1 intent
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to the subclass. This interface refers to an abstract method. This method indicates that an object needs to be created, but it does not provide the specific creation method and type of object to be created.

7.2 usage
The factory method can be used when a class does not know the class of the object it must create or a class wants the subclass to specify the object it creates.

7.3 Structure

Using System;
Namespace Cshaprecreator
{
Public   Abstract   Class Creator
{
Public AbstractProduction factorymathod ();
}

Public   Class Concretecreator: Creator
{< br> Public override production factorymathod ()
{< br> return New concreteproduction ();
}
}

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.