design patterns : state
This commit is contained in:
23
design-pattern/gof/src/state/after/Client.java
Normal file
23
design-pattern/gof/src/state/after/Client.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
OnlineCourse onlineCourse = new OnlineCourse();
|
||||||
|
Student kim = new Student("kim");
|
||||||
|
|
||||||
|
onlineCourse.addStudent(kim);
|
||||||
|
onlineCourse.addReview("hello", kim);
|
||||||
|
|
||||||
|
onlineCourse.changeState(new Private(onlineCourse));
|
||||||
|
onlineCourse.addReview("hello", kim);
|
||||||
|
|
||||||
|
Student lee = new Student("lee");
|
||||||
|
onlineCourse.addStudent(lee);
|
||||||
|
|
||||||
|
lee.addPrivate(onlineCourse);
|
||||||
|
onlineCourse.addStudent(lee);
|
||||||
|
|
||||||
|
System.out.println(onlineCourse);
|
||||||
|
}
|
||||||
|
}
|
||||||
24
design-pattern/gof/src/state/after/Draft.java
Normal file
24
design-pattern/gof/src/state/after/Draft.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
public class Draft implements State {
|
||||||
|
|
||||||
|
private OnlineCourse onlineCourse;
|
||||||
|
|
||||||
|
public Draft(OnlineCourse onlineCourse) {
|
||||||
|
this.onlineCourse = onlineCourse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addReview(String review, Student student) {
|
||||||
|
System.out.println("드래프트 상태에서는 리뷰를 남길 수 없습니다.");
|
||||||
|
// throw new UnsupportedOperationException("드래프트 상태에서는 리뷰를 남길 수 없습니다.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addStudent(Student student) {
|
||||||
|
this.onlineCourse.getStudents().add(student);
|
||||||
|
if (this.onlineCourse.getStudents().size() > 1) {
|
||||||
|
this.onlineCourse.changeState(new Private(this.onlineCourse));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
design-pattern/gof/src/state/after/OnlineCourse.java
Normal file
47
design-pattern/gof/src/state/after/OnlineCourse.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class OnlineCourse {
|
||||||
|
|
||||||
|
private State state = new Draft(this);
|
||||||
|
|
||||||
|
private List<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<String> reviews = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addStudent(Student student) {
|
||||||
|
this.state.addStudent(student);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addReview(String review, Student student) {
|
||||||
|
this.state.addReview(review, student);
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Student> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getReviews() {
|
||||||
|
return reviews;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeState(State state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "OnlineCourse{" +
|
||||||
|
"state=" + state +
|
||||||
|
", students=" + students +
|
||||||
|
", reviews=" + reviews +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
30
design-pattern/gof/src/state/after/Private.java
Normal file
30
design-pattern/gof/src/state/after/Private.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
public class Private implements State {
|
||||||
|
|
||||||
|
private OnlineCourse onlineCourse;
|
||||||
|
|
||||||
|
public Private(OnlineCourse onlineCourse) {
|
||||||
|
this.onlineCourse = onlineCourse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addReview(String review, Student student) {
|
||||||
|
if (this.onlineCourse.getStudents().contains(student)) {
|
||||||
|
this.onlineCourse.getReviews().add(review);
|
||||||
|
} else {
|
||||||
|
System.out.println("프라이빗 코스를 수강하는 학생만 리뷰를 남길 수 있습니다.");
|
||||||
|
// throw new UnsupportedOperationException("프라이빗 코스를 수강하는 학생만 리뷰를 남길 수 있습니다.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addStudent(Student student) {
|
||||||
|
if (student.isAvailable(this.onlineCourse)) {
|
||||||
|
this.onlineCourse.getStudents().add(student);
|
||||||
|
} else {
|
||||||
|
System.out.println("프라이빗 코스를 수강할 수 없습니다.");
|
||||||
|
// throw new UnsupportedOperationException("프라이빗 코스를 수강할 수 없습니다.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
design-pattern/gof/src/state/after/Published.java
Normal file
20
design-pattern/gof/src/state/after/Published.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
public class Published implements State {
|
||||||
|
|
||||||
|
private OnlineCourse onlineCourse;
|
||||||
|
|
||||||
|
public Published(OnlineCourse onlineCourse) {
|
||||||
|
this.onlineCourse = onlineCourse;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addReview(String review, Student student) {
|
||||||
|
this.onlineCourse.getReviews().add(review);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addStudent(Student student) {
|
||||||
|
this.onlineCourse.getStudents().add(student);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
design-pattern/gof/src/state/after/State.java
Normal file
8
design-pattern/gof/src/state/after/State.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
public interface State {
|
||||||
|
|
||||||
|
void addReview(String review, Student student);
|
||||||
|
|
||||||
|
void addStudent(Student student);
|
||||||
|
}
|
||||||
30
design-pattern/gof/src/state/after/Student.java
Normal file
30
design-pattern/gof/src/state/after/Student.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package state.after;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Student(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<OnlineCourse> onlineCourses = new HashSet<>();
|
||||||
|
|
||||||
|
public boolean isAvailable(OnlineCourse onlineCourse) {
|
||||||
|
return this.onlineCourses.contains(onlineCourse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPrivate(OnlineCourse onlineCourse) {
|
||||||
|
this.onlineCourses.add(onlineCourse);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
23
design-pattern/gof/src/state/before/Client.java
Normal file
23
design-pattern/gof/src/state/before/Client.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package state.before;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student student = new Student("whiteship");
|
||||||
|
OnlineCourse onlineCourse = new OnlineCourse();
|
||||||
|
|
||||||
|
Student keesun = new Student("keesun");
|
||||||
|
keesun.addPrivateCourse(onlineCourse);
|
||||||
|
|
||||||
|
onlineCourse.addStudent(student);
|
||||||
|
onlineCourse.changeState(OnlineCourse.State.PRIVATE);
|
||||||
|
|
||||||
|
onlineCourse.addStudent(keesun);
|
||||||
|
|
||||||
|
onlineCourse.addReview("hello", student);
|
||||||
|
|
||||||
|
System.out.println(onlineCourse.getState());
|
||||||
|
System.out.println(onlineCourse.getStudents());
|
||||||
|
System.out.println(onlineCourse.getReviews());
|
||||||
|
}
|
||||||
|
}
|
||||||
63
design-pattern/gof/src/state/before/OnlineCourse.java
Normal file
63
design-pattern/gof/src/state/before/OnlineCourse.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package state.before;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class OnlineCourse {
|
||||||
|
|
||||||
|
public enum State {
|
||||||
|
DRAFT, PUBLISHED, PRIVATE
|
||||||
|
}
|
||||||
|
|
||||||
|
private State state = State.DRAFT;
|
||||||
|
|
||||||
|
private List<String> reviews = new ArrayList<>();
|
||||||
|
|
||||||
|
private List<Student> students = new ArrayList<>();
|
||||||
|
|
||||||
|
public void addReview(String review, Student student) {
|
||||||
|
if (this.state == State.PUBLISHED) {
|
||||||
|
this.reviews.add(review);
|
||||||
|
} else if (this.state == State.PRIVATE && this.students.contains(student)) {
|
||||||
|
this.reviews.add(review);
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedOperationException("리뷰를 작성할 수 없습니다.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addStudent(Student student) {
|
||||||
|
if (this.state == State.DRAFT || this.state == State.PUBLISHED) {
|
||||||
|
this.students.add(student);
|
||||||
|
} else if (this.state == State.PRIVATE && availableTo(student)) {
|
||||||
|
this.students.add(student);
|
||||||
|
} else {
|
||||||
|
throw new UnsupportedOperationException("학생을 해당 수업에 추가할 수 없습니다.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.students.size() > 1) {
|
||||||
|
this.state = State.PRIVATE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeState(State newState) {
|
||||||
|
this.state = newState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getReviews() {
|
||||||
|
return reviews;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Student> getStudents() {
|
||||||
|
return students;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean availableTo(Student student) {
|
||||||
|
return student.isEnabledForPrivateClass(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
30
design-pattern/gof/src/state/before/Student.java
Normal file
30
design-pattern/gof/src/state/before/Student.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package state.before;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Student(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<OnlineCourse> privateCourses = new ArrayList<>();
|
||||||
|
|
||||||
|
public boolean isEnabledForPrivateClass(OnlineCourse onlineCourse) {
|
||||||
|
return privateCourses.contains(onlineCourse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addPrivateCourse(OnlineCourse onlineCourse) {
|
||||||
|
this.privateCourses.add(onlineCourse);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user