Go Language Simple Factory mode

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The simple factory model is characterized by the ability of the factory to produce classes of different functions according to the conditions, thus improving the reusability of the program code.

package mainimport  (     "FMT") type operater interface { // Operator Interface     operate (int, int)  int}type AddOperate struct {   //addition Operation class, implements the Arithmetic interface}func  (this *addoperate) Operate (rhs int, lhs int)   int  {    return rhs + lhs}type suboperate struct { // The subtraction operation class, implements the Arithmetic interface}func  (this *suboperate) Operate (rhs int, lhs int)  int {     return rhs - lhs}type OperateFactory struct {}func  Newoperatefactory ()   *operatefactory {    return &operatefactory{} }func  (this *operatefactory) createoperate (operatename string)  Operater {     switch   operatename {        case   "+":             return &addoperate{}         case  "-":             return &suboperate{}        default:             panic ("Invalid operation symbol")              return nil    }}int main ()  {     defer func ()  {        if err :=  Recover (); err != nil {             fmt. PRINTLN (ERR)         }    }{}     operator := newoperatefactory (). Createoperate ("+")   //this time operator  impossible for nil    fmt. Printf ("add result is %d\n",  operator.operate)  //add result is 3     return}

In this example, a variety of operation method classes to implement the Operation interface, in the business if you want to add a method of operation, only need to increase the implementation of an interface class, and in the factory class to add a type judgment. This design is suitable for the business type is not a lot of cases, if the business type is very large, then there will be a very long switch...case structure in the work class, the use of method Factory mode will be more appropriate. Under normal circumstances, the factory class can be replaced with a function. But for the aesthetics of its class diagram logic, it is defined as a factory class. The simple factory method puts the logic of business type judgment in the factory class, making it simpler and more uniform to call the factory class process. If the logical judgment of the factory class in the simple factory model is transferred to the factory class caller, it will become the factory method pattern, so that a specific factory class will correspond to a specific product class one by one. If further, a specific factory class to combine a number of specific product classes, then the method Factory mode will become abstract Factory mode. In the application process can be said that each has its own advantages, must look at specific application scenarios, such as if the factory class to produce specific product logic in the call factory class process after the business logic is basically consistent, then the use of a simple factory method is better than the other two Chinese factory methods. Simple Factory mode is not a good choice if the business logic is not the same after invoking the factory class to produce a specific product class.

Object-oriented programming is not about language, but the idea of programming, but it is difficult or intuitive to implement in different languages. The Go language support for the implementation of various design patterns is better than that of C + +.




Related Article

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.