Challenge: Solution Review
Explore how to apply the command pattern to separate request execution from invocation in JavaScript. Understand the roles of commands, receiver, and invoker by reviewing a BankAccount example with operations like withdraw, deposit, and check amount, improving your grasp of behavioral design patterns.
We'll cover the following...
Solution #
Explanation #
Let’s start by looking at the original code first:
In the example, there is a BankAccount class which contains the following functions:
-
checkAmount: returns theamountin the account -
withdrawMoney: withdraws an amount -
depositAmount: deposits an amount
An account object will be able to call on these functions directly. With the command pattern, we will change that. This means the object executing the function will be separated from the one requesting. As mentioned in the question, the command ...