`

状态模式

 
阅读更多

 

状态设计模式

  • 使用场景:
    • 1、行为随状态改变而改变的场景(在游戏、GUI、设备控制、编译器等场景用的比较多)。
    • 2、条件、分支语句的代替者。
  • 注意事项:在行为受状态约束的时候使用状态模式,而且状态不超过 5 个。

用订单状态变化来举例:

`
    /**
     * @author 
     * 声明5种状态
     */
    public interface IStateModel {

        void unPay();

        void pay();

        void refund();

        void confirm();

        void update();
    }


import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * @author jacktan
 *
 */
public class StatePattern extends JFrame {

    private JPanel contentPane;
    private JButton btnPay;
    private JButton btnRefund;
    private JButton btnConfirm;
    private JLabel labelState;

    IStateModel state; 

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    StatePattern frame = new StatePattern();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public StatePattern() {
        setTitle("\u72B6\u6001\u8BBE\u8BA1\u6A21\u5F0F");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 260, 131);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        btnPay = new JButton("\u4ED8\u6B3E");
        btnPay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                StatePattern.this.setState(new PayPattern());
            }
        });

        btnRefund = new JButton("\u9000\u6B3E");
        btnRefund.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                StatePattern.this.setState(new RefundPattern());
            }
        });

        btnConfirm = new JButton("\u786E\u8BA4\u6536\u8D27");
        btnConfirm.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                StatePattern.this.setState(new ConfirmPattern());
            }
        });

        labelState = new JLabel("\u672A\u4ED8\u6B3E");
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                        .addComponent(btnPay, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnConfirm, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
                        .addComponent(labelState, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnRefund, GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))
                    .addContainerGap(62, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
            gl_contentPane.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_contentPane.createSequentialGroup()
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                        .addComponent(btnPay)
                        .addComponent(btnRefund))
                    .addPreferredGap(ComponentPlacement.RELATED)
                    .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addComponent(btnConfirm)
                        .addComponent(labelState))
                    .addContainerGap(30, Short.MAX_VALUE))
        );

        setState(new UnPay());
        contentPane.setLayout(gl_contentPane);
    }

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



    class ConfirmPattern implements IStateModel {

        @Override
        public void pay() {

        }

        @Override
        public void refund() {

        }

        @Override
        public void confirm() {
            StatePattern.this.setState(new UnPay());    
        }


        @Override
        public void update() {
            StatePattern.this.btnRefund.setEnabled(false);
            StatePattern.this.btnConfirm.setEnabled(false);
            StatePattern.this.btnPay.setEnabled(true);
            StatePattern.this.labelState.setText("已确认收货");    
        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#unPay()
         */
        @Override
        public void unPay() {

        }

    }

    class RefundPattern implements IStateModel {

        @Override
        public void pay() {

        }

        @Override
        public void refund() {
            StatePattern.this.setState(new UnPay());
        }

        @Override
        public void confirm() {

        }


        @Override
        public void update() {
            StatePattern.this.btnRefund.setEnabled(false);
            StatePattern.this.btnConfirm.setEnabled(false);
            StatePattern.this.btnPay.setEnabled(true);
            StatePattern.this.labelState.setText("已退款");    
        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#unPay()
         */
        @Override
        public void unPay() {

        }

    }

    class PayPattern implements IStateModel {

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#unPay()
         */
        @Override
        public void unPay() {

        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#pay()
         */
        @Override
        public void pay() {
            System.out.println("不能重复支付");
        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#refund()
         */
        @Override
        public void refund() {

        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#confirm()
         */
        @Override
        public void confirm() {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see 状态模式.IStateModel#update()
         */
        @Override
        public void update() {
            StatePattern.this.btnRefund.setEnabled(true);
            StatePattern.this.btnConfirm.setEnabled(true);
            StatePattern.this.btnPay.setEnabled(false);
            StatePattern.this.labelState.setText("已支付");
        }

    }

    class UnPay implements IStateModel{

        @Override
        public void unPay() {
            System.out.println("还未进行支付");
        }

        @Override
        public void pay() {
            StatePattern.this.setState(new PayPattern());
        }

        @Override
        public void refund() {
            System.out.println("还款进行支付,不能退款");
        }

        @Override
        public void confirm() {
            System.out.println("还款进行支付,不能确认收货");    
        }

        @Override
        public void update() {
            StatePattern.this.btnRefund.setEnabled(false);
            StatePattern.this.btnConfirm.setEnabled(false);
            StatePattern.this.btnPay.setEnabled(true);
            StatePattern.this.labelState.setText("未支付");
        }        
    }

}
`
<!-- This document was created with MarkdownPad, the Markdown editor for Windows (http://markdownpad.com) -->

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics