璇汇€婂ぇ璇濊璁℃ā寮忋€嬧€斺€斿簲鐢ㄧ瓥鐣ユā寮忕殑"鍟嗗満鏀堕摱绯荤粺"(WinForm)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璇汇€婂ぇ璇濊璁℃ā寮忋€嬧€斺€斿簲鐢ㄧ瓥鐣ユā寮忕殑"鍟嗗満鏀堕摱绯荤粺"(WinForm)相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/break' title='break'>break
鏌ヨ nbsp center 鏄剧ず 鍖哄埆 new 鐢熸垚 art绛栫暐妯″紡鐨勭粨鏋?/strong>
杩欎釜妯″紡娑夊強鍒颁笁涓鑹?
鐜锛圕ontext锛夎鑹?/strong>锛氭寔鏈変竴涓?Strategy 绫荤殑寮曠敤銆?br />鎶借薄绛栫暐锛圫trategy锛夎鑹?/strong>锛氳繖鏄竴涓娊璞¤鑹诧紝閫氬父鐢变竴涓帴鍙f垨鎶借薄绫诲疄鐜般€傛瑙掕壊缁欏嚭鎵€鏈夌殑鍏蜂綋绛栫暐绫绘墍闇€鐨勬帴鍙c€?br />鍏蜂綋绛栫暐锛圕oncreteStrategy锛夎鑹?/strong>锛氬寘瑁呬簡鐩稿叧鐨勭畻娉曟垨琛屼负銆?/p>
涓婄瘒鍗氭枃鍐欑殑CashSuper 灏辨槸鎶借薄绛栫暐锛岃€屾甯告敹璐?CashNormal銆佹墦鎶樻敹璐?CashRebate 鍜岃繑鍒╂敹璐?CashReturn 灏辨槸涓変釜鍏蜂綋绛栫暐锛屼篃灏辨槸绛栫暐妯″紡涓鐨勫叿浣撶畻娉曘€?/p>
銆€銆€闄勪笂涓婄瘒鍗氭枃鐨勯儴鍒嗕唬鐮?/p>
銆€銆€鍔犲叆鐨勭瓥鐣ユā寮忥紙杩欓噷鍙互寮冪敤宸ュ巶妯″紡浜嗭級 銆€銆€浣嗘槸绋嬪簭杩樻槸灏戜笉浜唖witch...case璇彞锛?/p>
銆€銆€鏍稿績浠g爜(v1.3) 銆€銆€鏈€鍒濈殑绛栫暐妯″紡鏄湁缂虹偣鐨勶紝瀹㈡埛绔繀椤荤煡閬撴墍鏈夌殑绛栫暐绫伙紝骞惰嚜琛屽喅瀹氫娇鐢ㄥ摢涓€涓瓥鐣ョ被銆傝繖灏辨剰鍛崇潃瀹㈡埛绔繀椤荤悊瑙h繖浜涚畻娉曠殑鍖哄埆锛屼互渚块€傛椂閫夋嫨鎭板綋鐨勭畻娉曠被銆傛崲瑷€涔嬶紝绛栫暐妯″紡鍙€傜敤浜庡鎴?/strong> 銆€銆€鍘绘帀switch...case璇彞锛侊紒锛?/strong>——锛堟湰妗堜緥閲囩敤绠€鍗曠殑.net鎶€鏈細鍙嶅皠锛?/p>
銆€銆€ 銆€銆€鍏抽敭鐨勬搷浣滀唬鐮佷负锛?strong>Assembly.Load(" 绋嬪簭闆嗗悕绉?).CreateInstance(" 鍚嶇О绌洪棿.绫诲悕绉?)锛?/strong> 銆€銆€ 銆€銆€ 銆€銆€瀹㈡埛绔唬鐮?v1.4) 銆€銆€閫氳繃绋嬪簭鍘昏XML鐨勯厤缃枃浠讹紝鏉ョ敓鎴愯繖涓笅鎷夊垪琛ㄦ锛岀劧鍚庡啀鏍规嵁鐢ㄦ埛鐨勯€夋嫨锛岄€氳繃鍙嶅皠瀹炴椂鐨勫疄渚嬪寲鍑虹浉搴旂殑绠楁硶瀵硅薄锛屾渶缁堝埄鐢ㄧ瓥鐣ユā寮忚绠楁渶缁堢殑缁撴灉銆?/strong> 銆€銆€XML鏂囦欢——CashAcceptType.xml浠g爜濡備笅 銆€銆€ 銆€銆€鐜板湪鏃犺闇€姹傛槸浠€涔堬紝鐢ㄧ幇鍦ㄧ殑绋嬪簭锛屽彧闇€瑕佹敼鏀筙ML鏂囦欢灏卞叏閮ㄦ憜骞充簡銆傛瘮濡傜幇鍦ㄨ€佹澘瑙夊緱鐜板湪婊?00閫?00澶浜嗭紝瑕佹敼鎴愰€?0锛屾垜鍙渶瑕佸幓XML鏂囦欢閲屾敼灏辫浜嗐€?/p>
娉細濡傝娣诲姞鏂扮殑绠楁硶锛岄偅涔堣绠楁硶绫荤户鎵緾ashSuper,鍐嶅幓鏀逛竴涓媂ML鏂囦欢灏卞彲浠ヤ簡銆?/p>//姝e父娑堣垂锛岀户鎵緾ashSuper
class CashNormal:CashSuper
{
public override double acceptCash(double money)
{
return money;
}
}
//鎵撴姌鏀惰垂娑堣垂锛岀户鎵緾ashSuper
class CashRebate:CashSuper
{
private double moneyRebate = 1d;
//鍒濆鍖栨椂锛屽繀闇€瑕佽緭鍏ユ姌鎵g巼锛屽鍏姌锛屽氨鏄?,8
public CashRebate(string moneyRebate)
{
//鐣岄潰鍚戠被浼犲€?/span>
this.moneyRebate = double.Parse(moneyRebate);
}
public override double acceptCash(double money)
{
return money * moneyRebate;
}
}
//杩斿埄鏀惰垂
class CashReturn:CashSuper
{
private double moneyCondition = 0.0d;
private double moneyReturn = 0.0d;
//鍒濆鍖栨椂蹇呴』瑕佽緭鍏ヨ繑鍒╂潯浠跺拰杩斿埄鍊硷紝姣斿婊?00杩?00
//鍒檓oneyCondition涓?00,moneyReturn涓?00
public CashReturn(string moneyCondition, string moneyReturn)
{
this.moneyCondition =double.Parse(moneyCondition);
this.moneyReturn = double.Parse(moneyReturn);
}
public override double acceptCash(double money)
{
double result = money;
//鑻ュぇ浜庤繑鍒╂潯浠讹紝鍒欓渶瑕佸噺鍘昏繑鍒╁€?/span>
if (money >= moneyCondition)
{
result = money - Math.Floor(money / moneyCondition) * moneyReturn;
}
return result;
}
}
//鐜伴噾鏀跺彇鐖剁被
abstract class CashSuper
{
//鎶借薄鏂规硶:鏀跺彇鐜伴噾锛屽弬鏁颁负鍘熶环锛岃繑鍥炰负褰撳墠浠?/span>
public abstract double acceptCash(double money);
}
1 namespace ExtendDiscountOfStrategyPattern
2 {
3 class CashContext
4 {
5 //澹版槑涓€涓幇閲戞敹璐圭埗绫诲璞?/span>
6 private CashSuper cs;
7
8 //璁剧疆绛栫暐琛屼负,鍙傛暟涓哄叿浣撶殑鐜伴噾鏀惰垂瀛愮被(姝e父,鎵撴姌鎴栬繑鍒?
9 public void setBehavior(CashSuper csuper)
10 {
11 this.cs = csuper;
12 }
13
14 //寰楀埌鐜伴噾淇冮攢璁$畻缁撴灉锛堝埄鐢ㄤ簡澶氭€佹満鍒讹紝涓嶅悓鐨勭瓥鐣ヨ涓哄鑷翠笉鍚岀殑缁撴灉)
15 public double GetResult(double money)
16 {
17 return cs.acceptCash(money);
18 }
19 }
20 }
1 //澹版槑涓€涓猟ouble鍙橀噺total鏉ヨ绠楁€昏
2 double total = 0.0d;
3 private void btnConfirm_Click(object sender, EventArgs e)
4 {
5 //澹版槑涓€涓猟ouble鍙橀噺totalPrices
6 double totalPrices = 0d;
7 //绛栫暐妯″紡
8 CashContext cc = new CashContext();
9 switch (cbxType.SelectedItem.ToString())
10 {
11 case "姝e父娑堣垂":
12 cc.setBehavior(new CashNormal());
13 break;
14 case "婊?00杩?00":
15 cc.setBehavior(new CashReturn("300", "100"));
16 break;
17 case "鎵?鎶?/span>":
18 cc.setBehavior(new CashRebate("0.8"));
19 break;
20 case "鎵?鎶?/span>":
21 cc.setBehavior(new CashRebate("0.7"));
22 break;
23 case "鎵?鎶?/span>":
24 cc.setBehavior(new CashRebate("0.5"));
25 break;
26 }
27 totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
28 //灏嗘瘡涓晢鍝佸悎璁¤鍏ユ€昏
29 total = total + totalPrices;
30 //鍦ㄥ垪琛ㄦ涓樉绀轰俊鎭?/span>
31 lbxList.Items.Add("鍗曚环:" + txtPrice.Text + " 鏁伴噺:" + txtNum.Text + " 鍚堣:" + totalPrices.ToString());
32 //鍦╨blTotalShow鏍囩涓婃樉绀烘€昏鏁?/span>
33 lblTotalShow.Text = total.ToString();
34 }
绔煡閬撴墍鏈夌殑绠楁硶鎴栬涓虹殑鎯呭喌鏈€鍒濈殑绛栫暐妯″紡鏄湁缂虹偣鐨勶紝瀹㈡埛绔繀椤荤煡閬撴墍鏈夌殑绛栫暐绫伙紝骞惰嚜琛屽喅瀹氫娇鐢ㄥ摢涓€涓瓥鐣ョ被銆傝繖灏辨剰鍛崇潃瀹㈡埛绔繀椤荤悊瑙h繖浜涚畻娉曠殑鍖哄埆锛屼互渚块€傛椂閫夋嫨鎭板綋鐨勭畻娉曠被銆傛崲瑷€涔嬶紝绛栫暐妯″紡鍙€傜敤浜庡鎴风鐭ラ亾鎵€鏈夌殑绠楁硶鎴栬涓虹殑鎯呭喌銆?/strong> 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 //add
11 using CCWin;
12 using System.Data.SqlClient;
13
14 namespace ExtendDiscountOfStrategyPatternWithReflection
15 {
16 using System.Reflection;
17
18 public partial class frmMain :Skin_Metro
19 {
20
21 DataSet ds; //鐢ㄤ簬瀛樻斁閰嶇疆鏂囦欢淇℃伅
22
23 public frmMain()
24 {
25 InitializeComponent();
26 }
27
28 //澹版槑涓€涓猟ouble鍙橀噺total鏉ヨ绠楁€昏
29 double total = 0.0d;
30 private void btnConfirm_Click(object sender, EventArgs e)
31 {
32 //澹版槑涓€涓猟ouble鍙橀噺totalPrices
33 double totalPrices = 0d;
34 //绛栫暐妯″紡
35 CashContext cc = new CashContext();
36 //鏍规嵁鐢ㄦ埛鐨勯€夐」锛屾煡璇㈢敤鎴烽€夋嫨椤圭殑鐩稿叧琛?/span>
37 DataRow dr = ((DataRow[])ds.Tables[0].Select("name=鈥?/span>" + cbxType.SelectedItem.ToString() + "鈥?/span>"))[0];
38 //澹版槑涓€涓弬鏁扮殑瀵硅薄鏁扮粍
39 object[] args = null;
40 //鑻ユ湁鍙傛暟锛屽垯灏嗗叾鍒嗗壊鎴愬瓧绗︿覆鏁扮粍锛岀敤浜庡疄渚嬪寲鏃舵墍鐢ㄧ殑鍙傛暟
41 if (dr["para"].ToString() != "")
42 {
43 args = dr["para"].ToString().Split(鈥?/span>,鈥?/span>);
44 }
45 //閫氳繃鍙嶅皠瀹炰緥鍖栧嚭鐩稿簲鐨勭畻娉曞璞?/span>
46 cc.setBehavior((CashSuper)Assembly.Load("ExtendDiscountOfStrategyPatternWithReflection").
47 CreateInstance("ExtendDiscountOfStrategyPatternWithReflection." + dr["class"].ToString(), false,
48 BindingFlags.Default, null, args, null, null));
49
50 totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
51 //灏嗘瘡涓晢鍝佸悎璁¤鍏ユ€昏
52 total = total + totalPrices;
53 //鍦ㄥ垪琛ㄦ涓樉绀轰俊鎭?/span>
54 lbxList.Items.Add("鍗曚环:" + txtPrice.Text + " 鏁伴噺:" + txtNum.Text + " 鍚堣:" + totalPrices.ToString());
55 //鍦╨blTotalShow鏍囩涓婃樉绀烘€昏鏁?/span>
56 lblTotalShow.Text = total.ToString();
57 }
58
59 private void btnReset_Click(object sender, EventArgs e)
60 {
61 total = 0.0;
62 txtPrice.Text = "";
63 txtNum.Text = "";
64 lblTotalShow.Text = "";
65 lbxList.Items.Clear();
66 cbxType.SelectedIndex = 0;
67 }
68
69 private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
70 {
71 //鏁板瓧0~9鎵€瀵瑰簲鐨刱eychar涓?8~57
72 e.Handled = true;
73 //杈撳叆0-9
74 if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
75 {
76 e.Handled = false;
77 }
78 }
79
80 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
81 {
82 //鏁板瓧0~9鎵€瀵瑰簲鐨刱eychar涓?8~57
83 e.Handled = true;
84 //杈撳叆0-9
85 if ((e.KeyChar >= 47 && e.KeyChar <= 58) || (e.KeyChar == 8 || e.KeyChar==46))
86 {
87 e.Handled = false;
88 }
89 }
90
91 private void frmMain_Load(object sender, EventArgs e)
92 {
93 //璇诲彇閰嶇疆鏂囦欢
94 ds = new DataSet();
95 ds.ReadXml(Application.StartupPath + "\CashAcceptType.xml");
96 //灏嗚鍙栧埌鐨勮褰曠粦瀹氬埌涓嬫媺鍒楄〃妗嗕腑
97 foreach(DataRowView dr in ds.Tables[0].DefaultView)
98 {
99 cbxType.Items.Add(dr["name"].ToString());
100 }
101
102 //瑕佷笅鎷夐€夋嫨妗嗗湪鍔犺浇鐨勬椂鍊欙紝灏遍€夋嫨绱㈠紩涓?鐨勫厓绱?姝e父娑堣垂"
103 cbxType.SelectedIndex = 0;
104 }
105 }
106 }
1 <?xml version="1.0" encoding="utf-8" ?>
2 <CashAcceptType>
3 <type>
4 <name>姝e父娑堣垂</name>
5 <class>CashNormal</class>
6 <para></para>
7 </type>
8 <type>
9 <name>婊?00杩?00</name>
10 <class>CashReturn</class>
11 <para>300,100</para>
12 </type>
13 <type>
14 <name>婊?00杩?0</name>
15 <class>CashReturn</class>
16 <para>200,50</para>
17 </type>
18 <type>
19 <name>鎵?鎶?span style="color: #0000ff;"></name>
20 <class>CashRebate</class>
21 <para>0.8</para>
22 </type>
23 <type>
24 <name>鎵?鎶?span style="color: #0000ff;"></name>
25 <class>CashRebate</class>
26 <para>0.7</para>
27 </type>
28 <type>
29 <name>鎵?鎶?span style="color: #0000ff;"></name>
30 <class>CashRebate</class>
31 <para>0.5</para>
32 </type>
33 </CashAcceptType>