I’m using VB.NET but this question applies to any OO language – Java, C# etc. I’ve tagged the question with Java and C# but will remove if not considered appropriate.
I have an entity class ClassAAA that contains a reference to ClassBBB as one of it’s attributes:
Public Class ClassAAA
Private Property _blah As ClassBBB = Nothing
Private Property _somethingElse As String
Private Property _anotherthing As String
' Make it private so cannot instantiate class without supplying ClassBBB '
Private Sub New()
End Sub
Public Sub New(ByRef value As ClassBBB)
_blah = value
End Sub
Public Property ImportantAttribute As String
Get
Return _blah.Imp
End Get
Set(ByVal value As String)
_blah.Imp = value
End Set
End Property
' other stuff '
End Class
I don’t want to expose ClassBBB outside the entity class. This is my question: I want to do an update on ClassAAA and I have a static / Shared update method for ClassBBB that I want to use. What’s the best way to do this?
Public Shared Sub UpdateClassAAA(byRef value as ClassAAA)
' XXX Cannot do this because ClassBBB reference is private XXX '
ClassBBBDAO.update(value._blah)
' Do other updates on other ClassAAA attributes '
End Sub
I could make the ClassBBB reference public (or supply a public getter method) but that would expose the inner workings of ClassAAA.
What I’m thinking of doing is making the ClassBBB reference Friend so that it is only visible in the assembly i.e. a data layer and is not visible to client projects. But this stills feel like I’m exposing the inner workings of the class.
Is there a better approach to this?
If a
ClassAAAhides and encapsulatesClassBBBthen it isClassAAAthat should have a public method to internally updateClassBBB. The facade design pattern is an excellent example that illustrates a solution to your problem in a nice and elegant way.