Java
/
Effective
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
R
Q
INHERITANCE
p86 Used inappropriately, inheritance leads to fragile software.
/**
* Inheritance
*
* Superclass overriding methods could lead to undesired results.
*/
package com.minte9.effective.composition;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
public class Inheritance {
public static void main(String[] args) {
HS<String> hs = new HS<>();
hs.add("A");
//Call to HS.add()
hs.addAll(
Arrays.asList(new String[] {"B", "C", "D"})
);
// Call to HS.addAll()
// Call to HS.add() +1
// Call to HS.add() +1
// Call to HS.add() +1
assertEquals(4, hs.getCount());
// fails, expected:<4> but was:<7>
}
}
class HS<E> extends HashSet<E> {
private int count = 0;
@Override public boolean add(E e) {
count += 1;
System.out.println("Call to HS.add()");
return super.add(e); // Look Here
}
@Override public boolean addAll(Collection<? extends E> c) {
count += c.size();
System.out.println("Call to HS.addAll()");
return super.addAll(c); // incorrect: call to HS.add() - Look Here
}
public int getCount() {
return count;
}
}
COMPOSITION
p88 A class becames a component to the new class.
/**
* Composition
*
* Give your new class a private field that ...
* references an instance of the existing class.
*
* The existing class becames a component to the new class.
*/
package com.minte9.effective.composition;
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Composition {
public static void main(String[] args) {
S<String> s1 = new S<>(new HashSet<>());
S<String> s2 = new S<>(new TreeSet<>()); // Look Here
s1.add("A"); // Call to S.add()
s2.add("x"); // Call to S.add()
s1.addAll(List.of("B", "C", "D")); // Call to S.addAll()
s2.addAll(List.of("Y", "Z", "W")); // Call to S.addAll()
assertEquals(4, s1.getCount()); // pass
assertEquals(4, s2.getCount()); // pass
}
}
class S<E> { // no inheritance
private int count = 0;
private final Set<E> set; // Look Here
public S(Set<E> s) {
set = s;
}
public boolean add(E e) {
count += 1;
System.out.println("Call to S.add()");
return set.add(e); // Look Here
}
public boolean addAll(Collection<? extends E> c) {
count += c.size();
System.out.println("Call to S.addAll()");
return set.addAll(c); // correct: call to super.add() - Look Here
}
public int getCount() {
return count;
}
}
➥ Questions