private void btnOK_Click(object sender, RoutedEventArgs e) { // 根据下拉选择框,将相应的算法类型字符串传入CashContext的对象中 CashContext cc = new CashContext(cbxType.SelectedItem.ToString()); double totalPrices = 0d; totalPrices = cc.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text)); total = total + totalPrices; lbxList.Items.Add($"单价:{txtPrice.Text} 数量:{txtNum.Text} {cbxType.SelectedItem} 合计:{totalPrices}"); lblResult.Text = total.ToString(); }
static void Main(string[] args) { CashContext context; // 由于实例化不同的策略,所以最终在调用context.ContextInterface();时,所获得的结果就不尽相同 context = new CashContext(new ConcreteStrategyA()); context.ContextInterface(); context = new CashContext(new ConcreteStrategyB()); context.ContextInterface(); context = new CashContext(new ConcreteStrategyC()); context.ContextInterface(); }