S
Scott M.
Sounds like a job for Interfaces or Polymorphism to me.
is on machineX.
a bunch of blind and drunk teenagers could have come up with better
execution and a better strategy... oh wait; they already DID it is
called GOOGLE
Sounds like a job for Interfaces or Polymorphism to me.
(like "Main" or "appStart", etc.) and then you'd be able to use those
qualifiers (or, my preference - - import the namespace at the top of your
code and NOT qualify it.
Public Interface IDoor
Property LockType As Integer
Property Material As String
ReadOnly Property IsLocked As Boolean
Sub Lock
Sub UnLock
End Interface
Public Class Car
Implements IDoor
'This class must now implement all of the members of the IDoor
interface.
'The actual functionality of the interface members is left up to this
class creator
'but any Car instance will be guaranteed to have a Door interface with
the
'appropriate methods and properties
End Class
Public Class House
Implements IDoor
'This class must now implement all of the members of the IDoor
interface.
'The actual functionality of the interface members is left up to this
class creator
'but any House instance will be guaranteed to have a Door interface with
the
'appropriate methods and properties
End Class
'***********************************************
Public Class DoorUser
Public Sub UserDoor(x As IDoor)
'Since this method must be provided a type (any type) that implements
'the IDoor interface, we know for sure that it will be able to use
'any of that interface's properties & methods. This is Polymorphism
x.Lock
Dim y As Boolean = x.IsLocked
End Sub
End Class
- Show quoted text -
one thing, Interfaces imply a "type of" relationship - and a car and a
house are not types of doors. They have doors - so this is better
expressed intermes of a "has-a" relationship - and that means
aggregation... Most likely, I would create an abstract door class and
then inherit various door types from it.
<snip>Interfaces are just that,
interfaces. They do not imply a "type" at all, they only imply a way of
interacting or "interfacing" with a type.
Actually Tom, I think you've got that backwards. Interfaces are just that,
interfaces. They do not imply a "type" at all, they only imply a way of
interacting or "interfacing" with a type.
An abstract class is, in fact, a
type and so it would indicate a "type" when inheriting from it.
Yep.
Also, since
we can't inherit from more than one type at a time, Interfaces become an
important way of getting where we need to go.
Also, through the use of
interfaces, we can get the polymorphic behavior that is essential when
talking about a property or method that 2 unrelated types have, but use in
different ways.
<snip>
All that said - your implementation was an inappropriate use of
interfaces. A door is an object in it's own right. A house is not a
door, nor does it behave as a door. A house has a door and uses a
door. Same with a vehicle. The relationship is better expressed in a
"has-a" relationship.
</snip>
I don't belive you can acurrately say that my use of interfaces is
"inappropriate", you can only say that you would choose to "implement" a door
in a differet way that I chose to.
There is absolutely nothing wrong (or inapproprate) about the code that I've
shown. Quite literally, a door is an interface to ahouse or car and my code
allows either a house or a car to expose that interface, but in ways that are
specific to the individual types.
I agree that interfaces *can* be used as abstract types (such as IDisposable
or ISerializable), but that doesn't mean that they *must* be or, for that
matter *can't* be used, in just as valid of a way as I've shown. No, a house
is not a door, nor is a car a door, but you, by your own admission, are using
your own "has a", "type of" and "is a" rules here when, I belive they are not
applicable.
Historically, the "is a" test has been used to see if inheritance applies.
If you must brand your own terminology to this, stick with the "has a" test.
A house "has a" door and so does a car.