C# Limit creation of class instance to within namespace
Asked Answered
Y

4

9

I have two objects, RoomManager and Room, there will be several Rooms and one RoomManager. I want the RoomManager to be the only one allowed to create a Room object. So I'm wondering if there is a way to make the Room constructor (and the rest of the Room methods/properties) only accessible to the RoomManager. I was thinking maybe moving them to their own namespace and making Room private or internal or something. From Accessibility Levels (C# Reference) I see that internal is for the entire assembly though, not just the namespace.

Youmans answered 13/4, 2011 at 7:6 Comment(4)
Whenever I see XManager I remember this article: codinghorror.com/blog/2006/03/…Hujsak
You could make Room a private class inside RoomManager?Bison
@Nicholas, nice article.. must read. :DAbysm
@Nicholas, good article, I hadn't read that one. I also like the first comment "Well I feel smart. I just renamed every *Manager class to *Helper." :)Youmans
W
10

No, C# (and .NET in general) has no access modifiers which are specific to namespaces.

One fairly hack solution would be to make Room just have a private constructor, and make RoomManager a nested class (possibly just called Manager):

public class Room
{
    private Room() {}

    public class Manager
    {
        public Room CreateRoom()
        {
            return new Room(); // And do other stuff, presumably
        }
    }
}

Use it like this:

Room.Manager manager = new Room.Manager();
Room room = manager.CreateRoom();

As I say, that's a bit hacky though. You could put Room and RoomManager in their own assembly, of course.

Wisent answered 13/4, 2011 at 7:9 Comment(3)
Room and RoomManager will only ever be needed for one program, so putting them in their own assembly seems like overkill. Is your "Manager class inside Room class" approach too hacky to actually be used?Youmans
@jon: i would say its not hacky at all, it is a great way how to make these kind of controllers, managers ect ;)Piane
#586359Dioptrics
I
4

You can do something like this:

var room = Room.Factory.Create();

If the constructor of Room is private, it will still be accessible from Room.Factory if you declare the factory class inside the Room class.

Inescutcheon answered 13/4, 2011 at 7:11 Comment(0)
G
1

Defining the Room inside of RoomManager itself and making it's constructor private could be helpful.

EDIT : But the best solution I think is that to extract an abstract class of the Room, and expose that class to clients.

No one can create an instance of the abstract class.

Gonfalonier answered 13/4, 2011 at 7:10 Comment(2)
In this case the constructor won't be accessible from RoomManager.Inescutcheon
I'm sorry, I'm not following with the "extract an abstract class of the Room" part. I do need to create instances of the Room class. Could you clarify a bit?Youmans
V
0

You should implement 'Singleton pattern'. It creates object only once and any subsequent attemps to create object of that type will actually returns already created object.
You should create class factory which would implement singleton for RoomManager. Room, in turn, should be private type of RoomNamager. RoomNamager manager should have method for Room creation. But in this case you cannot access Room properties outside of RoomManager class. To solve this problem I recomend you to create IRoom public interface which would grant access to room functionality. So your create room method would return IRoom interface.

Volar answered 13/4, 2011 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.