Let's not look at information hiding which is an important design principle that helps in building badly 2
design software as mentioned earlier we'll be using access modifiers to achieve information hiding Joshua 3
Bloch mentions in one of the items that we must strive to write good programs rather than fast ones 4
. 5
And if a good program is not fast enough then its underlying design principles will allow the program 6
to be optimized later. 7
So it is important to learn and use good design principles. 8
So let's go ahead and learn about information Heidi. 9
We'll be discussing a couple of important effective July items to understand this topic Harmatz understand 10
what encapsulation is encapsulation is basically a language facility that allows bundling of data and 11
methods that operate on that data in July we already know how to do this. 12
It is basically done using it. 13
Class class has methods. 14
Andi does nothing but Grilli of on here is an example. 15
It is our familiar student class which bundles Littlefield's and run Metford operating on one of the 16
read affiance only addition here is that public access modifier has been added to the class variables 17
and the method encapsulation is one of the core features of object oriented programming. 18
However it alone does not lead to good software design. 19
To follow additional design principles to make that happen and we will look at a couple of them let's 20
reconsider our student class example here. 21
All variables are declared as public. 22
That means that part of the API on any kind called can access them directly that is your internal Dauphin's 23
are exposed and it is a bad design as it leads to something called tight coupling that is client code 24
is tightly coupled to your program let's consider the Ginnifer in this example and let's try to understand 25
what issues we can how do you get by coupling 26
one issue is we cannot enforce invariants. 27
That is we cannot define a fixed value range of values for the variable for example for the gender field 28
there is no way we can restrict gender to only these three values male female and transgender since 29
grindcore can directly accident or field they can assign it any value they wish that is any value out 30
of this range and that could bring the system into an incorrect state. 31
In fact if you recall our Dymov student class example V-1 Axelson gender free directly from the student 32
best class in order to set a value. 33
Did everyone do your demo very will change this based on the information hiding principle that we are 34
going to learn in this lesson and that if you could with that they cannot change the internal data representation 35
without effort being blind scored. 36
In our example gender is of type String undef Evista converted to and then the client code would also 37
have to change the date or type from strength and uming it uses the gender directly. 38
That's not a good thing. 39
Ask clansmen not expect the API to change in this way. 40
That is once that all that gender is off by a string. 41
And this public. 42
Which means that it is part of the API. 43
Then they would expect it to remain as string for whatever so these are a couple of issues. 44
Coupling And this brings us to an advice from effective Java. 45
Item 14 which is in public grassers use accessor methods are not public views and this is a most fundamental 46
rule of information hiding. 47
Let's see what accessor methods are and how we can use them to improve or worse during class and this 48
illustration only changes to a general election for wonderful access modifier is change from public 49
to private. 50
And then we introduce two methods one public sector matter and one public get on better. 51
So that matter is used to initialize large underfeed the get method simply reduce the value of the Jennifer 52
not that a sitter is also referred to as a mutator as we are basically mutating the state. 53
Again it is also referred to as an accessor which is what the effective delightsome that we just saw 54
refers to so this is how you should initialize instance fees. 55
So it does change the client code can no longer access the gender field directly in item 14 Joshua Bloch 56
also mentions about a couple of core classes in the standard July API that violated this principle. 57
And unfortunately that led to serious performance issues which are still there. 58
Probably they could not fix those issues because the code is already being widely used. 59
That is if they change their design it would break the code in scored. 60
In fact ideas like Eclipse provide you the option to automatically insert getters and setters if you 61
declare private instance feels that we actually show this to you. 62
In my Eclipse ID OK this is my Eclipse and this is a class that I how. 63
Let me just add a private instance when I read it is a type an ID as you can see Eclipse's highlighting 64
this idea. 65
And when I holler over it. 66
It says I feel is not used and it gives me three quick fixes on one of them is create. 67
Get an answer for ID. 68
Let's talk about it about the other two options. 69
Click on Doc and it gives me an option via like an ad these are get done on centers. 70
So let me just say asfast matter. 71
And here it is. 72
So you the getters and setters have been added automatically. 73
So let me just remove them. 74
And if the idea is already declared and if it is being used from one of the metrics in the class then 75
it will not be highlighted an item will not be highlighted in which case we can go here in the source 76
in the menu and click on this Jondrette getters and setters over here it shows Id just click on that 77
. 78
And if you click OK it once again automatically creates get transmitter's So it just shows the importance 79
of getters and setters. 80
So even eclipse provides an option to automatically gendered them 81
not see how the simple design principle addresses the two issues that we saw earlier a tight coupling 82
one was about enforcing invariants while the second was about changing the internal data representation 83
. 84
First let's see how the invariant issue is addressed. 85
And here is a modified code if you recall we wanted gender to be restricted to only three values male 86
female and transgender to enforce this. 87
The if statement in the setter method is checking of the input gender that is passed is one of the three 88
valid gender values. 89
If that's not the case then an exception is thrown indicating that an invalid gender value has been 90
passed. 91
Don't worry about the exceptionally good you will understand it when we get to exceptions. 92
So as you can see not allowing could directly access their underfilled is permitting us to enforce an 93
invariant next issue was related to changing in dinner the presentation of one or more fields without 94
affecting clangor. 95
For this we mentioned about changing gender from string to and let's assume that this change is necessary 96
to improve performance. 97
But as you can see Klein is already using certain ungraded methods and is expecting gender to remain 98
Astron. 99
So the TV make should not affect the signatures of these two methods that Rick Klein could will be unaffected 100
. 101
Since we are not following a good design principle this can be they can get off very easily. 102
And here is a modified called a new private and free agent that has been introduced all underfilled 103
remains as it's its subject matter. 104
We'll still use string as in book but but in the subject our methods body beside the gender feel based 105
on the input past have the input in the past as mail then I generally said to one if gender is female 106
then gender is said to do and of gender passes transgender then I did the research to three but if gender 107
is something other than these three then I gender would remain zero which is its default value and the 108
method would to an exception as before gender field is also being set as it can be used and get gender 109
mattered so with this change the client code is totally unaffected and they don't even know about this 110
implementation change. 111
So this design has given us the flexibility to change that implementation without affecting the class 112
so as you can see by hiding the fields using a private modifier and by using public getters and setters 113
we could address both the issues of tight coupling. 114
The code is now set to be loosely coupled or decoupled as the client code now only communicates through 115
the API and is oblivious to the implementation details. 116
So there is no more meddling with that data if it is directly so with meaningful package structure combined 117
with good information hiding principles we can double up loosely coupled components if how loosely couple 118
designs we can develop best use and optimize software components in isolation. 119
We just saw how student class could be optimized by converting Gandolfi from string to another advantage 120
of having loosley couple competences that they can be utilized in marketable projects and they also 121
decrease the risk in building large systems that is a system for which a particular component is Dola 122
might fail. 123
But the component itself might be useful in some other project. 124
So it is important to package your classes in a meaningful way. 125
Unadopt good designs that can lead to doping loosely coupled components this and this lesson here in 126
the next lesson we will review another effective delightsome related information hiding it but with 127
a few more suggestions on creating loosely coupled systems. 128
Thank you.