2

Which design pattern should I use when I have a class representing a data structure (aka C-style struct) and I wish to have only a single class to be able to access it.

I was thinking of simply declaring the class that represents a data structure as an inner class but wanted to know if there is a better way to design this.

eg:

class XXManager{
getXX();
setXXAtrtribute();
}
class XX{
 String name;
 String email;
 String job;
}

Here I want to design a class XXManager which will handle the creation and managment of 'XX' objects and no one should be able to access XX other than through the XXManager.

asked Sep 20, 2014 at 15:12
5
  • I don't know of a better solution. Commented Sep 20, 2014 at 15:36
  • 2
    Hard to say without knowing your wider design, but it's quite likely this isn't a good goal in the first place. Commented Sep 20, 2014 at 16:44
  • 1
    Inner classes are a perfect fit for this. Commented Sep 20, 2014 at 16:47
  • Take a look at nested classes. Commented Sep 20, 2014 at 18:14
  • The possible solutions to this are very language dependent. Do you have any constraints on the language? It looks like its Java like (indenting and name conventions), but that could just be Java style pseudo-code too. Commented Feb 17, 2015 at 21:18

4 Answers 4

1

To me it seems like you might want to take a look at the Proxy design pattern. With that said not everything you create has be based on design patterns. It might lead to some overengineering.

answered Sep 20, 2014 at 16:21
0

Making XX an inner class, or a base class of XXManager will both work. If you want to use it as a base class you could mark it abstract to prevent direct instantiation.

You could also combine the two classes--if you really must insist that they work with an XXManager and they do not work with XX then removing XX might be the simplest solution.

In all cases it feels a bit strange and raises the question of "why"? Perhaps it would be best to leave the classes independent and accessible and to rely on training or understanding enforce the standard.

answered Oct 20, 2014 at 18:00
0

Cannot quickly find any links, but I was taught that naming something an XXManager is a "code smell" and you should strongly consider naming it XX.

Why do you want an Anemic class XX with a separate XXManager?

Sometimes it makes sense to have a separate manager, e.g. if you are religiously following MVC, or a Database is involved, but is that the case here?

Assuming you want to do this, an inner class is one solution. Alternatively, in Java you could put all the classes in the same package and use package access. That isn't perfect, as an unscrupulous hacker could create a rogue class in the same package and abuse your data. So don't do that if you are creating some security library. Bu if the code is internal and you trust your fellow coders it is a good solution.

answered Mar 20, 2015 at 4:57
1
  • 1
    Using Manager in the name is "code smell" because it means you haven't defined what the class is really supposed to do. Thus, by calling it a manager it can do anything and everything even tangentially related to XX. A good class name should not only tell you what the class does, but should also tell you what the class doesn't do. Manager does anything but that goal. Commented Mar 20, 2015 at 19:20
0

I make an assumption that your implementation language is Java, due to the syntax in the description.

Your methods getXX and setXX of XXManager indicates that another class is responsible for both creating and using your XX class. Thus it cannot be a private inner class of XXManager.

STW has a point in that Manager is a bad word for a class. What does it really do? To me it seems to be holding just an instance of XX and nothing more, in which case it doesnt really make sense for the class to exist.

answered Apr 19, 2015 at 6:35

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.