# Is splitting to small classes good?

I have materials.

class Material {...};
class ConcreteMaterialA : public Material {...};
class ConcreteMaterialB : public Material {...};


I want it has a name

class WithName
{
public:
const string& Name() const          {return m_name;}
void Rename(const string& name)     {m_name = name;}

private:
string m_name;
}


And tracking its users

template<class UserTy>
class WithUsers
{
public:
const std::vector<T*>& Users() const    {return m_users;}

private:
std::vector<T*> m_users;
};


And wrapped with pointer

template<class T>
class WithPtr
{
public:
template<class U> T* Get() const;
template<class U> void Reset(U* u);
template<> T* Get<T>() const            {return m_object.get();}
template<> void Reset<T>(T* t)          {return m_object.reset(t);}

private:
std::unique_ptr<T> m_object;
};


Then I define my new material

class NewMaterial
: public WithPtr<Material>, public WithName, public WithUsers<MaterialUserType>
{}


It is kind of complex. I have many types need name, tracking users. Is it a good design? Thanks a lot.

-
It might be suggested to you that your post go to code review, but I'll answer via a comment. I was trained in C++ in 1992 at a course at Harvard Extension School. The people who taught the course were some of the principles of ObjectDesign (now part of Progress). Their teaching philosophy was small, tight, well-written SmallTalk style single inheritance classes. Their teaching served me well over the years. I could always go back and understand what I had done and modify it. Other luminaries will have differing opinions, which can't hurt you either. –  octopusgrabbus Dec 28 '12 at 22:45
@octopusgrabbus: It may be worth (or not) noting that you were taught pre-standard C++. It's changed substantially in the ensuing twenty years, not least by being standardised... thrice. Also you mean "principals" I believe. :) I write this comment mainly because I'm bored, not because I particularly disagree with anything that you say –  Lightness Races in Orbit Dec 28 '12 at 22:53
Why does "name" have to have it's own class? It's just a string, so there's no need to have it in a class as a resource cleanup item. Just stick string name as a member, and a getName() function in the classes that have names. Unless you specifically need some sort of "do this to all classes that have names" method somewhere. I think classes should be self-contained units as much as possible, but making things TOO small is just as big a problem as making them large. –  Mats Petersson Dec 28 '12 at 23:13
This looks crazy complicated, perhaps you could give more details about why you think this is a good idea. –  Winston Ewert Dec 29 '12 at 23:50

## migrated from stackoverflow.comDec 29 '12 at 15:26

This question came from our site for professional and enthusiast programmers.