π Effective Java: μμ±μ λμ μ μ ν©ν°λ¦¬ λ©μλλ₯Ό κ³ λ €νλΌ
Effective Javaμ μ μ μ‘°μμ λΈλ‘ν¬(Joshua Bloch)λ μμ±μ λμ μ μ ν©ν°λ¦¬ λ©μλλ₯Ό μ¬μ©νλ κ²μ κΆμ₯νλ©°, κ·Έ μ΄μ λ‘ μ¬λ¬ κ°μ§ μ₯μ μ μ μνκ³ μμ΅λλ€. μλμμ κ° μ₯μ κ³Ό λ¨μ μ μ½λ μμμ ν¨κ» μμΈν μ€λͺ νκ² μ΅λλ€.
μ μ ν©ν 리 λ©μλμ μ₯μ
1. μ΄λ¦μ κ°μ§ μ μλ€
μμ±μλ ν΄λμ€ μ΄λ¦μ κ°μ§λ―λ‘, μ¬λ¬ μμ±μκ° μμ κ²½μ° κ΅¬λΆμ΄ μ΄λ ΅μ΅λλ€. λ°λ©΄, μ μ ν©ν 리 λ©μλλ μλ―Έ μλ μ΄λ¦μ κ°μ§ μ μμ΄ μ½λ κ°λ μ±μ΄ ν₯μλ©λλ€.
μμ:
public class User {
private String username;
private String email;
private User(String username, String email) {
this.username = username;
this.email = email;
}
// μ μ ν©ν 리 λ©μλ
public static User fromUsername(String username) {
return new User(username, username + "@example.com");
}
public static User fromEmail(String email) {
String username = email.substring(0, email.indexOf("@"));
return new User(username, email);
}
@Override
public String toString() {
return "User{username='" + username + "', email='" + email + "'}";
}
}
μ¬μ© μ:
public class Main {
public static void main(String[] args) {
User user1 = User.fromUsername("john_doe");
User user2 = User.fromEmail("jane@example.com");
System.out.println(user1); // User{username='john_doe', email='john_doe@example.com'}
System.out.println(user2); // User{username='jane', email='jane@example.com'}
}
}
2. νΈμΆλ λλ§λ€ μΈμ€ν΄μ€λ₯Ό μλ‘ μμ±νμ§λ μμλ λλ€
μ μ ν©ν 리 λ©μλλ νμμ λ°λΌ κΈ°μ‘΄ μΈμ€ν΄μ€λ₯Ό μ¬μ¬μ©ν μ μμ΅λλ€. μ΄λ λ©λͺ¨λ¦¬ μ μ½κ³Ό μ±λ₯ ν₯μμ λμμ΄ λ©λλ€.
μμ:
public class BooleanWrapper {
private final boolean value;
private static final BooleanWrapper TRUE = new BooleanWrapper(true);
private static final BooleanWrapper FALSE = new BooleanWrapper(false);
private BooleanWrapper(boolean value) {
this.value = value;
}
public static BooleanWrapper valueOf(boolean value) {
return value ? TRUE : FALSE;
}
@Override
public String toString() {
return Boolean.toString(value);
}
}
μ¬μ© μ:
public class Main {
public static void main(String[] args) {
BooleanWrapper bw1 = BooleanWrapper.valueOf(true);
BooleanWrapper bw2 = BooleanWrapper.valueOf(true);
BooleanWrapper bw3 = BooleanWrapper.valueOf(false);
System.out.println(bw1 == bw2); // true
System.out.println(bw1 == bw3); // false
}
}
3. λ°ν νμ μ νμ νμ κ°μ²΄λ₯Ό λ°νν μ μλ λ₯λ ₯μ΄ μλ€
μ μ ν©ν 리 λ©μλλ λ°ν νμ μ νμ νμ κ°μ²΄λ₯Ό λ°νν μ μμ΄ μ μ°μ±μ΄ λμ΅λλ€.
μμ:
public interface Shape {
void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing Circle");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Drawing Square");
}
}
public class ShapeFactory {
public static Shape createShape(String type) {
switch(type.toLowerCase()) {
case "circle":
return new Circle();
case "square":
return new Square();
default:
throw new IllegalArgumentException("Unknown shape type");
}
}
}
μ¬μ© μ:
public class Main {
public static void main(String[] args) {
Shape shape1 = ShapeFactory.createShape("circle");
Shape shape2 = ShapeFactory.createShape("square");
shape1.draw(); // Drawing Circle
shape2.draw(); // Drawing Square
}
}
4. μ λ ₯ 맀κ°λ³μμ λ°λΌ λ§€λ² λ€λ₯Έ ν΄λμ€μ κ°μ²΄λ₯Ό λ°νν μ μλ€
μ λ ₯ 맀κ°λ³μμ λ°λΌ λ€μν ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ°νν μ μμ΄ μ μ°ν κ°μ²΄ μμ±μ ν μ μμ΅λλ€.
μμ:
public interface Connection {
void connect();
}
public class HttpConnection implements Connection {
@Override
public void connect() {
System.out.println("Connecting via HTTP");
}
}
public class FtpConnection implements Connection {
@Override
public void connect() {
System.out.println("Connecting via FTP");
}
}
public class ConnectionFactory {
public static Connection getConnection(String protocol) {
switch(protocol.toLowerCase()) {
case "http":
return new HttpConnection();
case "ftp":
return new FtpConnection();
default:
throw new IllegalArgumentException("Unsupported protocol");
}
}
}
μ¬μ© μ:
public class Main {
public static void main(String[] args) {
Connection conn1 = ConnectionFactory.getConnection("http");
Connection conn2 = ConnectionFactory.getConnection("ftp");
conn1.connect(); // Connecting via HTTP
conn2.connect(); // Connecting via FTP
}
}
5. μ μ ν©ν°λ¦¬ λ©μλλ₯Ό μμ±νλ μμ μλ λ°νν κ°μ²΄μ ν΄λμ€κ° μ‘΄μ¬νμ§ μμλ λλ€
μ μ ν©ν 리 λ©μλλ μΈν°νμ΄μ€λ₯Ό λ°ν νμ μΌλ‘ μ¬μ©ν μ μμ΄, λ°νν ꡬ체μ μΈ ν΄λμ€κ° λμ€μ μΆκ°λλλΌλ μ½λ μμ μμ΄ μ¬μ©ν μ μμ΅λλ€.
μμ:
public interface Logger {
void log(String message);
}
public class ConsoleLogger implements Logger {
@Override
public void log(String message) {
System.out.println("ConsoleLogger: " + message);
}
}
public class FileLogger implements Logger {
@Override
public void log(String message) {
// νμΌμ λ‘κ·Έλ₯Ό κΈ°λ‘νλ μ½λ
System.out.println("FileLogger: " + message);
}
}
public class LoggerFactory {
public static Logger getLogger(String type) {
switch(type.toLowerCase()) {
case "console":
return new ConsoleLogger();
case "file":
return new FileLogger();
default:
throw new IllegalArgumentException("Unknown logger type");
}
}
}
μ¬μ© μ:
public class Main {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger("console");
logger.log("This is a log message."); // ConsoleLogger: This is a log message.
}
}
μ μ ν©ν 리 λ©μλμ λ¨μ
1. μμμ νλ €λ©΄ publicμ΄λ protected μμ±μκ° νμνλ μ μ ν©ν 리 λ©μλλ§ μ 곡νλ©΄ νμ ν΄λμ€λ₯Ό λ§λ€ μ μλ€
μ μ ν©ν 리 λ©μλλ§ μ 곡νκ³ κ³΅κ° μμ±μκ° μμΌλ©΄, μΈλΆμμ ν΄λμ€λ₯Ό μμλ°μ νμ₯ν μ μμ΅λλ€. μ΄λ ν΄λμ€μ μ μ°μ±μ μ νν μ μμ΅λλ€.
μμ:
public class ImmutableClass {
private final int value;
private ImmutableClass(int value) {
this.value = value;
}
public static ImmutableClass of(int value) {
return new ImmutableClass(value);
}
public int getValue() {
return value;
}
}
// μμ μλ
public class ExtendedClass extends ImmutableClass {
public ExtendedClass(int value) {
super(value); // μ€λ₯: ImmutableClassμ μμ±μκ° privateμ΄λΌ μ κ·Ό λΆκ°
}
}
ν΄κ²° λ°©μ:
νμ ν΄λμ€λ₯Ό νμ©νλ €λ©΄ μ μ ν μ κ·Ό μ μ΄μλ₯Ό μ¬μ©νμ¬ μμ±μλ₯Ό μ 곡ν΄μΌ ν©λλ€. κ·Έλ¬λ μ΄λ μ μ ν©ν 리 λ©μλμ μ΄μ μ μΌλΆ ν¬κΈ°ν΄μΌ ν¨μ μλ―Έν©λλ€.
2. μ μ ν©ν 리 λ©μλλ νλ‘κ·Έλλ¨Έκ° μ°ΎκΈ° μ΄λ ΅λ€
μ μ ν©ν 리 λ©μλλ ν΄λμ€μ APIμ μ¨κ²¨μ Έ μμ μ μμΌλ©°, μ΄λ¦μ΄ μ§κ΄μ μ΄μ§ μμΌλ©΄ μ¬μ©μκ° μ΄λ₯Ό μ°ΎκΈ° μ΄λ €μΈ μ μμ΅λλ€. λ°λ©΄, μμ±μλ ν΄λμ€ μ΄λ¦κ³Ό λμΌν μ΄λ¦μ κ°μ§λ―λ‘ μ½κ² μ°Ύμ μ μμ΅λλ€.
μμ:
public class ComplexNumber {
private final double real;
private final double imaginary;
private ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
// μ μ ν©ν 리 λ©μλμ μ
public static ComplexNumber ofCartesian(double real, double imaginary) {
return new ComplexNumber(real, imaginary);
}
public static ComplexNumber ofPolar(double magnitude, double angle) {
return new ComplexNumber(magnitude * Math.cos(angle), magnitude * Math.sin(angle));
}
}
// μ¬μ©μκ° μ μ ν©ν 리 λ©μλλ₯Ό μ°ΎκΈ° μ΄λ €μ΄ κ²½μ°
public class Main {
public static void main(String[] args) {
// ComplexNumber cn = new ComplexNumber(1, 2); // μ€λ₯: μμ±μκ° private
ComplexNumber cn1 = ComplexNumber.ofCartesian(1, 2);
ComplexNumber cn2 = ComplexNumber.ofPolar(1, Math.PI / 2);
System.out.println("Cartesian: " + cn1.real + " + " + cn1.imaginary + "i");
System.out.println("Polar: " + cn2.real + " + " + cn2.imaginary + "i");
}
}
ν΄κ²° λ°©μ:
μ μ ν©ν 리 λ©μλμ μ΄λ¦μ μ§κ΄μ μΌλ‘ μ§μ νκ³ , ν΄λμ€ λ¬Έμμ μ΄λ₯Ό λͺ νν λͺ μνμ¬ νλ‘κ·Έλλ¨Έκ° μ½κ² μ°Ύμ μ μλλ‘ ν©λλ€.
κ²°λ‘
μ μ ν©ν 리 λ©μλλ μμ±μμ λΉν΄ μ¬λ¬ κ°μ§ μ₯μ μ μ 곡νμ§λ§, μΌλΆ λ¨μ λ μ‘΄μ¬ν©λλ€. νΉν μμκ³Ό API μ¬μ©μ νΈμμ± μΈ‘λ©΄μμ κ³ λ €ν΄μΌ ν μ¬νμ΄ μμ΅λλ€. μ΄λ¬ν μ₯λ¨μ μ μ μ΄ν΄νκ³ μν©μ λ§κ² μ¬μ©νλ κ²μ΄ μ€μν©λλ€. μλλ μμ κ°λ μ μ’ ν©ν κ°λ¨ν μμ μ λλ€.
μ’ ν© μμ:
// μΈν°νμ΄μ€ μ μ
public interface Animal {
void speak();
}
// ꡬ체μ μΈ ν΄λμ€λ€
public class Dog implements Animal {
@Override
public void speak() {
System.out.println("Woof!");
}
}
public class Cat implements Animal {
@Override
public void speak() {
System.out.println("Meow!");
}
}
// μ μ ν©ν 리 λ©μλλ₯Ό μ 곡νλ ν©ν 리 ν΄λμ€
public class AnimalFactory {
public static Animal createAnimal(String type) {
switch(type.toLowerCase()) {
case "dog":
return new Dog();
case "cat":
return new Cat();
default:
throw new IllegalArgumentException("Unknown animal type");
}
}
}
// μ¬μ© μ
public class Main {
public static void main(String[] args) {
Animal dog = AnimalFactory.createAnimal("dog");
Animal cat = AnimalFactory.createAnimal("cat");
dog.speak(); // Woof!
cat.speak(); // Meow!
}
}
μ΄ μμ μμλ AnimalFactory
μ μ μ ν©ν 리 λ©μλ createAnimal
μ μ¬μ©νμ¬ Animal
μΈν°νμ΄μ€μ λ€μν ꡬν체λ₯Ό μμ±ν©λλ€. μ΄λ₯Ό ν΅ν΄ μ½λμ μ μ°μ±κ³Ό κ°λ
μ±μ λμΌ μ μμ΅λλ€.
'BOOK > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
μ΄νν°λΈ μλ° : μ©μ΄ μ 리 & μ΄ν΄λΆκ° ꡬ문 ν΄μ (0) | 2024.12.29 |
---|
λκΈ