Skip to content

Commit

Permalink
Merge branch 'design-patterns'
Browse files Browse the repository at this point in the history
  • Loading branch information
ApplebaumIan committed Nov 20, 2024
2 parents 59b46a8 + 5a02b72 commit ccb57c4
Show file tree
Hide file tree
Showing 509 changed files with 431 additions and 0 deletions.
105 changes: 105 additions & 0 deletions documentation/design-patterns/behavioral-patterns/strategy.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import Figure from "../../src/components/Figure";

# Strategy Pattern

> **Strategy** is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
>
> [~ Refactoring Guru](https://refactoring.guru/design-patterns/strategy)
<Figure caption={"Strategy Pattern UML Class Diagram"}>

```mermaid
classDiagram
direction RL
class Context {
- strategy
+ setStrategy(strategy)
+ doSomething()
}
note for Context "strategy.execute()"
Context <-- Client
note for Client "str = new SomeStrategy()
context.setStrategy(str)
context.doSomething()
// … More stuff happens…
other = new OtherStrategy()
context.setStrategy(other)
context.doSomething()"
Context o--> Strategy
class Strategy {
<<interface>>
+execute(data)
}
Strategy <|.. ConcreteStrategies
ConcreteStrategies <.. Client
```

</Figure>

The Strategy design pattern allows for the creation of a family of algorithms, each encapsulated in a separate class, making them interchangeable. This pattern is useful when a class has multiple ways of performing a task, as it allows for the extraction of these algorithms into separate classes, reducing code bloat and improving maintainability. The context class delegates work to a linked strategy object, abstracting away the specific implementation details.

The Strategy pattern allows for the interchangeable use of different algorithms within an object at runtime. It enables the isolation of algorithm implementation details from the code that uses them. The pattern is useful when there are multiple similar classes that differ only in their execution behavior.

## Slides

<Figure caption={"Strategy Pattern"} subcaption={
<a href={"/slides-html/Strategy-Pattern/#0"} target={"_blank"} rel="noopener noreferrer">full screen click here</a>
}>
<iframe height={"500"} width={"100%"} src="/slides-html/Strategy-Pattern/#0" title="W3Schools Free Online Web Tutorials"></iframe>
</Figure>

---

## Step by Step Implementation

Let’s use the Strategy Pattern to create a system that calculates discounts in a shopping cart. Different discount algorithms (e.g., percentage discount, fixed amount discount, or no discount) will act as interchangeable strategies. The steps follow your instructions.


<table><thead>
<tr>
<td><div className={""}>

<iframe height={"651px"} width={"294px"} src="/slides-html/Strategy-Pattern-Implementation/#0" title="W3Schools Free Online Web Tutorials"></iframe>
</div></td>
<td>



```jsx live
function StrategyDemo() {
class ShoppingCart {
constructor() {
this.totalAmount = 0;
this.discountAlgorithm = null; // Placeholder for strategy
}

setDiscountAlgorithm(discountAlgorithm) {
this.discountAlgorithm = discountAlgorithm;
}

calculateTotal() {
if (!this.discountAlgorithm) {
throw new Error("No discount algorithm set!");
}
return this.discountAlgorithm.calculate(this.totalAmount);
}

addItem(price) {
this.totalAmount += price;
}
}

return <div>Step 1: Context class ShoppingCart defined with discount algorithm placeholder.</div>;
}
```

</td>
</tr></thead>
</table>


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"assets":{"51EB4556A7340D2F36CD6F7A4DEC6F5F":{"type":"texture","index":1,"url":{"native":"assets\/03A259ED-90E9-48F3-B58E-A77D6B4C1DAF.pdf"},"width":294,"height":651},"FB5FD7964BCA6161BF74013E8B3AE2E4":{"type":"texture","index":0,"url":{"native":"assets\/03A259ED-90E9-48F3-B58E-A77D6B4C1DAF.pdf"},"width":294,"height":651}},"events":[{"effects":[{"beginTime":0,"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"51EB4556A7340D2F36CD6F7A4DEC6F5F"},{"animations":[{"additive":false,"timeOffset":0,"beginTime":0,"from":{"scalar":false},"repeatCount":0,"fillMode":"both","duration":0.001,"autoreverses":false,"property":"hidden","to":{"scalar":true},"removedOnCompletion":false}],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"FB5FD7964BCA6161BF74013E8B3AE2E4"}]}]},"effects":[],"duration":0.001,"type":"transition","attributes":{"direction":0},"name":"none","objectID":"0"}],"automaticPlay":false,"hyperlinks":[],"accessibility":[{"text":"1280px-Temple_University_Logo.svg.png","targetRectangle":{"y":606.9375,"x":4.1303524971008301,"width":58.023105621337891,"height":16.047016143798828}},{"text":"You’ve completed the Strategy Pattern! 🔀","targetRectangle":{"y":272.59736329317093,"x":32.225338092651384,"width":237.25547999999998,"height":105}},{"text":"Ian Tyler Applebaum • Instructor • Temple University CIS","targetRectangle":{"y":626.2970122396946,"x":5.0006940960884094,"width":265.8300000000001,"height":11.709930419921875}}],"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"FB5FD7964BCA6161BF74013E8B3AE2E4"}]}]}}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local_slide( {"name":"03A259ED-90E9-48F3-B58E-A77D6B4C1DAF","json":{"assets":{"51EB4556A7340D2F36CD6F7A4DEC6F5F":{"type":"texture","index":1,"url":{"native":"assets\/03A259ED-90E9-48F3-B58E-A77D6B4C1DAF.pdf"},"width":294,"height":651},"FB5FD7964BCA6161BF74013E8B3AE2E4":{"type":"texture","index":0,"url":{"native":"assets\/03A259ED-90E9-48F3-B58E-A77D6B4C1DAF.pdf"},"width":294,"height":651}},"events":[{"effects":[{"beginTime":0,"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"51EB4556A7340D2F36CD6F7A4DEC6F5F"},{"animations":[{"additive":false,"timeOffset":0,"beginTime":0,"from":{"scalar":false},"repeatCount":0,"fillMode":"both","duration":0.001,"autoreverses":false,"property":"hidden","to":{"scalar":true},"removedOnCompletion":false}],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"FB5FD7964BCA6161BF74013E8B3AE2E4"}]}]},"effects":[],"duration":0.001,"type":"transition","attributes":{"direction":0},"name":"none","objectID":"0"}],"automaticPlay":false,"hyperlinks":[],"accessibility":[{"text":"1280px-Temple_University_Logo.svg.png","targetRectangle":{"y":606.9375,"x":4.1303524971008301,"width":58.023105621337891,"height":16.047016143798828}},{"text":"You’ve completed the Strategy Pattern! 🔀","targetRectangle":{"y":272.59736329317093,"x":32.225338092651384,"width":237.25547999999998,"height":105}},{"text":"Ian Tyler Applebaum • Instructor • Temple University CIS","targetRectangle":{"y":626.2970122396946,"x":5.0006940960884094,"width":265.8300000000001,"height":11.709930419921875}}],"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"FB5FD7964BCA6161BF74013E8B3AE2E4"}]}]}}]}} )
Binary file not shown.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"assets":{"B7D9DDB759E932D533D9D40242EE25B0":{"type":"texture","index":0,"url":{"native":"assets\/0DEC9AE3-74CB-4DEE-B934-5FFDE65ABBAC.pdf"},"width":294,"height":651},"4358A62D71B8E6E05816CE4D3AD86A97":{"type":"texture","index":1,"url":{"native":"assets\/0DEC9AE3-74CB-4DEE-B934-5FFDE65ABBAC.pdf"},"width":294,"height":651}},"events":[{"effects":[{"beginTime":0,"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"4358A62D71B8E6E05816CE4D3AD86A97"},{"animations":[{"additive":false,"timeOffset":0,"beginTime":0,"from":{"scalar":false},"repeatCount":0,"fillMode":"both","duration":0.001,"autoreverses":false,"property":"hidden","to":{"scalar":true},"removedOnCompletion":false}],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"B7D9DDB759E932D533D9D40242EE25B0"}]}]},"effects":[],"duration":0.001,"type":"transition","attributes":{"direction":0},"name":"none","objectID":"0"}],"automaticPlay":false,"hyperlinks":[],"accessibility":[{"text":"Ian Tyler Applebaum • Instructor • Temple University CIS","targetRectangle":{"y":626.2970122396946,"x":5.0006940960884094,"width":265.8300000000001,"height":11.709930419921875}},{"text":"1280px-Temple_University_Logo.svg.png","targetRectangle":{"y":606.9375,"x":4.1303524971008301,"width":58.023105621337891,"height":16.047016143798828}},{"text":"Let’s use the Strategy Pattern to create a system that calculates discounts in a shopping cart. Different discount algorithms (e.g., percentage discount, fixed amount discount, or no discount) will act as interchangeable strategies. The steps follow your instructions.","targetRectangle":{"y":33.538916409015656,"x":48.061446249485016,"width":227.26599999999996,"height":543.27200317382812}}],"baseLayer":{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"objectID":"0","layers":[{"animations":[],"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,-0.0012860049477591178,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":251658240,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"layers":[{"animations":[],"layers":[],"texturedRectangle":{"isBackgroundTexture":false,"singleTextureOpacity":1,"textureType":0,"textBaseline":0,"textXHeight":0,"isVerticalText":false},"initialState":{"affineTransform":[1,0,0,1,0,0],"masksToBounds":false,"rotation":0,"scale":1,"position":{"pointX":147,"pointY":325.5},"width":294,"sublayerTransform":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"contentsRect":{"y":0,"x":0,"width":1,"height":1},"opacity":1,"edgeAntialiasingMask":0,"height":651,"hidden":false,"anchorPoint":{"pointX":0.5,"pointY":0.5}},"texture":"B7D9DDB759E932D533D9D40242EE25B0"}]}]}}]}
Loading

0 comments on commit ccb57c4

Please sign in to comment.