Skip to content

Instantly share code, notes, and snippets.

@PetreVane
Last active January 24, 2020 17:17
Show Gist options
  • Save PetreVane/fd5d6119c8675c1f87cc5a85d761523a to your computer and use it in GitHub Desktop.
Save PetreVane/fd5d6119c8675c1f87cc5a85d761523a to your computer and use it in GitHub Desktop.
The Flyweight Pattern is a structural design pattern that minimizes memory usage and processing. The flyweight pattern provides a shared instance that allows other instances to be created too. Every reference to the class refers to the same underlying instance.
import UIKit
let red = UIColor.red
let red2 = UIColor.red
print(red === red2) // proves this is a flyweight
let color = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
let color2 = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
print(color === color2) // proves this is NOT a flyweight
extension UIColor {
public static var colorStore: [String: UIColor] = [:]
public class func rgba(_ red: CGFloat, _ green: CGFloat, _ blue: CGFloat, _ alpha: CGFloat) -> UIColor {
let key = "\(red)\(green)\(blue)\(alpha)"
// print("Your key is \(key)")
if let color = colorStore[key] {
return color
}
let color = UIColor(red: red,
green: green,
blue: blue,
alpha: alpha)
colorStore[key] = color
return color
}
}
let flyColor = UIColor.rgba(1, 0, 0, 1)
let flyColor2 = UIColor.rgba(1, 45, 6, 1)
print(flyColor === flyColor2) // proves this is a flyweight class method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment