In one sentence

Support a large number of fine-grained objects efficiently with sharing techniques.

demand

Now I have to develop websites for several clients, one for showcasing products, some for blogs. How do you design it? Don’t apply for a cloud server for every customer?

An example of the share mode

static func main(a) {
    var extrinsicState = 22

    let f = FlyweightFactory()

    extrinsicState - = 1
    let fx = f.getFlyweight(key: "X")
    fx?.operation(extrinsicState: extrinsicState)

    extrinsicState - = 1
    let fy = f.getFlyweight(key: "Y")
    fy?.operation(extrinsicState: extrinsicState)

    extrinsicState - = 1
    let fz = f.getFlyweight(key: "Z")
    fz?.operation(extrinsicState: extrinsicState)

    extrinsicState - = 1
    let uf = UnsharedConcreteFlyweight()
    uf.operation(extrinsicState: extrinsicState)
}

protocol Flyweight {
    func operation(extrinsicState: Int)
}

class ConcreteFlyweight: Flyweight {
    func operation(extrinsicState: Int) {
        print("Specific Flyweight:\(extrinsicState)")}}class UnsharedConcreteFlyweight: Flyweight {
    func operation(extrinsicState: Int) {
        print("Do not share specific flyweights:\(extrinsicState)")}}class FlyweightFactory {
    var dic: [String: Flyweight] = [:]

    init(a) {
        dic["X"] = ConcreteFlyweight()
        dic["Y"] = ConcreteFlyweight()
        dic["Z"] = ConcreteFlyweight()}func getFlyweight(key: String) -> Flyweight? {
        dic[key]
    }
}
Copy the code

Code V1.0

With the yuan model to achieve the requirements of the site, so that two sites can be instances, to meet the needs of 4 people.

static func main(a) {
    let f = WebSiteFactory(a)let fx = f.getWebSiteCategary(key: "Product Demonstration")
    fx.use(user: User(name: "Little X"))

    let fy = f.getWebSiteCategary(key: "Product Demonstration")
    fy.use(user: User(name: "Y"))

    let fz = f.getWebSiteCategary(key: "Product Demonstration")
    fz.use(user: User(name: "Z"))

    let fl = f.getWebSiteCategary(key: "Blog")
    fl.use(user: User(name: "L"))

    print("The total number of website categories is:\(f.getWebSiteCount())")}protocol WebSite {
    func use(user: User)
}

class User {
    let name: String
    init(name: String) {
        self.name = name
    }
}

class ConcreteWebSite: WebSite {
    let name: String
    init(name: String) {
        self.name = name
    }

    func use(user: User) {
        print("Website category:\(name), users:\(user.name)")}}class WebSiteFactory {
    var flyweights: [String: WebSite] = [:]

    func getWebSiteCategary(key: String) -> WebSite {
        if flyweights[key] = = nil {
            flyweights[key] = ConcreteWebSite(name: key)
        }
        return flyweights[key] ?? ConcreteWebSite(name: key)
    }

    func getWebSiteCount(a) -> Int {
        flyweights.values.count
    }
}
Copy the code

advantages

You can avoid the overhead of a large number of acacia class instances, call the common parts that do not change easily internal state, and call the differences such as different accounts external state.