大话设计模式——策略模式

Posted 不见长安见晨雾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大话设计模式——策略模式相关的知识,希望对你有一定的参考价值。

1)商场收银系统

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
  * 商场收银系统
 */
@SuppressWarnings("serial")
public class Mall implements ActionListener 

    private static JLabel unitPriceLabel;
    private static JTextField unitPriceValue;
    private static JLabel numLabel;
    private static JTextField numValue;

    private double totalPrice = 0;
    private static JList<String> jList;
    private static DefaultListModel<String> listModel;
    private static JLabel totalNum;

    public static void main(String[] args) 
        JFrame frame = new JFrame("商城收银系统");
        frame.setSize(450, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    

    private static void placeComponents(JPanel panel) 
        panel.setLayout(null);
        Mall mainFrame = new Mall();
        // 第一行
        unitPriceLabel = new JLabel("单价:");
        unitPriceLabel.setBounds(10, 20, 80, 25);
        panel.add(unitPriceLabel);

        unitPriceValue = new JTextField(20);
        unitPriceValue.setBounds(100, 20, 165, 25);
        panel.add(unitPriceValue);

        JButton confirmButton = new JButton("确定");
        confirmButton.setBounds(280, 20, 80, 25);
        confirmButton.addActionListener(mainFrame);
        confirmButton.setActionCommand("confirm");
        panel.add(confirmButton);

        // 第二行
        numLabel = new JLabel("数量:");
        numLabel.setBounds(10, 50, 80, 25);
        panel.add(numLabel);

        numValue = new JTextField(20);
        numValue.setBounds(100, 50, 165, 25);
        panel.add(numValue);

        JButton resetButton = new JButton("重置");
        resetButton.setBounds(280, 50, 80, 25);
        resetButton.addActionListener(mainFrame);
        resetButton.setActionCommand("reset");
        panel.add(resetButton);

        // 第三行
        listModel = new DefaultListModel<String>();
        jList = new JList<String>(listModel);
        jList.setBounds(10, 90, 400, 180);
        panel.add(jList);

        // 第四行
        JLabel totalLabel = new JLabel("总计:");
        totalLabel.setBounds(10, 300, 80, 25);
        panel.add(totalLabel);
        totalNum = new JLabel("0.00");
        totalNum.setBounds(100, 300, 80, 25);
        panel.add(totalNum);
    

    @Override
    public void actionPerformed(ActionEvent event) 
        if (event.getActionCommand().equals("confirm")) 
            double unitPrice = Double.parseDouble(unitPriceValue.getText());
            double num = Double.parseDouble(numValue.getText());
            double total = unitPrice * num;
            listModel.addElement("单价:"+unitPriceValue.getText()+" 数量:"+numValue.getText() +" 合计:"+total);
            totalPrice +=total;
            totalNum.setText(totalPrice+"");
         else if(event.getActionCommand().equals("reset")) 
            unitPriceValue.setText("");
            numValue.setText("");
            listModel.clear();
            totalPrice=0;
            totalNum.setText(totalPrice+"");
        
    



执行效果

2)增加打折功能

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 需求:增加打折功能
 */
@SuppressWarnings("serial")
public class Mall implements ActionListener 

	private static JLabel unitPriceLabel;
	private static JTextField unitPriceValue;
	private static JLabel numLabel;
	private static JTextField numValue;

	private double totalPrice = 0;
	private static JList<String> jList;
	private static DefaultListModel<String> listModel;
	private static JLabel totalNum;
	private static JLabel calcType;
	private static JComboBox<String> jComboBox;

	public static void main(String[] args) 
		JFrame frame = new JFrame("商城收银系统");
		frame.setSize(450, 400);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		frame.add(panel);
		placeComponents(panel);
		frame.setVisible(true);
	

	private static void placeComponents(JPanel panel) 
		panel.setLayout(null);
		Mall mainFrame = new Mall();
		// 第一行
		unitPriceLabel = new JLabel("单价:");
		unitPriceLabel.setBounds(10, 20, 80, 25);
		panel.add(unitPriceLabel);

		unitPriceValue = new JTextField(20);
		unitPriceValue.setBounds(100, 20, 165, 25);
		panel.add(unitPriceValue);

		JButton confirmButton = new JButton("确定");
		confirmButton.setBounds(280, 20, 80, 25);
		confirmButton.addActionListener(mainFrame);
		confirmButton.setActionCommand("confirm");
		panel.add(confirmButton);

		// 第二行
		numLabel = new JLabel("数量:");
		numLabel.setBounds(10, 50, 80, 25);
		panel.add(numLabel);

		numValue = new JTextField(20);
		numValue.setBounds(100, 50, 165, 25);
		panel.add(numValue);

		JButton resetButton = new JButton("重置");
		resetButton.setBounds(280, 50, 80, 25);
		resetButton.addActionListener(mainFrame);
		resetButton.setActionCommand("reset");
		panel.add(resetButton);

		// 第三行
		calcType = new JLabel("计算方式:");
		calcType.setBounds(10, 90, 80, 25);
		panel.add(calcType);
		jComboBox = new JComboBox<String>();
		jComboBox.insertItemAt("正常收费", 0);
		jComboBox.insertItemAt("打八折", 1);
		jComboBox.insertItemAt("打七折", 2);
		jComboBox.insertItemAt("打五折", 3);
		jComboBox.setSelectedIndex(0);
		jComboBox.setBounds(100, 90, 80, 25);
		panel.add(jComboBox);

		// 第四行
		listModel = new DefaultListModel<String>();
		jList = new JList<String>(listModel);
		jList.setBounds(10, 120, 400, 180);
		panel.add(jList);

		// 第五行
		JLabel totalLabel = new JLabel("总计:");
		totalLabel.setBounds(10, 300, 80, 25);
		panel.add(totalLabel);
		totalNum = new JLabel("0.00");
		totalNum.setBounds(100, 300, 80, 25);
		panel.add(totalNum);
	

	@Override
	public void actionPerformed(ActionEvent event) 
		if (event.getActionCommand().equals("confirm")) 
			double unitPrice = Double.parseDouble(unitPriceValue.getText());
			double num = Double.parseDouble(numValue.getText());
			int selectedIndex = jComboBox.getSelectedIndex();
			double total = 0;
			switch (selectedIndex) 
			case 0:
				total = unitPrice * num;
				break;
			case 1:
				total = unitPrice * num * 0.8;
				break;
			case 2:
				total = unitPrice * num * 0.7;
				break;
			case 3:
				total = unitPrice * num * 0.5;
				break;
			
			totalPrice += total;
			listModel.addElement("单价:" + unitPriceValue.getText() + " 数量:" + numValue.getText() + " "
					+ jComboBox.getSelectedItem() + " 合计:" + total);
			totalNum.setText(totalPrice + "");
		 else if (event.getActionCommand().equals("reset")) 
			unitPriceValue.setText("");
			numValue.setText("");
			listModel.clear();
			totalPrice = 0;
			totalNum.setText(totalPrice + "");
		
	



执行效果

3)简单工厂实现

问题:如果增加满300返100的促销算法,该如何处理?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class Mall implements ActionListener 

    private static JLabel unitPriceLabel;
    private static JTextField unitPriceValue;
    private static JLabel numLabel;
    private static JTextField numValue;

    private double totalPrice = 0;
    private static JList<String> jList;
    private static DefaultListModel<String> listModel;
    private static JLabel totalNum;
    private static JLabel calcType;
    private static JComboBox<String> jComboBox;

    public static void main(String[] args) 
        JFrame frame = new JFrame("商城收银系统");
        frame.setSize(450, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation以上是关于大话设计模式——策略模式的主要内容,如果未能解决你的问题,请参考以下文章

策略模式算法选择

策略模式-商场促销活动

大话设计模式——策略模式

大话设计模式之策略模式

大话设计模式之策略模式

大话涉及模式Python实现-策略模式