c++ - 使用哪种设计模式存储由第一个函数创建的指针,该指针将在以后使用?

标签 c++ c++11 pointers design-patterns

我正在寻找一种可以存储指针 vector 并可以根据需求删除指针 vector 的设计模式。

这是我现有的代码路径。

### implementation.h

class A {
   A() {}
   private:
     void AggregateMetrics();
     void FlushMetrics();
     X* x_;
     Y* y_;
};

class X {
   public:
       void CreateFiles(vector<B*> *objects, string path);
};

class B {
   B() {
     m_ = 0, n_ = 0;  
   }
   private:
      int m_, n_;
};

### implementation.cpp


void A::A() {
  x_ = new X();
  y_ = new Y();
}

void A::AggregateMetrics() {
}

void A::FlushMetrics () {
    vector<B*> objects;
    x_->CreateFiles(&objects, path);
    // In my new code, we are going to move the above two lines 
    // to AggregateMetrics() and i need to find a way to store 
    // the vector<B*>objects;
    y_->Flush(objects);
    return;
}

void X::CreateFiles(vector<B*> *objects, string path) {
    CHECK(objects.empty());
    for (int i = 0; i < 10; i++) {
        objects->push_back(new B());
    }
}

这是我的新代码:
### Implementation.h
class A {
   A() {}
   private:
     void AggregateMetrics();
     void FlushMetrics();
     X* x_;
     Y* y_;
};

class X {
   public:
       void CreateFiles(vector<B*> *objects, string path);
};

class B {
   B() {
     m_ = 0, n_ = 0;  
   }
   private:
      int m_, n_;
};

class PointerManager {
   public:
      PointerManager() {}
      void SetPointers(vector<B*>& objects);
      vector<B*> GetPointers();
   private:
      vector<B*>objects_;
};

### implementation.cpp
PointerManager::SetPointers(vector<B*>& objects) {
   objects_ = objects;
}

vector<B*> PointerManager::GetPointers() {
   return objects_;
}

void A::A() {
  x = new X();
  y = new Y();
  mgr_ = new PointerManager();
}

void A::AggregateMetrics() {
    vector<B*> objects;
    x->CreateFiles(&objects, path);
    mgr_->SetPointers(objects);
}

void A::FlushMetrics () {
    auto objects = mgr_->GetPointers();
    y->Flush(objects);
    return;
}

void X::CreateFiles(vector<B*> *objects, string path) {
    CHECK(objects.empty());
    for (;;) {
        objects->push_back(new B());
    }
}

我基本上是在创建一个名为PointerManager的新类,该类可以在创建后保存这些指针,并可以在需要时返回。这里理想的设计是什么?你们可以建议一种设计模式吗?

最佳答案

我建议使用smart pointer并将它们存储在容器中,以避免任何内存泄漏。

这是使用智能指针的设计版本

Implementation.hpp:

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <cassert>
class B {
    public:
        B() {
            m_ = 0, n_ = 0;
        }
    private:
        int m_, n_;
};
class Y{
    public:
        Y(){}
        ~Y(){}
        void Flush(std::vector<std::unique_ptr<B>>& objects);
};
class X {
    public:
        void CreateFiles(std::vector<std::unique_ptr<B>> &objects, std::string path);
};
class PointerManager {
    public:
        PointerManager() {}
        void InsertPointer(std::unique_ptr<B> &object);
        void SetPointers(std::vector<std::unique_ptr<B>> &objects);
        std::vector<std::unique_ptr<B>> &GetPointers();
    private:
        std::vector<std::unique_ptr<B>> objects_;
};
class A {
    public:
        A();
        void AggregateMetrics();
        void FlushMetrics();
    private:
        X* x_;
        Y* y_;
        PointerManager* mgr_;
};

实现.cpp
#include "implementation.hpp"

void Y::Flush(std::vector<std::unique_ptr<B>>& objects){
    for(int i =0;i<objects.size();i++){
        objects[i].release();
    }
}

void X::CreateFiles(std::vector<std::unique_ptr<B>> &objects, std::string path) {
    assert(objects.empty());
    for (int i = 0; i < 5;i++) {
        std::cout << "for loop in CreatesFiles " << std::endl;
        objects.emplace_back(new B);
    }
}

void PointerManager::InsertPointer(std::unique_ptr<B> &object) {
    std::cout << "InsertPointer " << std::endl;
    objects_.push_back(std::move(object)); // object now belongs to PointerManager
}

void PointerManager::SetPointers(std::vector<std::unique_ptr<B>> &objects){
    for(int i=0;i<objects.size();i++){
        this->InsertPointer(objects[i]);
    }
}

std::vector<std::unique_ptr<B>>& PointerManager::GetPointers() {
    std::cout << "Get Pointers" << std::endl;
    return objects_;
}

A::A() {
    x_ = new X();
    y_ = new Y();
    mgr_ = new PointerManager();
}

void A::AggregateMetrics() {
    std::cout << "Aggregate Metrics " << std::endl;
    std::string path = ".";
    std::vector<std::unique_ptr<B>> objects;

    x_->CreateFiles(objects, path);
    mgr_->SetPointers(objects);
}

void A::FlushMetrics () {
    std::cout << "Flush Metrics " << std::endl;
    y_->Flush(mgr_->GetPointers());
    return;
}

使用-std = c++ 11标志在CLANG 3.4.2和g++ 4.9.3中运行良好。

关于c++ - 使用哪种设计模式存储由第一个函数创建的指针,该指针将在以后使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42267639/

相关文章:

使用指向数组的指针的 C++ 迭代器范围

c++ - 在 C++ 中声明长文字时是否需要长后缀和无符号后缀?

c++ - 在 Qt Creator 中使用 Magick++

c++ - 将多个参数传递给线程函数 c++11

c++ - 为什么 sizeof(std::mutex)==40 (gcc,clang,icc)?

c++ - 分配 char 缓冲区以保存 float 的文本表示

c++ - C/C++如何调用外部程序并获取多个返回值?

c++ - 类型删除和可变参数模板化成员函数

c# - 非托管 C++ 代码导致 .Net 应用程序崩溃

c - 使用 & 运算符和不使用 & 运算符的地址有什么区别?