Page 1 of 1

Why Separation Of Concerns matters? Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,443
  • Joined: 29-May 08

Posted 30 October 2012 - 03:21 PM

Why Separation Of Concerns matters?

This tutorial is in response to

View Postvitroblue, on 29 October 2012 - 08:50 PM, said:

1 material data is stored in global multidimensional arrays (dimensions, weights, prices, accessories needed, assembly time, this and that all so pretty)


This should be suggesting that you need to separate concerns and models. eg Objects

Sorry but I'm going to use VB.net (but the principal remains the same for C#

Let's look at Dimensions and Weights.




To me, I consider these both measurements (albeit in different domains).

Measurement Base Class
I'll devise a common base class Measurement.
Namespace Global.Measurements
  Public MustInherit Class Measurement
  End Class
End Namespace



Decimal Measurement
This base class will be for any measurement that is in a decimal format. eg. 10Kgs 340mm
Namespace Global.Measurements
  Public MustInherit Class DecimalMeasurement
    Inherits Measurements.Measurement

    Public ReadOnly Value As Decimal
    Private Symbol As String
    Private RightSideSymbol As Boolean

    Friend Sub New(Value As Decimal, Symbol As String, Optional RightSideSymbol As Boolean = True)
      Me.Value = Value
      Me.Symbol = If(Symbol, "")
      Me.RightSideSymbol = RightSideSymbol
    End Sub

    Public Overrides Function ToString() As String
      Return If(RightSideSymbol,
                String.Format("{0} {1}", Me.Value, Me.Symbol),
                String.Format("{0} {1}", Me.Symbol, Me.Value))
    End Function
  End Class
End Namespace




Now I can build different measurement on top of.

Let do a measurement of length, so I differentiate them from other measurement I'll create another base class Distance

Namespace Global.Measurements
  Namespace Lengths
    Public MustInherit Class Length
      Inherits Measurements.Measurement
      Friend Sub New(Value As Decimal, Symbol As String)
        MyBase.New(Value, Symbol)
      End Sub
    End Class
  End Namespace
End Namespace


Now I can implement on top that base different units of measurement for length or distances.
Let do millimetres.
Namespace Global.Measurements
  Namespace Lengths
    Public Class Millimetres
      Inherits Measurements.Lengths.Length
      Public Sub New(Value As Decimal)
        MyBase.New(Value, "mm")
      End Sub
    End Class
  End Namespace
End Namespace



What about Weights?
Namespace Global.Measurements
  Namespace Weights
    Public MustInherit Class Weight
      Inherits Measurements.Measurement
      Friend Sub New(Value As Decimal, Symbol As String)
        MyBase.New(Value, Symbol)
      End Sub
    End Class
  End Namespace
End Namespace



Namespace Global.Measurements
  Namespace Weights
    Public Class Kilograms
      Inherits Measurements.Weights.Weight
      Public Sub New(Value As Decimal)
        MyBase.New(Value, "Kgs")
      End Sub
    End Class
  End Namespace
End Namespace







So now we have some basic measurements, let put them to use.

Namespace Global.Measurements
  Namespace Dimensions
    Public MustInherit Class Area_Base
    End Class

    Public Class Area
      Inherits Area_Base

      Public ReadOnly Width As Measurements.Lengths.Length
      Public ReadOnly Height As Measurements.Lengths.Length
      Friend Sub New(Width As Measurements.Lengths.Length,
                     Height As Measurements.Lengths.Length)
        Me.Width = Width
        Me.Height = Height
      End Sub

      Overrides Function ToString() As String
        Return String.Format("{0} x {1}", Width, Height)
      End Function
    End Class


    Public Class Volume
      Inherits Area
      Public ReadOnly Depth As Measurements.Lengths.Length
      Public Sub New(Width As Measurements.Lengths.Length,
                     Height As Measurements.Lengths.Length,
                     Depth As Measurements.Lengths.Length)
        MyBase.New(Width, Height)
        Me.Depth = Depth
      End Sub

      Overrides Function ToString() As String
        Return String.Format("{0} x {1}", MyBase.ToString(), Depth)
      End Function
    End Class
  End Namespace
End Namespace





Why do this?
  • I can extract and separate the measurement out and create a Library project(s)
  • Implement them a NuGet package(s)
  • Import them into future projects.
  • Develop them independently.
  • Debug them independently.
  • The IDE and your API can enforce correct usage.
    Is that Decimal 123 and Length or Weight. By using a Type we can define that context.

    AssemblyTime is not the same as DeliveryTime
  • I can model interaction and process at the correct conceptual model or Abstraction level.
    I have a Shopping Cart object and can put Items it.
  • The model is independent of GUI.

This post has been edited by AdamSpeight2008: 30 October 2012 - 03:36 PM


Is This A Good Question/Topic? 2
  • +

Page 1 of 1