Search code examples
swiftencapsulationmutators

Proper application of encapsulation


I've been researching the concept of encapsulation and found some decent threads about the topic like this one and this one. But I haven't been able to find an answer to a particular question I have. I'll use an example in Swift.

Say you have an object that is of type RoadTrip:

class RoadTrip() {
    private var duration: Double
    private var totalMiles: Double
    private var gallonsOfFuel: Double

    var averageMilesPerGallon: Double
}

Now let's say the app is going to calculate the averageMilesPerGallon which is the only public property:

func calculateAverageMilePerGallon() -> Double {
    let mpg = totalMiles / gallonsOfFuel

    return mpg
}

Should the calculation of average miles per gallon be a private method of the RoadTrip object that executes and updates its averageMilesPerGallon or would it be acceptable to have the calculation performed by another method in a separate utility class that then updates the averageMilesPerGallon property of the RoadTrip object using a mutator method that will set the value?

EDIT: Here's my single class that contains my app's basic calculations. I approached it this way based on what I learned in the Stanford course on iTunes, but I'm beginning to think in my case I should move much of this to my LiftEvent class:

infix operator ^^ { }
func ^^ (radix: Double, power: Double) -> Double {
    return Double(pow(Double(radix), Double(power)))
}

class CalculatorBrain: NSObject {
    var weightLifted: Double?
    var repetitions: Double?
    var oneRepMax: Double?
    let dataManager = LiftEventDataManager()

    func calculateOneRepMax(weightLifted: Double, repetitions: Int ) -> Double {
        let preferredFormulaID = UserDefaultsManager.sharedInstance.preferredFormula!
        let formulas = dataManager.fetchSelectableFormulas()
        let formulaName = formulas[preferredFormulaID].formulaName

        switch formulaName {
        case "Epley":
            oneRepMax = weightLifted * (1 + Double(repetitions)/30.0)
            return oneRepMax!
        case "Baechle":
            oneRepMax = weightLifted * (36/(37 - Double(repetitions)))
            return oneRepMax!
        case "Brzychi":
            oneRepMax = weightLifted * ( 1 + ( 0.033 * Double(repetitions)))
            return oneRepMax!
        case "Lander":
            oneRepMax = 100 * weightLifted / (101.3 - (2.67123 * Double(repetitions)))
            return oneRepMax!
        case "Lombardi":
            oneRepMax = weightLifted * (Double(repetitions) ^^ 0.10)
            return oneRepMax!
        case "Mayhew":
            oneRepMax = 100 * weightLifted / (52.2 + (41.9 * (2.71828 ^^ (-0.055 * Double(repetitions)))))
            return oneRepMax!
        case "O'Conner":
            oneRepMax = weightLifted * (1 + 0.025 * Double(repetitions))
            return oneRepMax!
        default:
            return 0.0
        }
    }


    private func calculatePercentOfWeight(maxWeight: Double, percent: Double) -> Double {
        return maxWeight * percent
    }

    func calculateWeightPercentages(maxWeight: String) -> [Int: Double] {
        let weightPercentages = [1.0, 0.95, 0.90, 0.85, 0.80, 0.75, 0.70, 0.65, 0.60, 0.55, 0.50, 0.45, 0.40, 0.35, 0.30, 0.25]

        var percentages = [Int: Double]()
        for percent in weightPercentages {
            let integerPercent = Int(percent * 100)
            percentages[integerPercent] = calculatePercentOfWeight(Double(maxWeight)!, percent: percent)
        }
        return percentages
    }

    func convertBetweenUnits(fromUnit: Int, toUnit: Int, value: Double) -> Double {
        let units = dataManager.fetchUnits()
        let from = units[fromUnit].conversionRatio as Double
        let to = units[toUnit].conversionRatio as Double
        let result = Double(value * to / from)
        return result
    }
}

Solution

  • I think this is the ideal use-case for a computed property:

    class RoadTrip {
        private let duration: Double
        private let totalMiles: Double
        private let gallonsOfFuel: Double
    
        private var averageMilesPerGallon: Double {
            return totalMiles / gallonsOfFuel
        }
    }