[Java 기초문법] by Professional Java Developer Career Starter: Java Foundations @ Udemy
Lambdas
public static final Employee createEmployee(String employeeText){
Matcher peopleMat = Employee.PEOPLE_PAT.matcher(employeeText);
if (peopleMat.find()) {
return switch (peopleMat.group("role")){
case "Programmer" -> new Programmer(employeeText);
case "Manager" -> new Manager(employeeText);
case "Analyst" -> new Analyst(employeeText);
case "CEO" -> new CEO(employeeText);
default -> new DummyEmployee();
};
} else{
return new DummyEmployee();
}
}
여기서 DummyEmployee라는 메소드는 어떤면에서는 redundant하다
이것을 최대한 간결하게 하는 방법 중에 하나는 anonymous를 사용해서 다른 공간을 할당하지 않고 여기서 끝내버리는 것이다.
또 하나의 방법은 lambdas를 이용하는 것이다.
consice한 function기능
A method is a method that takes no input, is expressed as () and it returns zero
using arrow
여기서 발생하는 에러를 보면
So here, Java will try to take this lambda expression and it will try to inspect what is the data type that is
expected to returned here.
It will loo and see that his is a switch expression, a part of a method, whose return type is employee.
therefore this function should return something that looks like an employee.
In this case, Java is unable to make that determination with this data type.
no input and return one something ? => method in interface.
따라서 메소드로 정의된 Employee의 타입을 IEmployee로 바꾼다
여기서 일어나는 로직은 this functions appears and Java sees this expression, which is decribing a method that takes no input and returns an integer, it sees that the return type as IEmployee and goes look at the code and find same expression => create an object that implements the IEmployee => return what is declared.
Delegate method
만약 CEO가 CEO이면서 동시에 Pilot이라면 어떨까?
파일럿을 모델링한다.
package com.neutrio.employees;
public class Pilot {
private int hoursFlown = 0;
private boolean ifr = false;
public Pilot(int hoursFlown, boolean ifr) {
this.hoursFlown = hoursFlown;
this.ifr = ifr;
}
public void fly(){
System.out.println("Prepare for take off");
}
public int getHoursFlown() {
return hoursFlown;
}
public void setHoursFlown(int hoursFlown) {
this.hoursFlown = hoursFlown;
}
public boolean isIfr() {
return ifr;
}
public void setIfr(boolean ifr) {
this.ifr = ifr;
}
}
문제는 자바에서는 복수 상속이 불가능하다는 점이다.
따라서 CEO 클래스가 Pilot 을 상속하지 못한다.
Pseudo fake out하게 이것을 해결해보자.
that class can be composed as such multiple classes
Just creating a new instance of a pilot as a field => doesn't give us any of that.
However, by doing so, so called delegate methods on the CEO and what those would be are methods that mirror the public methods of the pilot class, such taht CEO HAS those same method.
Delegate method를 만든다
Delegate method가 만들어졌다.
with this, it being able to expand the capabilities of my class, somewhat similarly, extending another class inheritence.
또 다른 방법으로는 Flyer라는 새로운 interface를 만들어서 implements 하는 방식
'Programming > Java, Spring' 카테고리의 다른 글
[Java 기초문법] Streams API | Streams, stream의 다양한 메소드, sum, sort, filter (0) | 2022.10.20 |
---|---|
[Java 기초문법] collections, list, linked list, looping with iterators (0) | 2022.10.20 |
[Java 기초문법] 자바 records (0) | 2022.10.20 |
[Java 기초문법] 자바 객체지향 | OOP, Interfaces, Super class, Abstract class, Factory Methods, Nested class, inner class (0) | 2022.10.20 |
[Java 기초문법] 객체지향 | Gradle, Enums, Enum의 여러 기능들 (0) | 2022.10.19 |