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