Dot Net Design Mode-factory method mode

Source: Internet
Author: User
Tags dot net
1. Introduction
The factory method is a design pattern with a small granularity, because the expression 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 Data command object. The createcommand method is the factory method and its structure.

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:
Idbconnectio createcommad ();
The specific connection class defines the corresponding implementation, such as for the Oracle database:
Public oraclecommand createcommand ();

2. Overview
2.1 intent
Defines an interface used to create objects, so that the subclass determines which class to instantiate. 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 does not provide the specific creation method and the type of object to be created.
C # Code As follows: Using system;

Namespace cshapecreator
{
Public   Abstract Class cteator
{
Public AbstractProduction factorymathod ();
}

Public   Class Concretecreator: Creator
{
Public   Override Production factorymathod ()
{
Return NewConcreteproduction ();
}
}
}

2.2 usage
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, the factory method can be used.

2.3 Structure
Structure of the factory method.

Note that, in the figure, the part of the factory method mode is only factory method (). Define the interface for this method in create and implement this method in concretecreator. If this method is not available, it is not the factory method mode. It should also be noted that creator is not only responsible for creating a product, it often also contains the template method. That is, the mode is limited to the method part, which is why it is called the "Factory method. Unlike the abstract factory and constructor, both have factory classes. During use, instances of the factory class must be passed as parameters for the user to instantiate the product. Of course, the factory method mode may be used in these modes.

2.4 results
The factory method makes the code in the class independent of the class it must create. The Code only needs to know the interface of the class it needs to create. The disadvantage of the factory method is that a new class needs to be created, and a corresponding subclass needs to be added.

3. Factory method in. Net -- get iterator
Collections and lists in. Net all provide Iterative Methods for traversing and accessing itself. The set implements the ienumerable interface, while the iterator implements the ienumerator interface. Since the iterator cannot exist independently from aggregation, it does not have a public constructor and can only be created through aggregation. Different aggregates use different iterators. In ienumerable, the interface for generating iterators is defined, that is, the factory method ..

Classes that implement the ienumerable interface, such as arraylist and other collection classes, implement the getenumerator function, that is, implement the specific implementation of the factory method. Obtain different ienumerator based on different classes. For example, the ienumerator returned by getenumerator in arraylist is arraylistenumeratorsimple, And the ienumerator returned by getenumerator in sting is charenumerator: Using System;
Using System. collections;

Namespace Enumerator
{
Public   Class Class1
{
[Stathread]
Static   Void Main (sting [] ARGs)
{
Arraylist =   New Arraylist ();
Ienuemrator E = A. getenumerator ();

System. Console. writeline (E. GetType (). Name );

String s = "";
Ienumerator se = S. getenumerator ();

System. Console. writeline (SE. GetType (). Name );

System. Console. Readline ();
}
}
}

Here, the factory method plays a role in the connection class hierarchy.

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.