I'm designing a system, and it needs future expandability for the use of a permission system of some kind. I'm wondering if the flyweight pattern would be a good choice to implement this. I'm not responsible for the implementation right now, as it is just a prototype, and we're not prototyping any parts that need the right system. However, because of the demand for future extensibility to parts that need permission management the interface needs to be solid. This means that in the end it will be a thought experiment, rather than a real part of what I have to work on. However, I do need to be able to explain and justify my design in this area of the application.

In favour of using the are that you can define permissions by having them symbolized through a token class, as represented in the flyweight pattern. If you're dealing with hundreds of users, this would somewhat simplify the handling and issuing of the rights a user holds, and the required rights a user needs for an action within the system; as well as the memory usage of the rights assigned to all the users. In the system I have in mind, a factory method of some kind will assign the rights needed at construction time.

As I'm not really experienced with designing with security in mind, I'm having a paranoid line of thought of which I can't determine if it's justified, security wise. A shared pointer could be hijacked by 'evil intruders' to gain rights they should not be getting. This is the major argument against the use of a flyweight that keeps bugging me, Even though the 'how' is undefined, and I wouldn't know how someone would get it done. (no experience in the security mindset, or it's workings. However, I'm not really looking for a lecture in security beyond the secure use of patterns, unless motivated and clearly related to the question)

Q: Is the flyweight pattern a suitable pattern to manage the representation of the rights of a user (or other some 'hand out' type of data) in the design of a software system that needs to be secure by design? Does the use of flyweight objects as representation of permissions pose more of a security risk than other patterns (I could also use a decorator chain for it, even though that would take up more memory) when dealing with permissions?


1 Answer 1


If you're trying to design the interface, why are you worrying about the implementation details? Even on the level of making an implementation, I think worrying about whether to use flyweight is premature. Flyweight is a memory-space optimization, and if you don't know how much space you're using to start with, how do you know it will save you space?

You don't mention what kinds of things you'll be tracking with the permissions, so I'll assume that it's about the same amount as unix-style read/write/execute bits for user, group, and other. That's a total of 9 bits; depending on how you slice it, it will fit in 2 or 3 bytes. Each flyweight would need a pointer, probably in the range 32-64 bits, or 4 to 8 bytes. So, even if you didn't need to allocate any more space for the permission objects (and you would), you're probably increasing memory requirements.

Of course, that might not be true if your permissions objects are large enough, but in that case, you'd still get nothing from flyweight unless there was a significant amount of duplication in your large objects. Do you know that you would get a lot of duplication among your objects and that they'd be relatively large? Probably not, if you're still figuring out the interface.

I can't really tell you about security, but it seems to me that if anyone has penetrated your system to the point that they can overwrite an arbitrary pointer, they already have control over your program.

  • I don't want to bash you, but the security is at the heart of this question. However, I'm well aware of the purpose of the flyweight as a memory saving pattern, and that this might not be its ideal case. I thought that if you'd have 1k people loaded in the system, then it might be a factor. I was worried about the security aspect specifically though, the rest is all up for debate, really. Currently there are about 10 different kinds of permission planned, and it needs to be very expandable for new operations that might be added later. Flyweight came to mind as a flexible way of managing those.
    – Onno
    Commented Mar 31, 2014 at 1:54
  • I wouldn't take that as bashing, just clarifying what you meant. However, as the question stands now, the fact that the word 'security' isn't mentioned in the title of the question, and that you state that "I'm not really looking for a lecture in security beyond the question at hand" imply that security isn't the heart of the question. Commented Mar 31, 2014 at 3:30
  • Well I wanted to avoid getting slammed with the type of answers/comments that basically tell me to go learn security for security's sake, and that don't deal with my use of flyweight for permissions.
    – Onno
    Commented Mar 31, 2014 at 3:33
  • I've made an edit to make the limit on the scope of the answers look less dramatic (At least, I hope it does that) Thank you for pointing out the effect of that particular line
    – Onno
    Commented Mar 31, 2014 at 4:03

Your Answer

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

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