472,993 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

Re: Provide access to a class without access to the constructor

On Fri, 11 Apr 2008 01:43:52 -0700, Weeble <cl************@gmail.com>
wrote:
The difference is that A has ConsumeB, which takes an IB as a
parameter.
That's not a difference. The code example you compared to has the
implication that A will use the IB instance passed back from the GetB
method. After all, that's why the return type for GetB is IB, rather than
B.
It can't take a B as a parameter, because B is private to
A. So it only has strong guarantees about the methods that are
available, not what they do.
That's why you define the interface in a way that provides the guarantee
you need. This doesn't enforce the semantics, but it does provide
implementors with very strond indications regarding what the
implementations should do

For example:
Well, in the rather more complicated example that inspired this, B
contains a transformation matrix which, at least when constructed by
A, is known to be a rotation. It's quite awkward (though certainly
possible) to verify if a given matrix is a pure rotation, but it's
quite easy to compose one out of other known rotations.
In that case, you need to write an interface that represents itself only
in terms of rotation. It can return a matrix (cloned, so that you don't
allow external mutation of internal state) as a convenience, but the API
should be strictly in terms of rotating. Even better is for the interface
to be immutable. You can only create an instance by providing a rotation,
or by combining existing instances. The interface would include the
latter operation, and of course the implementor provides the former.

You could either make the assumption that the implementor has followed the
semantics of the interface or, if you really think it's that important,
it's not hard to inspect a transformation matrix and determine whether it
only rotates. This is more flexible than insisting on your own
implementation, if doing that bothers you.

Personally, I think it's fine to make an assumption about whether the
interface is implemented with the intended semantics. For one, you never
know: perhaps some day someone will find a need to provide an
implementation that does something other than just rotate. For another,
if it's true that your own code will really break unless that condition is
met, then that should be readily apparent as soon as someone tries to use
an incorrect implementation. To some extent, I find it reasonable to rely
on testing to ensure correctness. Not everything about the correctness of
the code can be represented by the API that code exposes.

That said, I have the impression that you're not all that enthusiastic
about using interfaces to address this. And that's fine.
[...]
>You can't really guarantee that. But you can, as I mentioned in my
previous reply to your message, at least check the type of the
implementing instance to make sure it's B. Presumably that's really all
you care about anyway.

I certainly *could* do, but that feels very against-the-grain. It
seems like it should be possible to solve this at compile-time. I was
inspired by recent articles by Eric Lippert, such as:
http://blogs.msdn.com/ericlippert/ar...why-can-i.aspx
as well as his previous series on immutable data types. It made me
think that I could and should be using the type system and access
controls to provide stronger guarantees about the behaviour of my
classes.
Well, as has been noted, you can. I think that using "internal" is the
best way, personally. I agree that nesting the classes can get out of
hand, and for no real good reason.

You might need to adjust your thinking, so that you're willing to treat
assemblies as legitimate compilation divisions. But it seems like they
offer the separation you're looking for here.

Pete

Jun 27 '08 #1
1 1178
On Apr 11, 6:32 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 11 Apr 2008 01:43:52 -0700, Weeble <clockworksa...@gmail.com>
wrote:
The difference is that A has ConsumeB, which takes an IB as a
parameter.

That's not a difference. The code example you compared to has the
implication that A will use the IB instance passed back from the GetB
method. After all, that's why the return type for GetB is IB, rather than
B.
I don't quite follow you here. GetB returns an IB, but we do know it's
really a B. GetB is a public method, and B is private, so it could not
have a return type of B. In contrast, the IB passed into ConsumeB
really could be some non-B class.

[Rotation stuff]
In that case, you need to write an interface that represents itself only
in terms of rotation. It can return a matrix (cloned, so that you don't
allow external mutation of internal state) as a convenience, but the API
should be strictly in terms of rotating. Even better is for the interface
to be immutable.
I do like immutable classes. :)

That said, I have the impression that you're not all that enthusiastic
about using interfaces to address this. And that's fine.
In the end I didn't use interfaces for this one. Rotations have a
public constructor that guarantees to make a proper rotation, and a
private constructor that is used by the methods that compose or
transpose rotations.
You might need to adjust your thinking, so that you're willing to treat
assemblies as legitimate compilation divisions. But it seems like they
offer the separation you're looking for here.
Actually, I was originally going to use assemblies for these kind of
divisions, but apparently it causes some sort of problems to have too
many assemblies, so I've been discouraged from doing this. (I can see
that it's annoying to have to add lots of references to each project,
though.) And when I have one huge assembly with lots of classes, some
of which need access to others' internal methods, it gets hard to make
it clear which classes should be using the internal methods and which
shouldn't.

Thanks for all the advice! It's been an interesting puzzle, but I
think I've finally dodged the nasty parts and found a solution that's
going to be at least moderately idiot-proof.

Weeble.
Jun 27 '08 #2

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
4
by: Francisco Amaro | last post by:
Hi all, Have question about inheriting a class that has parameters in the constructor such as : Public MustInherit Class MyParentClass Public mystring As String Public Sub New(ByVal...
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
5
by: wpmccormick | last post by:
What is the cleanest way to gain access to object methods and properties across classes and files in the same namespace? Example: A form object frmForm in file frmForm.cs creates obj1 defined in...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
2
by: vijayrvs | last post by:
SearchCrawler.java The program search crawler used to search the files from the website. From the following program i got 7 compiler error. can any body clarify it and provide me solution. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.