Blog Home

Defeat "Static Cling"

2008-06-26

Static functions, like this singleton GetInstance method, are a sign of tight coupling.

Bad

class MyObject {
 public:
  int DoSomething(int id) {
    return TheirEntity::GetInstance().GetSomething(id);
  }
};

There is a way around this using the Repository Pattern.

Good

class TheirEntityRepository {
 public:
  virtual ~TheirEntityRepository() = default;
  virtual TheirEntity& GetInstance() = 0;
  // Other static methods here
};

class TheirEntityStaticRepository : public TheirEntityRepository {
 public:
  TheirEntity& GetInstance() { return TheirEntity::GetInstance(); }
};

class MyObject {
 public:
  explicit MyObject(std::unique_ptr<TheirEntityRepository> repository)
      : repository_(std::move(repository)) {}
  int DoSomething(int id) { return repository_->GetInstance().GetSomething(); }

 private:
  std::unique_ptr<TheirEntityRepository> repository_;
};

All thats left is to derive a MockTheirEntityRepository suitable for your testing needs.