An e-commerce app is not complete as it only shows the products without giving the possibility of buying them.
In this section, we'll implement the cart using the same technique presented in the previous section to wrap the call to a server.
Let's start by defining what a cart should do, as follows:
Adding a product
Removing a product
Buying the cart
Given these commands, we define the gateway in this way:
import Foundation protocol CartGateway { func addProductID(productID: String) func removeProductID(productID: String) func buy() }
The local implementation is basically an empty implementation of the protocol:
class LocalCartGateway: CartGateway { func addProductID(productID: String){ } func removeProductID(productID: String){ } func buy() { } }
The CartStore
class is nothing more than a wrapper around a dictionary that sends the command to the gateway:
class CartStore { private var products = [String:Product...