The "factory approach" of the ASP. NET design pattern is an abstract factory implemented using

Source: Internet
Author: User

    • Main parts of the change:

<summary>
6. Create a factory method pattern (Abstract Factory: interface)
</summary>
Interface Ifactory//Parent interface (parent class factory)
{
<summary>
This parent interface provides an argument-free function method
</summary>
<returns></returns>
Operation CreateOperation ();
}
<summary>
Define the subclass factory and inherit the parent class factory (parent interface)
</summary>
Class Factoryadd:ifactory
{
<summary>
Encapsulates a function with the same name as the parent factory (the return value type is also the same)
</summary>
<returns></returns>
Public Operation CreateOperation ()
{
Returns the Operationadd subclass above
return new Operationadd ();
}
}

    • Source:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 7 namespaceFactorymodel8 {9     //==================== uses C # and uses a simple factory model to implement simple calculator functions ====================Ten     //knowledge points in the study: object-oriented three characteristics--inheritance, encapsulation, polymorphism One     /// <summary> A     ///1. Define the parent class, which is also a package -     /// </summary> -     classOperation the     { -         //2. Because you want subclasses to have access to the parent class, you should define the parameter as a protected variable type -         protected intNumbera; -         protected intNumberb; +         //defining attributes (required) -          Public intNumbera +         { A             Get{returnNumbera;} at             Set{Numbera =value;} -         } -          Public intNumberb -         { -             Get{returnNumberb;} -             Set{Numberb =value;} in         } -         //3. Encapsulating virtual methods for subclasses to override to          Public Virtual intGetresule () +         { -             intresult =0; the             returnresult; *         } $     }Panax Notoginseng     /// <summary> -     ///4. Define subclasses, inherit the parent class, and override (add) the parent class the     /// </summary> +     classoperationadd:operation A     { the          Public Override intGetresule () +         { -             returnNumbera +Numberb; $         } $     } -     /// <summary> -     ///5. Define subclasses, inherit the parent class, and override (subtract) the parent class the     /// </summary> -     classoperationsub:operationWuyi     { the          Public Override intGetresule () -         { Wu             returnNumbera-Numberb; -         } About     } $     /// <summary> -     ///6. Create a factory method pattern (Abstract Factory: interface) -     /// </summary> -     InterfaceIfactory//Parent Interface (parent class factory) A     { +         /// <summary> the         ///This parent interface provides an argument-free function method -         /// </summary> $         /// <returns></returns> the operation CreateOperation (); the     } the     /// <summary> the     ///define the subclass factory and inherit the parent class factory (parent interface) -     /// </summary> in     classfactoryadd:ifactory the     { the         /// <summary> About         ///encapsulates a function with the same name as the parent factory (the return value type is also the same) the         /// </summary> the         /// <returns></returns> the          PublicOperation CreateOperation () +         { -             //returns the Operationadd subclass above the             return NewOperationadd ();Bayi         } the     } the     /// <summary> -     ///define the subclass factory and inherit the parent class factory (parent interface) -     /// </summary> the     classfactorysub:ifactory the     { the         /// <summary> the         ///encapsulates a function with the same name as the parent factory (the return value type is also the same) -         /// </summary> the         /// <returns></returns> the          PublicOperation CreateOperation () the         {94             ////return above Operationadd subclass the             return Newoperationsub (); the         } the     }98     //7. Calling in the main function About     class Program -     {101         Static voidMain (string[] args)102         {103             //add operation, specify the appropriate factory type, no need to enter the operation type again104Ifactory i =Newfactorysub (); the             //perform the subtraction operation106             //ifactory II = new Factorysub (); 107Operation op =i.createoperation ();108Op. Numbera =Ten;109Op. Numberb = -; the             //call a method in a subclass to get the result111             intresult =op.getresule (); the Console.WriteLine (Result); 113 Console.readkey (); the         } the         //If you still need to have other operations in subsequent programming, you only need to add the subclasses to the subclass. the         //and in the factory method to add the corresponding factory class can be, no need to change the code, which is the factory method (abstract factory) "Li" is located117     }118}

The "factory approach" of the ASP. NET design pattern is an abstract factory implemented using

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.