Skip to content

Commit 75beecb

Browse files
committed
Add exmplanations and examples
1 parent eafc269 commit 75beecb

File tree

1 file changed

+74
-12
lines changed

1 file changed

+74
-12
lines changed

Diff for: README.md

+74-12
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55
* Software development paradigm
66
* We should represent our system concepts using classes
77
* Classes deals with common behaviour to all of its different instances (objects):
8+
89
![Classes vs objects](resources/blueprint-vs-house.png)
910
* Objects have their own memory
1011
* Object communicate between them sending and receiving messages
1112

1213
## Visibility and inheritance
1314

14-
### `public`, `protected`, and `private`
15+
### `public`, `protected`, and `private` visibility keywords
1516
* When do you use each one?
17+
* Guilt presumption
18+
* Simplify our classes API (exposed methods) => Easier to understand, easier to be SRP compliant, avoid having to maintain public methods because others are coupled to them
1619
* Question:
1720
* Which would be the output of the `Child#visibilityTest` method?
18-
* Solution: `ChildShould`
21+
* Solution: `ChildShould`. Possible answers:
22+
```
23+
"Child#privateMethod Child#protectedMethod Child#publicMethod" // a
24+
"Parent#privateMethod Child#protectedMethod Child#publicMethod" // b
25+
"Parent#privateMethod Parent#protectedMethod Child#publicMethod" // c
26+
"Parent#privateMethod Child#protectedMethod Parent#publicMethod" // d
27+
"Parent#privateMethod Parent#protectedMethod Parent#publicMethod" // e
28+
// It doesn't compile // f
29+
```
1930

20-
### `static`
31+
### `static` keyword
2132
* What is it for?
2233
* Question:
23-
* Which would be the output?
34+
* Which would be the output for the following `getTotal` calls?
35+
* Solution: `CounterShould`. Possible answers:
2436
```java
2537
Counter counterA = new Counter();
2638
Counter counterB = new Counter();
@@ -35,21 +47,71 @@ counterB.increaseTotal();
3547

3648
counterC.increaseTotal();
3749

38-
counterA.getTotal(); // ???
39-
counterB.getTotal(); // ??
40-
counterC.getTotal(): // ?
50+
// a:
51+
counterA.getTotal(); // 0
52+
counterB.getTotal(); // 0
53+
counterC.getTotal(): // 0
54+
55+
// b:
56+
counterA.getTotal(); // 6
57+
counterB.getTotal(); // 6
58+
counterC.getTotal(): // 6
59+
60+
// c:
61+
counterA.getTotal(); // 3
62+
counterB.getTotal(); // 5
63+
counterC.getTotal(): // 6
64+
65+
// c:
66+
counterA.getTotal(); // 6
67+
counterB.getTotal(); // 3
68+
counterC.getTotal(): // 1
4169
```
42-
* Solution: `CounterShould`
4370

44-
### `final`
45-
* What does it do in methods and classes?
71+
### `final` keyword
72+
* What does it do in attributes?
73+
* Does not allow to redefine them
74+
* What does it do in methods?
75+
* Does not allow to override them
76+
* What does it do in classes?
77+
* Does not allow to inherit from them
4678
* When we should use it?
79+
* Same reasoning as with the visibility keywords: Guilt presumption.
80+
* Why: Make the next developer think twice before extending from it.
81+
* Key concept: [Composition over Inheritance](https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205).
4782

48-
### `abstract` vs `interface`
83+
### `abstract` classes vs `interface`s
4984
* What's the difference?
85+
* Interfaces:
86+
* Doesn't allow to implement method bodies. It only allow us to declare method contracts/headers. <- True until Java8
87+
* They're great because as they have fewer capabilities, they are easier to read and understand without letting us mess up adding behaviour.
88+
* A class can implement different interfaces.
89+
* Abstract classes:
90+
* Allow to implement method bodies.
91+
* A class can only extend from one abstract class.
5092
* When we should use `abstract` classes?
93+
* Opinion: Almost never. Just exceptional cases. We should have a very big reason to do so 🙂
5194
* When we should use `interfaces`?
52-
* What about multiple inheritance?
95+
* Opinion: In order to decouple from infrastructure* stuff.
96+
* *Infrastructure: behaviour related to a third party library or component (Postgres DB, AWS SDK, Slack SDK, MailChimp API…)
97+
* Usage example:
98+
```java
99+
100+
interface ProductRecommender
101+
{
102+
Recommendations findFor(ProductId productId);
103+
}
104+
105+
final class BlueknowProductRecommender implements ProductRecommender
106+
{
107+
@Override
108+
public Recommendations findFor(ProductId productId) {
109+
// Call to the Blueknow service API
110+
// Parse the JSON response into a `Recommendations` class instance
111+
return recommendations;
112+
}
113+
}
114+
```
53115

54116
Example:
55117
* Context:

0 commit comments

Comments
 (0)