设计模式☞策略模式

价格

package 价格;
public abstract class Price {
	public abstract double getPrice(double p);
}

package 价格;

public class CashNormol extends Price {
	@Override
	public double getPrice(double cash) {
		
		return cash;
	}

}

package 价格;

public class CashDiscount extends Price {

	private double rate;
	
	public CashDiscount(double rate) {
		this.rate = rate*0.1;
	}

	@Override
	public double getPrice(double cash) {
		// TODO Auto-generated method stub
		return cash*rate;
	}

}

package 价格;

public class CashReturn extends Price {
	private double moneyCondition;
	private double moneyReturn;
	
	public CashReturn(double con,double re) {
		this.moneyCondition=con;
		this.moneyReturn=re;
	}

	@Override
	public double getPrice(double cash) {
		double a=cash/this.moneyCondition;
		int b=(int)a;
		return cash-this.moneyReturn*b;
	}

}

使用策略模式创建对象

package 实现计算超市打折后的价格_策略模式;

import 价格.Price;

public class Context {
	private Price p;
	public Context(Price p) {
		this.p = p;
	}
	public double test(double price) {
		return p.getPrice(price);
	}
	
	
	
}

客户端

package 实现计算超市打折后的价格_策略模式;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JComboBox;

import 价格.CashDiscount;
import 价格.CashNormol;
import 价格.CashReturn;

import javax.swing.DefaultComboBoxModel;


public class Client {

	private Frame fr;
	private TextField tf1;
	private TextField tf2;
	private JComboBox<String> jcb;
//	private TextArea ta;
	private Button bt1,bt2;
	private TextField tf3;
	private Label lab1,lab2;
	
	public Client() {
		init();
	}
	
	
	public void init() {
		fr=new Frame();
		fr.setLayout(new FlowLayout());
		fr.setBounds(700,300, 400, 500);
		tf1=new TextField(40);
		tf2=new TextField(40);
		tf3=new TextField(40);
		
		jcb=new JComboBox<String>();
		jcb.setModel(new DefaultComboBoxModel<String>(new String[] {"\u539F\u4EF7", "8\u6298", "\u6EE1300\u51CF100"}));
//		ta=new TextArea(20,40);
		bt1=new Button("确定");
		bt2=new Button("重置");
		lab1=new Label("单价");
		lab2=new Label("数量");
		fr.add(lab1);
		fr.add(tf1);
		
		fr.add(lab2);
		fr.add(tf2);
		fr.add(jcb);
		fr.add(bt1);
		fr.add(bt2);
//		fr.add(ta);
		fr.add(tf3);
		fr.setVisible(true);
		
		myEvent();
		
		
	}
	private void myEvent() {
		fr.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		//给计算总价钱
	bt1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			String p1=tf1.getText();
			double p2=Double.parseDouble(p1);
			String c1=tf2.getText();
			double c2=Double.parseDouble(c1);
			//计算总价
			String s=(String)jcb.getSelectedItem();
			double sum=0;
			Context con=null;
			switch(s) {
			case "原价":{
				 con=new Context(new CashNormol());
				break;
			}
			case "8折":{
				 con=new Context(new CashDiscount(8));
				break;
			}
			case "满300减100":{
				 con=new Context(new CashReturn(300,100));
				 break;
			}
			}
			sum=con.test(p2*c2);
			String string = String.valueOf(sum);
			tf3.setText(string);
			
			
			//显示列表
		}
	});
	//重置
	bt2.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			tf1.setText("");
			tf2.setText("");
			tf3.setText("");
		}
	});
		
	}
	
	public static void main(String[] args) {
		new Client();

	}
	
}

策略模式和简单工厂结合创建对象

package 实现计算超市打折后的价格_策略工厂结合;

import 价格.CashDiscount;
import 价格.CashNormol;
import 价格.CashReturn;
import 价格.Price;

public class Context {
	private Price con;
	public Context(String p) {
		switch(p) {
			case "原价":{
				 con=(new CashNormol());
				break;
			}
			case "8折":{
				 con=(new CashDiscount(8));
				break;
			}
			case "满300减100":{
				 con=(new CashReturn(300,100));
				 break;
			}
		}
	}
	public double test(double price) {
		return con.getPrice(price);
	}
	
	
	
}

package 实现计算超市打折后的价格_策略工厂结合;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;



public class Client {
	private Frame fr;
	private TextField tf1;
	private TextField tf2;
	private JComboBox<String> jcb;
//	private TextArea ta;
	private Button bt1,bt2;
	private TextField tf3;
	private Label lab1,lab2;
	
	public Client() {
		init();
	}
	
	
	public void init() {
		fr=new Frame();
		fr.setLayout(new FlowLayout());
		fr.setBounds(700,300, 400, 500);
		tf1=new TextField(40);
		tf2=new TextField(40);
		tf3=new TextField(40);
		
		jcb=new JComboBox<String>();
		jcb.setModel(new DefaultComboBoxModel<String>(new String[] {"\u539F\u4EF7", "8\u6298", "\u6EE1300\u51CF100"}));
//		ta=new TextArea(20,40);
		bt1=new Button("确定");
		bt2=new Button("重置");
		lab1=new Label("单价");
		lab2=new Label("数量");
		fr.add(lab1);
		fr.add(tf1);
		fr.add(lab2);
		fr.add(tf2);
		fr.add(jcb);
		fr.add(bt1);
		fr.add(bt2);
//		fr.add(ta);
		fr.add(tf3);
		fr.setVisible(true);
		
		myEvent();
		
		
	}
	private void myEvent() {
		fr.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		//给计算总价钱
	bt1.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			
			String p1=tf1.getText();
			double p2=Double.parseDouble(p1);
			String c1=tf2.getText();
			double c2=Double.parseDouble(c1);
			//计算总价
			String s=(String)jcb.getSelectedItem();
			double sum=0;
			Context con=new Context(s);
			
			sum=con.test(p2*c2);
			String string = String.valueOf(sum);
			tf3.setText(string);
			
			
			//显示列表
		}
	});
	//重置
	bt2.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			tf1.setText("");
			tf2.setText("");
			tf3.setText("");
		}
	});
		
	}
	
	public static void main(String[] args) {
		new Client();

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值