Skip to content

Instantly share code, notes, and snippets.

@codedeman
Created August 12, 2019 08:43
Show Gist options
  • Save codedeman/ec5253c75e17af1128a154ee7455358a to your computer and use it in GitHub Desktop.
Save codedeman/ec5253c75e17af1128a154ee7455358a to your computer and use it in GitHub Desktop.
The Prototype Pattern
class Location{
var name:String = ""
var address:String = ""
init(name:String,address:String) {
self.name = name
self.address = address
}
}
class Appointment3:NSObject,NSCopying {
var name:String = ""
var day:String = ""
var place:Location;
init(name:String,day:String,place:Location) {
self.name = name
self.day = day
self.place = place
}
func copy(with zone: NSZone? = nil) -> Any {
return Appointment3(name: self.name, day: self.day, place: self.place)
}
func printDetails(label:String) {
print("\(label) with \(name) on \(day) at \(place.name), "
+ "\(place.address)");
}
}
var beerMeeting = Appointment3(name: "Bob", day: "Mon",
place: Location(name:"Joe's Bar", address: "123 Main St"));
var workMeeting = beerMeeting.copy() as! Appointment3;
workMeeting.name = "Alice";
workMeeting.day = "Fri";
workMeeting.place.name = "Conference Rm 2"; workMeeting.place.address = "Company HQ";
beerMeeting.printDetails(label: "Social");
workMeeting.printDetails(label: "Work");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment