O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the proxy pattern

In this section, we will demonstrate the proxy pattern by creating a house class that we can add multiple floor plans to, where each floor plan represents a different story of the house. Let's begin by creating a FloorPlan protocol:

protocol FloorPlan { 
  var bedRooms: Int { get set } 
  var utilityRooms: Int { get set } 
  var bathRooms: Int { get set } 
  var kitchen: Int { get set } 
  var livingRooms: Int { get set } 
} 

In the FloorPlan protocol, we define five properties that will represent the number of rooms contained in each floor plan. Now, let's create an implementation of the FloorPlan protocol named HouseFloorPlan, which is as follows:

struct HouseFloorPlan: FloorPlan { var bedRooms = 0 var utilityRooms = 0 var ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required