A Simple Class

The Liskov Substitution Principle states that "in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)"

This tutorial will show you how to create an class which will substitute text for the S and T in the example, so the concept is easier to understand, for example, replacing S with "Car" and T with "Vehicle" (as Car could be a subtype of vehicle), it will render the following paragraph:

If Car is a subtype of Vehicle, then objects of type Vehicle may be replaced with objects of type Car (i.e., objects of type Car may substitute objects of type Vehicle) without altering any of the desirable properties of that program (correctness, task performed, etc.)

Writing the code

  1. Create a new OS X Application in Xcode, and choose the 'Command Line Tool' Option
  2. Name the product 'LSP', and ensure the language is set to Swift
  3. Save it somewhere you can remember
  4. Add a new Swift file to your project by choosing File - New - File from the menu bar
  5. Choose Swift file as the type, and name it LiskovPhraseMaker
  6. Add the following code to the new file, to create a class called LiskovPhraseMaker:
    class LiskovPhraseMaker {
        
    }
  7. The class needs two properties which will store the type and subtype. Add the following code inside the class declaration:
    //class properties
    var type = "T"
    var subtype = "S"
    This will create two string variables, with default text of S and T
  8. Add a method inside the class, which will replace the values in the sentence describing the Liskov Substitution Principle with the values from the variables as follows:
    func modifiedLiskovPhrase() -> String{
            
    return "If \(subtype) is a subtype of \(type), then objects of type \(type) may be replaced with objects of type \(subtype) (i.e., objects of type \(subtype) may substitute objects of type \(type)) without altering any of the desirable properties of that program (correctness, task performed, etc.)"
        }
  9. Open the main.swift file, and after the import statement, replace the existing code with that shown below, to make a new instance of the LiskovPhraseMaker class, and call the modifiedLiskovPhrase method:
    var phraseMaker = LiskovPhraseMaker()
    print(phraseMaker.modifiedLiskovPhrase())
  10. Run the application, and you should see the Liskov Substitution Principle output in the console
  11. The idea of the our Liskov phrase maker is to allow us to see it in the context of and actual type and sub type. To do this we need to change the type and subtype variables, before calling the modifiedLiskovPhrase method
  12. Add the following code in between the lines of code you added above
    var phraseMaker = LiskovPhraseMaker()
    phraseMaker.type = "Feline"
    phraseMaker.subtype = "Lion"
    print(phraseMaker.modifiedLiskovPhrase())
  13. Run the application again, and you should see the phrase output but with 'Feline' and 'Lion' as the type and subtype respectively

Improving the application

At present it is possible to set just the type or subtype, and not the other, which could lead to some incorrect phrases, for example if S is a subtype of Feline, in order to avoid this, we could create a method which requires both the type and subtype to be set at the same time, and then make the type and subtype properties private, so they cannot be set separately

  1. Firstly, in the LiskovPhraseMaker class, create a method which allows the type and subtype to be set as follows:
    func set(type:String, subtype:String){
        self.type = type
        self.subtype = subtype
    }
    You will notice that the parameters the method takes (type and subtype) have the same names as the properties we created earlier. In order to differentiate between them in the method body, we prefix the properties with the self keyword
  2. Secondly, add the word private before each of the properties:
    private var type = "T"
    private var subtype = "S"
  3. Finally, open the main.swift file, you will notice that there are now errors on the lines where you have specified the type and subtype (because the properties they refer to are now private). Replace those lines of code with a call to the method you just created:
    phraseMaker.set(type: "Canine", subtype: "Dog")
  4. Run the application and you should see the same result as before, albeit with different types