Created December 13, 2009
Last updated September 11, 2010
[ Japanese | English ]

Encapsulation , Attributes and Operations

  In this page , I will explan the encapsulation.
  Ahead of that , if you don't understand the Classes and Object, please read "Classes and Objects".

  I will explain "What is the encapsulation?" and "What is good concretely when you encapsule?".

  The encapsulation is one of three large elements of object-oriented.
  The others are the inheritance and polymorphism , but I think that the encapsulation is more important ( maintainable and readable ) than those.

1. What is the encapsulation?
2. What is good concretely when you encapsule?


1. What is the encapsulation?

  For the time being, please read the quotation from e-Words (Sorry , this is a Japanese page ).
(Attention : The following is translated it into English form Japanese by the machine translation.)
  It is one of the features of object oriented programming. Data and the procedure that operates it are integrated, it defines as "Object", and a detailed specification and the structure in the object are concealed from the outside. The independence of an individual object rises because it manipulates data only by using the procedure open to the public from the outside. The change of the specification in the object doesn't influence outside by advancing encapsulation, maintainability and the development efficiency of software rise, and it becomes easy to recycle the program partially.

From e-Words

  I'm sorry.
  I could not understand so much.
Back to Top

2. What is good concretely when you encapsule?

  In the page of the "POA , DOA and Object-oriented Approach 3. The POA , DOA and OOA", I explained that the base of object-oriented is the DOA.

  Please look at the following program that is encapsuled and that isn't so. And let's think what is good.
  The programming lanuage is the VBA.

When not encapsuled
(Class Name : Class1)

When encapsuled
(Class Name : Class2)

Public number as Integer

Private m_number As Integer

Public Property Get number() As Integer
    number = m_number
End Property

Public Property Let number(ByVal newNumber As Integer)
    m_number = newNumber
End Property


  The followings are 2 samples that uses the Class1 and Class2.

The sample that uses Class1

The sample that uses Class2

Sub main()
    Dim obj As Class1
    Set obj = New Class1
    obj.number = 1000
    Debug.Print obj.number
End Sub
Sub main()
    Dim obj As Class2
    Set obj = New Class2
    obj.number = 1000
    Debug.Print obj.number
End Sub


  The same ?
  Are there merits of the encapsulation?
  Even if you think so, it is not wrong.
  If the access methods (In VBA , there are Property Get Procedure , Property Let Procedure and Property Set Procedure ) are only defined, I think that there are no merits.

  Then , let's think the following that is the case that number attribute of the class is 0 or more and 100 or less.

When not encapsuled
(ClassName : Class3)

When encapsuled
(ClassName : Class4)

Public number as Integer

Private m_number As Integer

Public Property Get number() As Integer
    number = m_number
End Property

Public Property Let number(ByVal newNumber As Integer)
    If (newNumber < 0) Or (newNumber > 100) Then
        Call Err.Raise(380)
    End If

    m_number = newNumber
End Property


  The followings are 2 samples that uses the Class3 and Class4.

The sample that uses Class3

The sample that uses Class4

Sub main()
    Dim obj As Class3
    Set obj = New Class3
    obj.number = 1000
    Debug.Print obj.number
End Sub
Sub main()
    Dim obj As Class4
    Set obj = New Class4
    obj.number = 1000
    Debug.Print obj.number
End Sub


  Is sample that uses Class3 able to set 1000 ?
  Then , I think that there are necessary checking the number attribute to protect the number attribute. Where do you check it?
  And , if you check at points that use the class3 , and if there are 1000 points that use the number attribute, programming cost and time is increased (you must add checking programs at all points. and you must check a mistack and leakage) .
  
  The others , when there are 2 or more datas that must set at the same time, when there are enable datas when the other data ( for instance, status) is only enable, the datas must be encapsuled. ( When not encapsuled , the datas are easily inconsistency.)

  I repeat a part, I think that there are following merits.
  1. Inconsistency datas are decreased because of checking the datas when setting 1.
    you can design thread safety too.
  2. When there are 2 or more datas that must set at the same time , you can ban that one side is set.
  3. You can program log output , when datas are set or gotten.
  4. You can program access control.
  5. Except in the VBA, you can design that datas are only set by the constructor and design getting method 2.
  6. You can change the visiblity of the setting method and getting method.
  7. The program become a little independent from specification change. Later , when the datas or classes structure are changed , the change of the classes that uses those dose not become increased because of abstraction of data structures by methods 3.
  8. The decrease at the cohesion level on the call side can be prevented by doing in the method of setting processing without the necessity that the call side considers when data is set 4.
-----
Note.
1: I think that data checking is related datas. Then, when there are datas and checking procedure in the same class , the cohesion of the class is high.
2: You can design the Immutable object.
3: The module coupling is lower.
4: For example , the java.util.Vector class. When data is added, the area is increased by the automatic operation when capacity is insufficient.
This increasing area processing is a processing that the call side class don't want to consider.


Prev Up Next  Top
Back to Top

I doesn't assume the responsibility of any damage that occurs because of the use of this page.

Microsoft ,Windows ,Visual Basic and Excel are registered trademarks of Microsoft Corporation in the United States and other countries.
Visual Basic® for Applications may represent a VBA.
Mac ,Mac OS ,Mac OS X ,AppleScript are trademarks of Apple Inc., registerd in the U.S. and other countries.
Other brands and their products are trademarks or registered trademarks of their respective holders and should be noted as such.

The author of this page and these companies do not have any relationship.