☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
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 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
S
R
Q
Java Effective Constructors
Static factory, a static instance Telescoping, JavaBeans, Builder static A INSTANCE = new A() new A(240); new A(240, 100)Static
Static Factory
p5 With static factory method you don't have to create an object every time.
/**
* Static factory methods return an instance of a class.
*
* One advantage is that, unlike constructors, they have names.
* The resulting code is easier to read.
*
* Second, you don't need to create an object every time.
*/
package com.minte9.effective.constructors;
public class StaticFactory {
public static void main(String[] args) {
MyClass.getInstance();
// Class constructor
MyClass.getInstance();
// nothing
}
}
class MyClass {
private static final MyClass INSTANCE = new MyClass();
public static MyClass getInstance() { // Look Here
return INSTANCE;
}
private MyClass() {
System.out.println("Class constructor");
}
}
Telescoping
p10 Programmers use telescoping constructor when faced with many parameters.
/**
* Telescoping constructors works, but ...
* it is hard to write and hard to read.
*/
package com.minte9.effective.constructors;
public class Telescoping {
public static void main(String[] args) {
new Nutrition(240);
new Nutrition(240, 100);
new Nutrition(240, 100, 10);
/*
Constructor (size, calories, fat) 240 0 0
Constructor (size, calories, fat) 240 100 0
Constructor (size, calories, fat) 240 100 10
*/
}
}
class Nutrition {
public Nutrition(int size) { // one param always required
this(size, 0);
}
public Nutrition(int size, int calories) {
this(size, calories, 0);
}
public Nutrition(int size, int calories, int fat) {
System.out.println(
"Constructor (size, calories, fat) "
+ size + " "
+ calories + " "
+ fat
);
}
}
JavaBeans
p12 A second alternative is to use setter methods (JavaBeans pattern).
/**
* A JavaBeans constructor object ...
* may be in an inconsistent state
*/
package com.minte9.effective.constructors;
public class Javabeans {
public static void main(String[] args) {
A a = new A();
a.setSize(240);
// setSize: 240
a.setCalories(100);
// setCalories: 100
a.setFat(11);
// setFat: 11
}
}
class A {
private int size = -1; // required, no default
private int calories = 0;
private int fat = 0;
public void setSize(int s) {
size = s;
System.out.println("setSize: " + size);
}
public void setCalories(int s) {
calories = s;
System.out.println("setCalories: " + calories);
}
public void setFat(int s) {
fat = s;
System.out.println("setFat: " + fat);
}
}
Builder
p14 A Builder combines the safety and readability of both Telescoping and JavaBeans.
/**
* Consider a builder when faced with many constructor parameters
*/
package com.minte9.effective.constructors;
public class BuilderApp {
public static void main(String[] args) {
N n = new N.Builder(240)
.calories(100)
.fat(11)
.build();
System.out.println(n.getFat());
// 11
}
}
class N {
private final int size;
private final int calories;
private final int fat;
public static class Builder {
private int size;
private int calories = 0;
private int fat = 0;
public Builder(int s) {
size = s;
}
public Builder calories(int c) {
calories = c;
return this;
}
public Builder fat(int f) {
fat = f;
return this;
}
public N build() {
return new N(this);
}
}
private N(Builder builder) {
size = builder.size;
calories = builder.calories;
fat = builder.fat;
}
public int getSize() {
return size;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
}
➥ Questions
Last update: 71 days ago