Skip to content

Commit b3acbd0

Browse files
committed
fix: improve code formatting and string interpolation in strategy pattern examples
1 parent 2e70f04 commit b3acbd0

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

behavioral_design_patterns/strategy/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,7 @@ void main() {
346346

347347
### Payment Example
348348

349-
- /payment_example.dart
350-
- <strong><a href="storage_example.dart" target="_blank">Storage Data Source Example</a></strong>
349+
- <strong><a href="payment_example.dart" target="_blank">Payment Example</a></strong>
351350

352351

353352
## Summery

behavioral_design_patterns/strategy/payment_example.dart

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore_for_file: constant_identifier_names
2+
13
import 'dart:developer';
24

35
/// Payment Example
@@ -13,11 +15,11 @@ class PaymentProcessor {
1315

1416
void processPayment(double amount) {
1517
if (_paymentType == PaymentType.CREDIT_CARD) {
16-
log("Processing credit card payment of amount " + "$amount");
18+
log("Processing credit card payment of amount $amount");
1719
} else if (_paymentType == PaymentType.DEBIT_CARD) {
18-
log("Processing debit card payment of amount " + "$amount");
20+
log("Processing debit card payment of amount $amount");
1921
} else if (_paymentType == PaymentType.PAYPAL) {
20-
log("Processing PayPal payment of amount " + "$amount");
22+
log("Processing PayPal payment of amount $amount");
2123
} else {
2224
throw "Invalid payment type";
2325
}
@@ -40,12 +42,12 @@ enum PaymentType { CREDIT_CARD, DEBIT_CARD, PAYPAL }
4042
/// The PaymentProcessor class violates the Strategy pattern by using conditional statements to determine the type of payment and then processing it accordingly.
4143
/// This approach can quickly become unmanageable and inflexible as the number of payment types increases.
4244
43-
///
44-
///
45+
///
46+
///
4547
/// FIX
46-
///
48+
///
4749
/// To fix this problem, you can use the Strategy Design Pattern
48-
///
50+
///
4951
/// Step 1: Identify the algorithm or behavior that needs to be encapsulated and made interchangeable.
5052
/// Step 2: Define an interface that represents the behavior, with a single method signature that takes in any required parameters.
5153
/// Step 3: Implement concrete classes that provide specific implementations of the behavior defined in the interface.
@@ -60,18 +62,21 @@ abstract interface class PaymentStrategy {
6062
/// For example, here are the CreditCardPaymentStrategy, DebitCardPaymentStrategy, and PaypalPaymentStrategy classes:
6163
6264
class CreditCardPaymentStrategy implements PaymentStrategy {
65+
@override
6366
void processPayment(double amount) {
6467
log("Processing credit card payment of amount: $amount");
6568
}
6669
}
6770

6871
class DebitCardPaymentStrategy implements PaymentStrategy {
72+
@override
6973
void processPayment(double amount) {
7074
log("Processing debit card payment of amount: $amount");
7175
}
7276
}
7377

7478
class PaypalPaymentStrategy implements PaymentStrategy {
79+
@override
7580
void processPayment(double amount) {
7681
log("Processing PayPal payment of amount: $amount");
7782
}
@@ -82,7 +87,7 @@ class PaypalPaymentStrategy implements PaymentStrategy {
8287
class PaymentProcessorStrategy {
8388
PaymentStrategy? _paymentStrategy;
8489

85-
PaymentProcessor(PaymentStrategy paymentStrategy) {
90+
void paymentProcessor(PaymentStrategy paymentStrategy) {
8691
_paymentStrategy = paymentStrategy;
8792
}
8893

behavioral_design_patterns/strategy/storage_example.dart

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/// Storage Example
2+
library;
23

34
/// This example shows a simple implementation of a list controller that is
45
/// able to display models from different data sources:

behavioral_design_patterns/strategy/strategy_operation_example.dart

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
///
21
/// Step 1: Create an interface.
32
abstract interface class Strategy {
43
int doOperation(int num1, int num2);
54
}
65

7-
///
6+
///
87
/// Step 2: Create concrete classes implementing the same interface.
98
109
/// OperationAdd
@@ -30,24 +29,24 @@ class OperationMultiply implements Strategy {
3029
class Context {
3130
final Strategy _strategy;
3231

33-
Context(Strategy strategy) : this._strategy = strategy;
32+
Context(Strategy strategy) : _strategy = strategy;
3433

3534
int executeStrategy(int num1, int num2) {
3635
return _strategy.doOperation(num1, num2);
3736
}
3837
}
3938

40-
///
39+
///
4140
/// Step 4: Use the Context to see change in behavior when it changes its Strategy.
4241
void main() {
4342
Context context = Context(OperationAdd());
44-
print("10 + 5 = " + "${context.executeStrategy(10, 5)}");
43+
print("10 + 5 = ${context.executeStrategy(10, 5)}");
4544

4645
context = Context(OperationSubtract());
47-
print("10 - 5 = " + "${context.executeStrategy(10, 5)}");
46+
print("10 - 5 = ${context.executeStrategy(10, 5)}");
4847

4948
context = Context(OperationMultiply());
50-
print("10 * 5 = " + "${context.executeStrategy(10, 5)}");
49+
print("10 * 5 = ${context.executeStrategy(10, 5)}");
5150
}
5251

5352
/// Step 5: Verify the output.

0 commit comments

Comments
 (0)