1

I have a fairly simple application that is divided into two classes. The first class is the Manager class and the second class is the employee class. These are simple classes and do not inherit from any other class. Now what I want is to keep others from instantiating the employee class.The Manager class has an instantiated pointer to the employee class and controls all the methods of the employee class from there.My first idea is to keep the constructor of the employee as private and putting the manager as its friend. Such as this:

class employee
{
 friend class manager;
 private:
  employee()
  {
  }
}

class manager
{
  private:
  boost::shared_ptr<employee> emp;
  public:
  bar()
  {
    emp = boost::shared_ptr<employee>(new employee());
    //Now manager uses this pointer to control employee
  } 
} 

This is something i came up with but wanted to know if there was a design pattern or a better approach to accomplish this . My objective is to prevent others from creating or using the employee class the only drawback i see to this is exposing the private variables of employee class to the manager class

1

I guess the way you have solved the problem of having a private Constructor might just serve your need. I don't see any creational design pattern fit this requirement, but you should be fine without one here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.