#10 effective java: item 1
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.example.effectivejava;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EffectiveJavaApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EffectiveJavaApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
public enum Difficulty {
|
||||
|
||||
EASY, NORMAL, HARD, HELL
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
public interface HelloService {
|
||||
|
||||
String hello();
|
||||
|
||||
static String hi() {
|
||||
prepareMessage();
|
||||
return "hi";
|
||||
}
|
||||
|
||||
static private void prepareMessage() {
|
||||
}
|
||||
|
||||
static String hi1() {
|
||||
prepareMessage();
|
||||
return "hi";
|
||||
}
|
||||
|
||||
static String hi2() {
|
||||
prepareMessage();
|
||||
return "hi";
|
||||
}
|
||||
|
||||
default String bye() {
|
||||
return "bye";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class HelloServiceFactory {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServiceLoader<HelloService> loader = ServiceLoader.load(HelloService.class);
|
||||
Optional<HelloService> helloServiceOptional = loader.findFirst();
|
||||
helloServiceOptional.ifPresent(h -> {
|
||||
System.out.println(h.hello());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class Order {
|
||||
|
||||
private boolean prime;
|
||||
|
||||
private boolean urgent;
|
||||
|
||||
private Product product;
|
||||
|
||||
private OrderStatus orderStatus;
|
||||
|
||||
public static Order primeOrder(Product product) {
|
||||
Order order = new Order();
|
||||
order.prime = true;
|
||||
order.product = product;
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
public static Order urgentOrder(Product product) {
|
||||
Order order = new Order();
|
||||
order.urgent = true;
|
||||
order.product = product;
|
||||
return order;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Order order = new Order();
|
||||
if (order.orderStatus == OrderStatus.DELIVERED) {
|
||||
System.out.println("delivered");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
public enum OrderStatus {
|
||||
|
||||
PREPARING(0), SHIPPED(1), DELIVERING(2), DELIVERED(3);
|
||||
|
||||
private int number;
|
||||
|
||||
OrderStatus(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class Product {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Settings settings1 = Settings.getInstance();
|
||||
Settings settings2 = Settings.getInstance();
|
||||
|
||||
System.out.println(settings1);
|
||||
System.out.println(settings2);
|
||||
|
||||
Boolean.valueOf(false);
|
||||
EnumSet.allOf(Difficulty.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.effectivejava.chapter01.item01;
|
||||
|
||||
/**
|
||||
* 이 클래스의 인스턴스는 #getInstance()를 통해 사용한다.
|
||||
* @see #getInstance()
|
||||
*/
|
||||
public class Settings {
|
||||
|
||||
private boolean useAutoSteering;
|
||||
|
||||
private boolean useABS;
|
||||
|
||||
private Difficulty difficulty;
|
||||
|
||||
private Settings() {}
|
||||
|
||||
private static final Settings SETTINGS = new Settings();
|
||||
|
||||
public static Settings getInstance() {
|
||||
return SETTINGS;
|
||||
}
|
||||
}
|
||||
1
effective-java/src/main/resources/application.properties
Normal file
1
effective-java/src/main/resources/application.properties
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.effectivejava;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class EffectiveJavaApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user