473,473 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Design Question WRT controlling access to private methods

I have a situation where I need to control the creation of a class.
I need to be able to create it externally but only by classes that I define.
To accomplish this I thought of the following process:

Define a delegate that returns an instance of my class.
Pass this an instance of this delegate to any classes that I want to be
able to create instances of myclass.

i.e.

//Safe class
using System;
using System.Collections.Generic;
using System.Text;

namespace DelegateCreation
{
public class MySafeClass
{
public delegate MySafeClass CreateMySaveClassDelegate();

private MySafeClass()
{
}

private static MySafeClass Create()
{
return new MySafeClass();
}

public static void Load()
{
List<MySafeClass> instances =
ClassCreator.CreateMySafeClass(new CreateMySaveClassDelegate(Create));
}
}
}

//Class creator
using System;
using System.Collections.Generic;

namespace DelegateCreation
{
public class ClassCreator
{

public static List<MySafeClass>
CreateMySafeClass(MySafeClass.CreateMySaveClassDel egate myDelegate)
{
List<MySafeClass> returnList = new List<MySafeClass>();
returnList.Add(myDelegate.Invoke());
return returnList;
}
}
}
Jun 8 '06 #1
2 980
John,
I have a situation where I need to control the creation of a class.
I need to be able to create it externally but only by classes that I define.
To accomplish this I thought of the following process:

Define a delegate that returns an instance of my class.
Pass this an instance of this delegate to any classes that I want to be
able to create instances of myclass.


And your question is what exactly?

The code you posted doesn't seem to fulfill your "only by classes that
I define" requirement. A delegate doesn't add any "security", just a
level of indirection.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 8 '06 #2
Mattias Sjögren wrote:
John,
I have a situation where I need to control the creation of a class.
I need to be able to create it externally but only by classes that I define.
To accomplish this I thought of the following process:

Define a delegate that returns an instance of my class.
Pass this an instance of this delegate to any classes that I want to be
able to create instances of myclass.


And your question is what exactly?

The code you posted doesn't seem to fulfill your "only by classes that
I define" requirement. A delegate doesn't add any "security", just a
level of indirection.
Mattias

Yes it does, because I (MySafeClass) am the only one that can create
this delegate pointing to my (private) Create method.
Jun 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Neil Zanella | last post by:
Hello, It seems to me that using too many variables at class scope in C++ (e.g. private data members) can be just as bad as having a C program with lots of global variables. This is especially...
4
by: TheFerryman | last post by:
Hi, I keep running up against the same design problem. Let's say I have a cat object like this: //-----------------------------------------------------------------------------------------...
2
by: Rubén Campos | last post by:
I want to write a class to manage keyboard SHIFT, ALT and CTRL modifier keys in an event management system, distinguishing between left and right keys of each one. This seems trivial, but I've...
6
by: Baris | last post by:
Given the C# code below, can anyone think of a better class design? What really gets my goat is that the code within derived classes D1 and D2 is identical and in my mind should be refactored into...
13
by: John Kraft | last post by:
Friends, I'm working on some crud stuff, and I was looking for opinions on the subject. Below, I have pasted some VERY simple sample code. Class2 is a "traditional" crud type object. In a...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
0
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base...
4
by: sunderjs | last post by:
Hi, This is from a typical telecom software implementation. I have three subsystems (x, y, z) which exchange data amongst them. The arrangement is such that x talks to y over interface xy. y...
7
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.