Re: Provide access to a class without access to the constructor
- From: Weeble <clockworksaint@xxxxxxxxx>
- Date: Thu, 10 Apr 2008 10:27:40 -0700 (PDT)
On Apr 10, 6:16 pm, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@xxxxxxxxx> wrote:
On Apr 10, 11:43 am, Weeble <clockworksa...@xxxxxxxxx> wrote:
Have a public interface, IB, and make B a private class inside of A.
This is not good because other code can still create instances of IB,
which might not behave at all like B, and pass them as arguments to
methods on A.
I'm assuming this is the solution you're referring to, since it's the
only one that uses an interface.
Not really, if the class B is declared as private you do not have that
problem:
public class A {
public IB GetB() { return new B(); }
class B: IB {
public string getIt() { return "hello"; }
}
}
public interface IB { string getIt();}
What if it's more like this?
public sealed class A {
public IB MakeB() { return new B(); }
public void ConsumeB(IB b) { ... }
class B: IB { ... }
}
And what if B makes some guarantees about its behaviour that
A.ConsumeB relies on? A could then be broken by a bad implementation
of IB. I would like to guarantee that every B passed to ConsumeB was
created by A.MakeB. (It doesn't matter which instance of A made the
instance of B.)
.
- Follow-Ups:
- Re: Provide access to a class without access to the constructor
- From: Peter Duniho
- Re: Provide access to a class without access to the constructor
- References:
- Provide access to a class without access to the constructor
- From: Weeble
- Re: Provide access to a class without access to the constructor
- From: Ignacio Machin ( .NET/ C# MVP )
- Provide access to a class without access to the constructor
- Prev by Date: Re: Strange problem when not in debugger
- Next by Date: Re: Serialiazing pure HTML via DataContractSerializer (CDATA?)
- Previous by thread: Re: Provide access to a class without access to the constructor
- Next by thread: Re: Provide access to a class without access to the constructor
- Index(es):