Original Post
Is giving another class access to the private and protected members of a class possible and if so, how?
Quote:
Original post by Narf the Mouse
Is giving another class access to the private and protected members of a class possible and if so, how?
Quote:
IIRC, it is possible to access private members via reflection. A quick google of "set private member by reflection" should turn up the methods.
Quote:
Original post by Narf the Mouse
...I was thinking it might make my Factory methods more secure, if only they could access the creation functions. *Dry*
Thanks for the info.
Quote:
Original post by TheTroll
Why don't you just initialize in the constructor?
theTroll
Quote:I've never suffered from unwieldy constructors, so it makes me wonder what you're doing differently. I'd say it's probably a sign that your class is getting overloaded with responsibility.
Original post by Narf the Mouse
A constructor can get large and unwieldy.
Quote:Depending on the language you might be able to use constructor chaining.
Plus, then you get multiple constructors. So you make a Set/Initialize method. And then SubSet/SubInitialize methods.
Quote:You still need to construct and initialise the object though, so either you still have the constructors (so you haven't gained anything) or you have lots of setters/properties for all internal variables (which is foul).
...Or, you make a Factory that makes Whatever according to a pattern.
Quote:
Original post by Narf the Mouse Quote:
Original post by TheTroll
Why don't you just initialize in the constructor?
theTroll
Separation of logic. A constructor can get large and unwieldy. Plus, then you get multiple constructors. So you make a Set/Initialize method. And then SubSet/SubInitialize methods.
...Or, you make a Factory that makes Whatever according to a pattern.
Map map = Map.Create(sizeX, sizeY); List<Room> rooms = new List<Room>(); rooms.Add(Room.Create(map, RoomType.Room, new Point(8, 8))); List<Door> unusableDoors = new List<Door>(); Door[] doors; Room currentRoom; Room tempRoom; int n = 0; int retryDoorsCount = 0, maxRetryDoorsCount = 25; { for (n = 0; n < rooms.Count; ++n) { unusableDoors.Clear(); currentRoom = rooms[n]; doors = currentRoom.GetDoors; tempRoom = null; foreach (Door door in doors) { RoomType roomType = (RoomType)GeneralData.Random.Next((int)RoomType.Total); if (roomType == RoomType.Hallway) tempRoom = Room.Create(map, roomType, door, new Point(15, 15)); else if (roomType == RoomType.Room) tempRoom = Room.Create(map, roomType, door, new Point(8, 8)); if (tempRoom != null && rooms.FindOrDefault(a => { return Room.Intersection(tempRoom, a) && tempRoom != a; }) == null) { rooms.Add(tempRoom); } else unusableDoors.Add(door); } if (unusableDoors.Count > 0) { currentRoom.RemoveDoors(unusableDoors); if (currentRoom.GetDoors.Length == 0 && retryDoorsCount < maxRetryDoorsCount) { currentRoom.GenerateDoors(map); --n; ++retryDoorsCount; } else retryDoorsCount = 0; } else retryDoorsCount = 0; } } foreach (Room room in rooms) { map.Carve(room); map.Draw(); } foreach (Room room in rooms) foreach (Door door in room.GetDoors) map.Carve(door); return map;Quote:
Original post by Narf the Mouse
Put your money where your mouths are. Here's my roguelike map generation code that I wrote today. Refactor it to a better state, changing only the constructor use.
Any comments about any other part of the code (Which is rough draft) will be ignored.
*** Source Snippet Removed ***
Quote:
Original post by ChaosEngine Quote:
Original post by Narf the Mouse
Put your money where your mouths are. Here's my roguelike map generation code that I wrote today. Refactor it to a better state, changing only the constructor use.
Any comments about any other part of the code (Which is rough draft) will be ignored.
*** Source Snippet Removed ***
Do your own damn work. I'm not going to fix your code for you and I'm certainly not going to attempt to fix up a snippet of code without any context.
Quote:
Original post by Narf the Mouse
...I was thinking it might make my Factory methods more secure, if only they could access the creation functions. *Dry*
Thanks for the info.
Quote:
Original post by Telastyn
What parts do you have problems with?
I mean there's very few constructors actually being invoked directly in that code. Personally, I don't see a Room as having any invariants it needs to keep (except perhaps a Map reference to set some readonly field), so it should have a very simple constructor.
Quote:
Original post by ernow Quote:
Original post by Narf the Mouse
...I was thinking it might make my Factory methods more secure, if only they could access the creation functions. *Dry*
Thanks for the info.
A way of getting the desired effect is by adding the factory methods as static methods to the class itself. That way you can make the properties/methods/constructors private and have the static Factory method access them.
BTW: I am not too fond of using statics but this might be one of those times.
A nicer design, if you are that concerned about 'security', is by using internal and putting the class and its factory class in one assembly (dll).
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more