- 📚 I'm currently learning OOP, TDD, DDD
lenkim / everyone-is-effective-java-study Goto Github PK
View Code? Open in Web Editor NEW다우기술 연구소 스터디 저장소
다우기술 연구소 스터디 저장소
사용중인 java.util.date 를 불변으로 만들 수는 없을까?
http://www.informit.com/articles/article.aspx?p=31551&seqNum=2
출처- http://egloos.zum.com/gyumee/v/3200829
자바는 클래스를 하나만 상속해서 확장할 수 있다.
class GundamMk2 extends Gundam {
...
}
클래스를 둘 이상 상속 받을려고 하면 컴파일이 안 된다.
class SpaceGundamV extends Gundam, Valkyrie {
...
}
자바가 다중상속을 지원하지 않는 이유 중에 하나는 다이아몬드 문제
라고 한다.
http://en.wikipedia.org/wiki/Diamond_problem
하지만, 사실 이 문제는 다중 상속을 지원하지 않은 이유로는 설득력이 낮다. 왜냐하면, 다중 상속을 지원하는 다양한 언어는 이 문제를 쉽게 풀어내기 때문이다.
객체지향 프로그래밍의 상징처럼 여겨지는 상속을 가능하면 쓰지 말고 객체 구성을 활용하자
이 글의 저자는 자바는 다중 상속을 지원한다
라고 말하고 있다.
자바는 부모 객체가 특별한 조건에 부합될 때에만 다중 상속을 할 수 있도록 언어 차원에서 규제를 걸어 놨다. 더 정확히 말하자면, 자바에서 다중 상속을 하려면 부모 객체가 추상 객체여야 한다. 구상 객체는 다중 상속을 받지 못한다. 그것도 그냥 추상 객체면 되는 게 아니라 순수한 추상 객체여야 한다. 순수 추상 객체라는 말은 어떤 로직도 가지고 있지 말아야 한다는 의미로, 자바에서는 이런 추상 객체를 부르는 용어가 따로 있다. 바로 인터페이스다.
자바에서 쓰이는 extends 는 상속이라고 표현하지만 사실 확장
이라는 단어가 더욱 어울린다.
왜냐하면, 상속이라는 의미는 is-a
는 부모 객체 A를 자식 객체 B가 상속했을 때 "B는 A다" 라고 말할 수 있다. 그러나 대부분의 자바에서 상속이 쓰이는 경우 has-a
의 개념을 따라간다.
조류라는 객체를 상속받아 닭을 구현했다. 그럼 닭는 조류이어야 한다. 그렇다면 닭은 조류의 역할을 해야 하지만, 자바에서는 닭이라는 객체가 변경될 수 있기 때문에 has-a 개념을 따라간다.
모 출판사의 편집자도 읽었다는 명저 Effective C++ (3판)의 32번과 38번 항목은 이렇다.
항목 32: public 상속 모형은 반드시 "is-a(...는 ...의 일종이다)"를 따르도록 만들자
항목 38: "has-a(...는 ...를 가짐)" 혹은 "is-implemented-in-terms-of(...는 ...를 써서 구현됨)"를 모형화할 때는 객체 합성을 사용하자
저자의 결론은 다음과 같다.
내 결론은 이렇다. 우리가 '상속'이란 말을 is-a로 사용한다면, 자바는 분명히 다중 상속을 지원하는 언어다. 반대로, has-a로 변질될 위험성이 있는 의미로 사용한다면, 자바는 다중 상속을 지원하지 않는 언어다. C++에서는 이 부분을 개발자가 실천해야할 규범으로 열어 놓았다면 자바는 언어차원에서 조금 더 강제할 뿐이다. 자바의 다중 상속 지원 방식이 최선인지는 모르겠다. 하지만 분명히 좋은 방법 중 하나라고 생각한다.
언제 Super를 쓰고, Extend 를 쓰지? 책에서 말하는 생산자/소비자 의 구분은 어떻게 되는걸까?
이펙티브자바 아이템에서 나오는 super/extend에 대해서 아무도 이해못함...
List<<T> extend Class & operation>
와 같은 표현식을 보고 경계를 결정해주는 클래스를 작성하다가 이야기가 나옴.
@qualifier
의 역할은 무엇인가?
Rombok
부터 간략히 설명하면, 다음과 같다.
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
이런 코드를
@Data
public class User {
private String name;
private int age;
}
한방에 해결해 줄 수 있는 것이 Rombok
인데, Builder로 마찬가지다.
public class User {
private String name;
private int age;
public static UserBuilder builder() {
return new UserBuilder();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//Builder Class
public class UserBuilder {
private String name;
private int age;
public User build() {
User user = new User();
user.setName(this.name);
user.setAge(this.age);
return user;
}
public UserBuilder name(String name) {
this.name = name;
return this;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
}
이런 코드를
@Data
@Builder
public class User {
private String name;
private int age;
}
이렇게 한 번에 해결할 수 있다. 한 번에 해결할 수 있는 만큼 편리하지만, @builder 에서 이슈는 초기값을 어떻게 주는가? 이다.
Rombok의 버전 1.16.16버전 이상을 사용하면 @Builder.Default 라는 Annotation이 추가 되었다.
@Data
@Builder
public class User {
private String name;
@Builder.Default private int age = 19;
}
결과물로, User(name=홍길동, age=19)
이에 대한 자세한 설명은 [docs]: https://projectlombok.org/features/Builder "롬복공식문서" 를 참조하면 된다.
참조 사이트 - https://tomining.tistory.com/180
아래 코드를 보면 더욱 그렇다.
public interface Flyable {
String interfaceConstant = "Test";
default void interfaceTest(){
int a = 10;
System.out.println(a);
};
static void staticTest(){
int a = 15;
System.out.println(a);
}
// public abstract void test3();
}
public abstract class Fly {
final String abstractConstant = "Test";
final void abstractFinalTest(){
System.out.println("abstractFinalTest");
};
public void abstractPublicTest(){
System.out.println("abstractPublicTest");
};
// abstract void abstractTest();
}
public class JpaMain extends Fly implements Flyable {
public void temp(){
//Abstract
abstractFinalTest();
abstractPublicTest();
System.out.println(abstractConstant);
//Interface
interfaceTest();
Flyable.staticTest();
System.out.println(interfaceConstant);
}
//abstractFinalTest
//abstractPublicTest
//Test
//10
//15
//Test
}
차이점은 주로 데이타 멤버와 메소드들에 대한 접근성.
추상클래스는 non-static 과 non-final 필드 및 public, private, protected 메소드를 사용 할 수 있다.
인터페이스는 public, static, final 을 상속할 수 있고 모든 상속 가능한 메소드는 public 이다.
모 출판사의 편집자도 읽었다는 명저 Effective C++ (3판)의 32번과 38번 항목은 이렇다.
항목 32: public 상속 모형은 반드시 "is-a(...는 ...의 일종이다)"를 따르도록 만들자
항목 38: "has-a(...는 ...를 가짐)" 혹은 "is-implemented-in-terms-of(...는 ...를 써서 구현됨)"를 모형화할 때는 객체 합성을 사용하자
추상클래스
인터페이스
[참고자료]
PR 회의에서 막연히 지금까지 지연평가가 좋은 도구라고 생각하고 살아왔는데, 이로 인한 이슈가 발생했음.
Conenction Pool 에서 한번에 너무 많은 커넥션을 요청함으로써 overdue가 일어서 문제가 발생함.
https://ko.wikipedia.org/wiki/%EB%8A%90%EA%B8%8B%ED%95%9C_%EA%B3%84%EC%82%B0%EB%B2%95
lambda를 직렬화한다. 라는 것은 무슨 의미인가? 또 직렬화하지 말아야 하는 이유는 무엇인가?
대안으로, 정적 중첩 클래스를 만들어 활용하라는데 왜?
https://stackoverflow.com/questions/22807912/how-to-serialize-a-lambda
https://dzone.com/articles/how-and-why-to-serialialize-lambdas
https://woowabros.github.io/experience/2017/10/17/java-serialize.html
명로성(clarity) : '컴포넌트는 사용자를 놀라게 하는 동작을 해서는 절대 안된다.'
단순성(simplicity) : '컴포넌트는 가능한 작되'
재사용성 : '코드는 복사되는 게 아니라 재사용되어야 한다.'
최소 의존성 : '컴포넌트 사이의 의존성은 최소로 유지해야 한다.'
[참고 자료]
https://programming.guide/java/boolean-vs-boolean.html
A Boolean is a class, or a reference type, defined in the standard library. It stores a reference to an object containing a value (a "box").
A boolean on the other hand, is a primitive type and part of the language itself. It stores an actual value.
We say that Boolean is the wrapper type for boolean, and objects of type Boolean are boxed values.
Boolean은 클래스이며, reference type이다. 이는 value를 포함하고 있는 object로서 저장됩니다.
반면에, boolean 은 primitive type으로서 언어의 한 부분입니다. 이건 실제 Value로서 저장됩니다.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.