디자인 모드 = = 상태 모드 (State)

1758 단어 디자인 모드
/*
 *     (State)
 *                。
 *                      。
 */

package model;

import static java.lang.System.*;

public class TestState {
    public static void main(String[] args) {
        BBSUser u = new BBSUser();
        u.setState(new GuestState());
        u.publish();
        u.setState(new NormalState());
        u.publish();
        u.setState(new BlockedState());
        u.publish();
        u.setState(new NewComerState());
        u.publish();
    }
}

class BBSUser {
    private State state;

    public void setState(State state) {
        this.state = state;
    }

    public void publish() {
        state.action();
    }
}

abstract class State {
    public abstract void action();
}

class GuestState extends State {
    public void action() {
        out.println("       ,    ");
    }
}

class NormalState extends State {
    public void action() {
        out.println("       ,      ");
    }
}

class BlockedState extends State {
    public void action() {
        out.println("       ,      ");
    }
}

class NewComerState extends State {
    public void action() {
        out.println("    ,      ,3    ");
    }
}

class StateFactory {
    public static State createState(int i) {
        if (i == 1)
            return new GuestState();
        else
            return new NormalState();
    }
}

좋은 웹페이지 즐겨찾기