Skip to content

Commit 01eaf2c

Browse files
committed
[Update] Refactor, add LoginService interface
1 parent d2e2ac1 commit 01eaf2c

File tree

5 files changed

+45
-7
lines changed

5 files changed

+45
-7
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class EmailLoginService implements LoginService {
2+
3+
private final String email;
4+
private final String passwd;
5+
6+
public EmailLoginService(String email, String passwd) {
7+
this.email = email;
8+
this.passwd = passwd;
9+
}
10+
11+
@Override
12+
public void login() {
13+
System.out.println("LoginService by email:" + email);
14+
}
15+
}

src/main/java/Example.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
public class Example {
22
public static void main(String[] args) {
3-
LoginView loginView = new LoginView();
4-
loginView.login("test@gmailcom", "passwd");
53

6-
loginView.loginByPhoneNumber("+886910111222", "passwd");
4+
LoginService emailLoginService = new EmailLoginService("[email protected]", "passwd");
5+
LoginView loginView = new LoginView(emailLoginService);
6+
loginView.startLogin();
7+
8+
LoginService phoneNumberLoginService = new PhoneNumberLoginService("+886910111222", "passwd");
9+
loginView = new LoginView(phoneNumberLoginService);
10+
loginView.startLogin();
711
}
812

913
}

src/main/java/LoginService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface LoginService {
2+
void login();
3+
}

src/main/java/LoginView.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
public class LoginView {
2-
public void login(String email, String passwd) {
3-
System.out.println("Login :" + email);
2+
private LoginService loginService;
3+
4+
public LoginView(LoginService emailLoginService) {
5+
this.loginService = emailLoginService;
46
}
57

6-
public void loginByPhoneNumber(String phoneNumber, String passwd) {
7-
System.out.println("Login by phone: " + phoneNumber);
8+
public void startLogin() {
9+
loginService.login();
810
}
911
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class PhoneNumberLoginService implements LoginService {
2+
private String phonenumber;
3+
private String passwd;
4+
5+
public PhoneNumberLoginService(String phoneNumber, String passwd) {
6+
this.phonenumber = phoneNumber;
7+
this.passwd = passwd;
8+
}
9+
10+
@Override
11+
public void login() {
12+
System.out.println("Login by phone number:" + this.phonenumber);
13+
}
14+
}

0 commit comments

Comments
 (0)