闽公网安备 35020302035485号
策略模式(Strategy Pattern)是一种行为设计模式,能在运行时根据对象的行为进行变化。在策略模式中,行为被封装到具有共同接口的独立类中,可以从一个对象中独立出一些行为的具体实现,进而在不同的情况下使用不同的行为。
abstract class PaymentStrategy {
void pay(double amount);
}
定义具体策略:class CreditCardPayment implements PaymentStrategy {
// 堆代码 duidaima.com
@override
void pay(double amount) {
print('Paying $amount using Credit Card');
}
}
class AlipayPayment implements PaymentStrategy {
@override
void pay(double amount) {
print('Paying $amount using Alipay');
}
}
class WeChatPayment implements PaymentStrategy {
@override
void pay(double amount) {
print('Paying $amount using WeChat Pay');
}
}
定义上下文:class ShoppingCart {
PaymentStrategy? _paymentStrategy;
void setPaymentStrategy(PaymentStrategy? strategy) {
_paymentStrategy = strategy;
}
void checkout(double amount) {
_paymentStrategy?.pay(amount);
}
}
然后你可以在你的 UI 代码中使用这些策略:void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Strategy Pattern Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Strategy Pattern Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ShoppingCart _cart = ShoppingCart();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Pay with Credit Card'),
onPressed: () {
_cart.setPaymentStrategy(CreditCardPayment());
_cart.checkout(100.0);
},
),
RaisedButton(
child: Text('Pay with Alipay'),
onPressed: () {
_cart.setPaymentStrategy(AlipayPayment());
_cart.checkout(100.0);
},
),
RaisedButton(
child: Text('Pay with WeChat Pay'),
onPressed: () {
_cart.setPaymentStrategy(WeChatPayment());
_cart.checkout(100.0);
},
),
],
),
),
);
}
}
当用户点击按钮时,会使用用户选择的支付方式进行支付。在 PaymentStrategy 的 pay 方法中实现调用支付 API 的逻辑。abstract class ShippingCostStrategy {
double calculate(double weight);
}
定义具体策略:class FreeShipping implements ShippingCostStrategy {
@override
double calculate(double weight) {
return 0.0;
}
}
class FixedCostShipping implements ShippingCostStrategy {
@override
double calculate(double weight) {
return 5.0;
}
}
class WeightBasedShipping implements ShippingCostStrategy {
@override
double calculate(double weight) {
return weight * 0.5;
}
}
定义上下文:class ShoppingCart {
ShippingCostStrategy? _shippingCostStrategy;
void setShippingCostStrategy(ShippingCostStrategy? strategy) {
_shippingCostStrategy = strategy;
}
double checkout(double weight) {
return _shippingCostStrategy?.calculate(weight) ?? 0.0;
}
}
然后你可以在你的 UI 代码中使用这些策略:void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Strategy Pattern Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Strategy Pattern Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ShoppingCart _cart = ShoppingCart();
double weight = 10.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
child: Text('Free Shipping'),
onPressed: () {
_cart.setShippingCostStrategy(FreeShipping());
print('Shipping cost: ${_cart.checkout(weight)}');
},
),
ElevatedButton(
child: Text('Fixed Cost Shipping'),
onPressed: () {
_cart.setShippingCostStrategy(FixedCostShipping());
print('Shipping cost: ${_cart.checkout(weight)}');
},
),
ElevatedButton(
child: Text('Weight Based Shipping'),
onPressed: () {
_cart.setShippingCostStrategy(WeightBasedShipping());
print('Shipping cost: ${_cart.checkout(weight)}');
},
),
],
),
),
);
}
}
当用户点击按钮时,应用会使用用户选择的运费计算策略来计算运费。在 ShippingCostStrategy 的 calculate 方法中实现的运费计算逻辑。策略模式是一种非常有用的设计模式,它可以让你根据情况在运行时切换算法或策略。在 Flutter 中,你可以通过定义接口和实现这些接口的类来使用策略模式,然后在需要的地方使用这些策略。