当前位置:   article > 正文

创建型——抽象工厂模式C++实现_std::shared_ptr

std::shared_ptr

相比于工厂方法模式,抽象工厂模式每个工厂可以生产多种产品(比如电影和书籍)。

实例

AbstractFactory.h

  1. #ifndef ABSTRACT_FACTORY_H_
  2. #define ABSTRACT_FACTORY_H_
  3. #include <memory>
  4. #include "AbstractProduct.h"
  5. class Factory {
  6. public:
  7. virtual std::shared_ptr<Movie> ProductMovie() = 0;
  8. virtual std::shared_ptr<Book> ProductBook() = 0;
  9. };
  10. #endif // ABSTRACT_FACTORY_H_

ConcreteFactory.h

  1. #ifndef CONCRETE_FACTORY_H_
  2. #define CONCRETE_FACTORY_H_
  3. #include <memory>
  4. #include "AbstractFactory.h"
  5. #include "ConcreteProduct.h"
  6. // 具体工厂类 中国生产者
  7. class ChineseProducer : public Factory {
  8. public:
  9. std::shared_ptr<Movie> ProductMovie() override {
  10. return std::make_shared<ChineseMovie>();
  11. }
  12. std::shared_ptr<Book> ProductBook() override {
  13. return std::make_shared<ChineseBook>();
  14. }
  15. };
  16. // 具体工厂类 日本生产者
  17. class JapaneseProducer : public Factory {
  18. public:
  19. std::shared_ptr<Movie> ProductMovie() override {
  20. return std::make_shared<JapaneseMovie>();
  21. }
  22. std::shared_ptr<Book> ProductBook() override {
  23. return std::make_shared<JapaneseBook>();
  24. }
  25. };
  26. #endif // CONCRETE_FACTORY_H_

AbstractProduct.h

  1. #ifndef ABSTRACT_PRODUCT_H_
  2. #define ABSTRACT_PRODUCT_H_
  3. #include <string>
  4. // 抽象产品类 电影
  5. class Movie {
  6. public:
  7. virtual std::string ShowMovieName() = 0;
  8. };
  9. // 抽象产品类 书籍
  10. class Book {
  11. public:
  12. virtual std::string ShowBookName() = 0;
  13. };
  14. #endif // ABSTRACT_PRODUCT_H_

ConcreteProduct.h

  1. #ifndef CONCRETE_PRODUCT_H_
  2. #define CONCRETE_PRODUCT_H_
  3. #include <iostream>
  4. #include <string>
  5. #include "AbstractProduct.h"
  6. // 具体产品类 电影::国产电影
  7. class ChineseMovie : public Movie {
  8. std::string ShowMovieName() override {
  9. return "《让子弹飞》";
  10. }
  11. };
  12. // 具体产品类 电影::日本电影
  13. class JapaneseMovie : public Movie {
  14. std::string ShowMovieName() override {
  15. return "《千与千寻》";
  16. }
  17. };
  18. // 具体产品类 书籍::国产书籍
  19. class ChineseBook : public Book {
  20. std::string ShowBookName() override {
  21. return "《三国演义》";
  22. }
  23. };
  24. // 具体产品类 书籍::日本书籍
  25. class JapaneseBook : public Book {
  26. std::string ShowBookName() override {
  27. return "《白夜行》";
  28. }
  29. };
  30. #endif // CONCRETE_PRODUCT_H_

main.cpp

  1. #include <iostream>
  2. #include "AbstractFactory.h"
  3. #include "ConcreteFactory.h"
  4. int main() {
  5. std::shared_ptr<Factory> factory;
  6. // 这里假设从配置中读到的是Chinese(运行时决定的)
  7. std::string conf = "China";
  8. // 程序根据当前配置或环境选择创建者的类型
  9. if (conf == "China") {
  10. factory = std::make_shared<ChineseProducer>();
  11. } else if (conf == "Japan") {
  12. factory = std::make_shared<JapaneseProducer>();
  13. } else {
  14. std::cout << "error conf" << std::endl;
  15. }
  16. std::shared_ptr<Movie> movie;
  17. std::shared_ptr<Book> book;
  18. movie = factory->ProductMovie();
  19. book = factory->ProductBook();
  20. std::cout << "获取一部电影: " << movie->ShowMovieName() << std::endl;
  21. std::cout << "获取一本书: " << book->ShowBookName() << std::endl;
  22. return 0;
  23. }

编译运行:

  1. $g++ -g main.cpp -o abstractfactory -std=c++11
  2. $./abstractfactory
  3. 获取一部电影: 《让子弹飞》
  4. 获取一本书: 《三国演义》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51363
推荐阅读
  

闽ICP备14008679号