Wednesday, October 2, 2013

Design Pattern - Immutable Pattern លំនាំមិនផ្លាសផ្តូរ

លំនាំនែការរចនា - លំនាំមិនផ្លាសផ្តូរ
Design Pattern - Immutable Pattern
====================================


Why Use: គោលដៅ
Reduce the overhead of concurrent access to an object.
Avoids the need to synchronize multiple threads of execution that share an object.

Thought:​គំនិត
Not allow the shared object's contents to change after the object is constructed.

Implementation: ការអនុវត្តន៏
Only constructors should modify the Class's instance variables.
The Class should have no writable properties.
None of its functions or methods should modify the values of its instance variables.

Sample Coding:

Public Class Position
    Private myX As Integer
    Private myY As Integer

    'Constructor
    Public Sub New(ByVal x As Integer, ByVal y As Integer)
        myX = x
        myY = y
    End Sub

    Public ReadOnly Property X() As Integer
        Get
            Return myX
        End Get
    End Property

    Public ReadOnly Property Y() As Integer
        Get
            Return myY
        End Get
    End Property

    Public Function Offset(ByVal xOffset As Integer, ByVal yOffset As Integer) As Position
        Return New Position(myX + xOffset, myY + yOffset)
    End Function
End Class


Any Talk: