From 15bcfc4b025ef070ebeda1137a2bbcf86bf54dae Mon Sep 17 00:00:00 2001 From: haerong22 Date: Mon, 20 Dec 2021 23:57:14 +0900 Subject: [PATCH] design patterns : state --- .../gof/src/state/after/Client.java | 23 +++++++ design-pattern/gof/src/state/after/Draft.java | 24 +++++++ .../gof/src/state/after/OnlineCourse.java | 47 ++++++++++++++ .../gof/src/state/after/Private.java | 30 +++++++++ .../gof/src/state/after/Published.java | 20 ++++++ design-pattern/gof/src/state/after/State.java | 8 +++ .../gof/src/state/after/Student.java | 30 +++++++++ .../gof/src/state/before/Client.java | 23 +++++++ .../gof/src/state/before/OnlineCourse.java | 63 +++++++++++++++++++ .../gof/src/state/before/Student.java | 30 +++++++++ 10 files changed, 298 insertions(+) create mode 100644 design-pattern/gof/src/state/after/Client.java create mode 100644 design-pattern/gof/src/state/after/Draft.java create mode 100644 design-pattern/gof/src/state/after/OnlineCourse.java create mode 100644 design-pattern/gof/src/state/after/Private.java create mode 100644 design-pattern/gof/src/state/after/Published.java create mode 100644 design-pattern/gof/src/state/after/State.java create mode 100644 design-pattern/gof/src/state/after/Student.java create mode 100644 design-pattern/gof/src/state/before/Client.java create mode 100644 design-pattern/gof/src/state/before/OnlineCourse.java create mode 100644 design-pattern/gof/src/state/before/Student.java diff --git a/design-pattern/gof/src/state/after/Client.java b/design-pattern/gof/src/state/after/Client.java new file mode 100644 index 00000000..034d5955 --- /dev/null +++ b/design-pattern/gof/src/state/after/Client.java @@ -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); + } +} diff --git a/design-pattern/gof/src/state/after/Draft.java b/design-pattern/gof/src/state/after/Draft.java new file mode 100644 index 00000000..8e06e443 --- /dev/null +++ b/design-pattern/gof/src/state/after/Draft.java @@ -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)); + } + } +} diff --git a/design-pattern/gof/src/state/after/OnlineCourse.java b/design-pattern/gof/src/state/after/OnlineCourse.java new file mode 100644 index 00000000..c8232758 --- /dev/null +++ b/design-pattern/gof/src/state/after/OnlineCourse.java @@ -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 students = new ArrayList<>(); + + private List 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 getStudents() { + return students; + } + + public List getReviews() { + return reviews; + } + + public void changeState(State state) { + this.state = state; + } + + @Override + public String toString() { + return "OnlineCourse{" + + "state=" + state + + ", students=" + students + + ", reviews=" + reviews + + '}'; + } + +} diff --git a/design-pattern/gof/src/state/after/Private.java b/design-pattern/gof/src/state/after/Private.java new file mode 100644 index 00000000..eaf4032a --- /dev/null +++ b/design-pattern/gof/src/state/after/Private.java @@ -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("프라이빗 코스를 수강할 수 없습니다."); + } + } +} diff --git a/design-pattern/gof/src/state/after/Published.java b/design-pattern/gof/src/state/after/Published.java new file mode 100644 index 00000000..d18170ed --- /dev/null +++ b/design-pattern/gof/src/state/after/Published.java @@ -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); + } +} diff --git a/design-pattern/gof/src/state/after/State.java b/design-pattern/gof/src/state/after/State.java new file mode 100644 index 00000000..5f4b2a43 --- /dev/null +++ b/design-pattern/gof/src/state/after/State.java @@ -0,0 +1,8 @@ +package state.after; + +public interface State { + + void addReview(String review, Student student); + + void addStudent(Student student); +} diff --git a/design-pattern/gof/src/state/after/Student.java b/design-pattern/gof/src/state/after/Student.java new file mode 100644 index 00000000..13132c50 --- /dev/null +++ b/design-pattern/gof/src/state/after/Student.java @@ -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 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 + '\'' + + '}'; + } +} diff --git a/design-pattern/gof/src/state/before/Client.java b/design-pattern/gof/src/state/before/Client.java new file mode 100644 index 00000000..e98306d3 --- /dev/null +++ b/design-pattern/gof/src/state/before/Client.java @@ -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()); + } +} diff --git a/design-pattern/gof/src/state/before/OnlineCourse.java b/design-pattern/gof/src/state/before/OnlineCourse.java new file mode 100644 index 00000000..817d0aaa --- /dev/null +++ b/design-pattern/gof/src/state/before/OnlineCourse.java @@ -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 reviews = new ArrayList<>(); + + private List 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 getReviews() { + return reviews; + } + + public List getStudents() { + return students; + } + + private boolean availableTo(Student student) { + return student.isEnabledForPrivateClass(this); + } + + +} diff --git a/design-pattern/gof/src/state/before/Student.java b/design-pattern/gof/src/state/before/Student.java new file mode 100644 index 00000000..354d04aa --- /dev/null +++ b/design-pattern/gof/src/state/before/Student.java @@ -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 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 + '\'' + + '}'; + } +}