jeudi 5 septembre 2019

Can't figure out which design pattern has been used

I can't figure out which design patter has been implemented by SPARTA (https://github.com/sparta/sparta.git).

I have implemented in my code a design that is very similar, but I don't know how to classify it.

It basically consists in a base class (let us call it 'Motherbase') containing reference to pointers to its derived ones; another class (let us call it 'DSMC', for direct simulation Monte Carlo) also wraps pointers to the derived classes of Motherbase, and has a methods to return references to those pointers. So, Motherbase constructor receives a pointer to DSMC and initialise its members from the references returned by DSMC. Class derived from Motherbase constitutes the modules of the program; this design allows each module to call the public methods (and share the public variables) of all other modules. Here is a 'minimum' example:

motherbase.hpp

#ifndef EV_MOTHERBASE_HPP
#define EV_MOTHERBASE_HPP

#include "dsmc.hpp"

class Motherbase
{

protected:

  DSMC* dsmc;

  DefaultPointer<ParallelEnvironment>& par_env;

public:

  Motherbase(DSMC* _dsmc_):
    dsmc (_dsmc_),
    par_env (dsmc->get_par_env())
    { }

  virtual ~Motherbase() = default;

};

#endif /* EV_MOTHERBASE_HPP */

parallel_environment.hpp

#ifndef EV_PARALLEL_HPP
#define EV_PARALLEL_HPP

#include "motherbase.hpp"

class ParallelEnvironment : protected Motherbase
{

public:

  ParallelEnvironment(DSMC*);
  ~ParallelEnvironment() = default;

};

#endif /* EV_PARALLEL_HPP */


parallel_environment.cpp

#include "parallel_environment.hpp"

#include <iostream>

ParallelEnvironment::ParallelEnvironment
  (DSMC* dsmc):
  Motherbase(dsmc)
  {
    std::cout << "Print info about parallel environment ..." << std::endl;
  }

dsmc.hpp

#ifndef EV_DSMC_HPP
#define EV_DSMC_HPP

#include <memory>

#define DefaultPointer std::shared_ptr

class ParallelEnvironment;

class DSMC
{

private:

  DefaultPointer<ParallelEnvironment> par_env;

public:

  DSMC();
  ~DSMC() = default;

  inline DefaultPointer<ParallelEnvironment>& get_par_env() { return par_env; }

};

#endif /* DSMC_HPP */


dsmc.cpp

#include "dsmc.hpp"
#include "parallel_environment.hpp"

#include <iostream>

DSMC::DSMC():
  par_env ( new ParallelEnvironment(this) )
  {
    std::cout << "DSMC has been constructed" << std::endl;
  }


So, I am quite ignorant about design patterns and it would be really helpful if someone would point me to the right direction; is this a well-known design? If not, is it similar to some other?

Aucun commentaire:

Enregistrer un commentaire