Creational pattern - factory method pattern (FACTORY METHOD)

Define an interface for user-created objects and let subclasses decide which class to instantiate. The factory method pattern defers the instantiation of a class to its subclasses.

The core spirit of this mode is to encapsulate the changing part of the class, extract the personalized and fickle part as an independent class, and achieve the purpose of decoupling, reuse and convenient later maintenance and expansion through dependency injection. Its core structure has four roles, namely abstract factory; concrete factory; abstract product; concrete product.

Factory methods are often used in two situations:

The first case is that for a certain product, the caller clearly knows which specific factory service should be used, instantiates the specific factory, and produces a specific product.

In the second case, you just need a product, and you don’t want to know or need to know which factory is for production, that is, the final choice of which specific factory to choose is on the producer’s side, and they instantiate a product according to the current system situation. The specific factory is returned to the user, and this decision-making process is transparent to the user.

example:

// Abstract factory class produces movies
class Factory
{ public:     virtual std::shared_ptr<Movie> get_movie() = 0; };


// Specific factory class Chinese Producer
class ChineseProducer : public Factory
{ public:     std::shared_ptr<Movie> get_movie() override     {         return std::make_shared<ChineseMovie>();     } };





// Specific factory class Japanese producer
class JapaneseProducer : public Factory
{ public:     std::shared_ptr<Movie> get_movie() override     {         return std::make_shared<JapaneseMovie>();     } };





// Specific factory class American Producer
class AmericanProducer : public Factory
{ public:     std::shared_ptr<Movie> get_movie() override     {         return std::make_shared<AmericanMovie>();     } };





// Abstract product class Movie
class Movie
{ public:     virtual std::string get_a_movie() = 0; };


// Specific product movie:: domestic movie
class ChineseMovie : public Movie
{ public:     std::string get_a_movie() override     {         return ""Let the Bullets Fly"";     } };





// Specific product movie:: Japanese movie
class JapaneseMovie : public Movie
{ public:     std::string get_a_movie() override     {         return ""Spirited Away"";     } };





// Specific product movie:: American movie
class AmericanMovie : public Movie
{ public:     std::string get_a_movie() override     {         return ""Iron Man"";     } };





int main()
{
    std::shared_ptr<Factory> factory;
    std::shared_ptr<Movie> product;

    // Here it is assumed that what is read from the configuration is Chinese (determined at runtime)
    std::string conf = "China";

    // The program selects the type of producer according to the current configuration or environment
    if (conf == "China") {         factory = std::make_shared<ChineseProducer>();     } else if (conf == "Japan") {         factory = std ::make_shared<JapaneseProducer>();     } else if (conf == "America") {         factory = std::make_shared<AmericanProducer>();     } else {         std::cout << "error conf" << std: :endl;     }







    product = factory->get_movie();
    std::cout << "Get a movie: " << product->get_a_movie() << std::endl;
}

Guess you like

Origin blog.csdn.net/rukawashan/article/details/124485553