본문 바로가기

강의/Design pattern

상태(State) 패턴

SMALL

State Pattern

상태에 따라 행동이 달라지는 객체를 위한 패턴.
상태에 특화된 행동들을 분리할 수 있고, 새로운 행동을 추가하더라도 다른 행동에 영향을 주지 않음.


Code Example

Client

public class Client {
  public static void main(String[] args) {
    Student student = new Student("student");
    OnlineCourse onlineCourse = new OnlineCourse();
    
    Student s = new Student("s");
    s.addPrivateCourse(onlineCourse);
    
    onlineCourse.addStudent(student);
    onlineCourse.changeState(OnlineCourse.State.PRIVATE);
    
    onlineCourse.addStudent(s);
    onlineCourse.addReview("hello", student);
  }
}

# Code Example - 2

Client

public class Client {
  public static void main(String[] args) {
    OnlineCourse onlineCourse = new OnlineCourse();
    Student student = new Student("student");
    
    onlineCourse.addStudent(student);
    onlineCourse.changeState(OnlineCourse.State.PRIVATE);
    onlineCourse.addReview("hello", student);
    
  }
}

Student

public class Student {
  private String name;
  
  public Student (String anme) {
    this.name = name;
  }
  private Set<OnlineCourse> onlineCourse = new HashSet<>();
  
  public boolean isAvailable (OnlineCourse onlineCourse) {
    return onlineCourse.contains(onlineCourse);
  }
  public void addPrivate(OnlineCourse onlineCourse) {
    this.onlineCourses.add(onlineCourse);
  }
}

OnlineCourse

public class OnlineCourse {
  private State state;
  private List<Student> students;
  private List<String> reviews;
  public void addStudent(Student student) {
    this.state.addStudent(student);
  }
  public void addReview(String review, Student student) {
    this.students.add(student);
    if(this.students.size() > 1) {
      this.
    }
  }
}

Draft - State

public class Draft implements State {
  private OnlineCourse onlineCourse;
  
  public Draft(OnlineCourse onlineCourse) {
    this.onlineCourse = onlineCourse;
  }
  
  @Override
  public void addReview(String review, Student student) {
    throw new UnsupportedOperationException("no review");
  }
  
  @Override
  public void addStudent(Student student) {
    this.onlineCourse.getStudents().add(student);
    if(this.onlineCourse.getStudents() > 1) {
      this.changeState(new )
    }
  }
}

Private

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 {
      throw new UnsupportedOperationException("private");
    }
  }
  
  @Override
  public void addStudent(Student student) {
    if(student.isAvailable(this.onlineCourse) {
      this.onlineCourse.adStudent(student);
    } else {
      throw new UnsupportedOperationException("private");
    }
  }
}

Published

public class Published implements State {
  private OnlineCourse onlineCourse;
  
  public Published (OnlineCourse onlinecourse) {
    this.onlineCourse = onlineCourse;
  }
  
  @Override
  public void addReview(String review, String student) {
    throw new UnsupportedOperationException ("no draft");
  }
  
  @Override
  public void addStudent (Student student) {
    this.onlineCourse.getStudents().add(studnet);
    if(this.onlineCourse.getStudnets().size() > 1) {
      this.onlineCourse.changeState(new Private());
    }
  }
} 

장단점

장점

  • 상태에 따른 동작을 개별 클래스로 옮겨서 관리할 수 있다.
  • 기존의 특정 상태에 따른 동작을 변경하지 않고 새로운 상태에 다른 동작을 추가할 수 있다.
  • 코드 복잡도를 줄일 수 있다.

단점

  • 복잡도가 증가한다.
SMALL

'강의 > Design pattern' 카테고리의 다른 글

템플릿 메소드 (Template method) 패턴  (0) 2022.09.22
전략(Strategy) 패턴  (0) 2022.09.22
관찰자(Observer) 패턴  (0) 2022.09.22
중재자(Mediator) 패턴  (0) 2022.09.22
이터레이터(Iterator) 패턴  (0) 2022.09.22