Demeter Law and OOP confusion

Recently, I was engaged in reading and ran into the Law of Demeter. Now, some of what I read makes perfect sense, for example. a wallet will never be able to shoot through a pocket for customers, grab a wallet and pull out money. A wallet is something that a client should control, not a paperboy .

What throws me into the law, maybe I just misunderstand all this, is that string properties along with a hierarchy of functionality / information can be so useful. e.g. HTTPContext.NET Class.

There will be no code, for example:

If DataTable.Columns.Count >= 0 Then
   DataTable.Columns(0).Caption = "Something"
End If

or

Dim strUserPlatform as string = HttpContext.Current.Request.Browser.Platform.ToString()

or

If NewTerm.StartDate >= NewTerm.AcademicYear.StartDate And 
   NewTerm.EndDate <= NewTerm.AcademicYear.EndDate Then
   ' Valid, subject to further tests.
Else
   ' Not valid.
End If

violate this law? I thought (perhaps erroneously) that the OOP point was partially provided for accessing related classes in a good hierarchical structure.

, , toolkit, , , :

Dim strUserInput As String = "London, Paris, New York"
For Each strSearchTerm In Tools.StringManipulation.GetListOfString(strUserInput, ",")
    Dim ThisItem As New SearchTerm
    ThisItem.Text = strSearchTerm 
Next

... , , , ... , ? , , , , , , :)

+5
3

( " /" ) , " ", , . .

, , , , , :

function getPayment(Customer customer)
{
    Money payment = customer.leftpocket.getWallet().getPayment(100);
    ...
    // do stuff with the payment
}

, , , :

function getPayment(Money money)
{
    // do stuff with the payment
}

, , , , . , . .

+6

, . , - . , - HTTP, - .

, , , , , DataTable HttpContext, .

+3

, - , , .

, , , - Count:

DataTable.Columns.Count = 42;

Add Columns, , , .

+1

All Articles