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

asked Dec 3, 2014 at 22:59

1 Answer 1

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.

answered Dec 3, 2014 at 23:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.