Tags

  • AWS (7)
  • Apigee (3)
  • ArchLinux (5)
  • Array (6)
  • Backtracking (6)
  • BinarySearch (6)
  • C++ (19)
  • CI&CD (3)
  • Calculus (2)
  • DesignPattern (43)
  • DisasterRecovery (1)
  • Docker (8)
  • DynamicProgramming (20)
  • FileSystem (11)
  • Frontend (2)
  • FunctionalProgramming (1)
  • GCP (1)
  • Gentoo (6)
  • Git (15)
  • Golang (1)
  • Graph (10)
  • GraphQL (1)
  • Hardware (1)
  • Hash (1)
  • Kafka (1)
  • LinkedList (13)
  • Linux (27)
  • Lodash (2)
  • MacOS (3)
  • Makefile (1)
  • Map (5)
  • MathHistory (1)
  • MySQL (21)
  • Neovim (10)
  • Network (66)
  • Nginx (6)
  • Node.js (33)
  • OpenGL (6)
  • PriorityQueue (1)
  • ProgrammingLanguage (9)
  • Python (10)
  • RealAnalysis (20)
  • Recursion (3)
  • Redis (1)
  • RegularExpression (1)
  • Ruby (19)
  • SQLite (1)
  • Sentry (3)
  • Set (4)
  • Shell (3)
  • SoftwareEngineering (12)
  • Sorting (2)
  • Stack (4)
  • String (2)
  • SystemDesign (13)
  • Terraform (2)
  • Tree (24)
  • Trie (2)
  • TwoPointers (16)
  • TypeScript (3)
  • Ubuntu (4)
  • Home

    [Structural] Flyweight

    Published May 07, 2022 [  DesignPattern  ]

    Real world example

    Did you ever have fresh tea from some stall? They often make more than one cup that you demanded and save the rest for any other customer so to save the resources e.g. gas etc. Flyweight pattern is all about that i.e. sharing.

    In plain words

    It is used to minimize memory usage or computational expenses by sharing as much as possible with similar objects.

    Wikipedia says

    In computer programming, flyweight is a software design pattern. A flyweight is an object that minimizes memory use by sharing as much data as possible with other similar objects; it is a way to use objects in large numbers when a simple repeated representation would use an unacceptable amount of memory.

    Programmatic example

    Translating our tea example from above. First of all we have tea types and tea maker

    // Anything that will be cached is fly weight
    // Types of tea here will be flyweights
    class KaraTea {
    
    }
    
    // acts as a factory and saves the tea
    class TeaMaker {
        protected availableTea = [];
    
        make(preference) {
            if(!this.availableTea[preference]) {
                this.availableTea[preference] = new KaraTea()
            }
            return this.availableTea[preference]
        }
    }
    

    Then we have the TeaShop which takes orders and serves them

    class TeaShop {
        protected orders;
        protected teaMaker;
    
        constructor(teaMaker: TeaMaker) {
            this.teaMaker = teaMaker;
        }
    
        takeOrder(teaType: string, table: number){
            this.orders[table] = this.teaMaker.make(teaType)
        }
    
        serve(){
            this.orders.forEach((table, tea) => {
                console.log(`Serving team to table #{table}`)
            })
        }
    
    }
    

    And it can be used as below

    const teaMaker = new TeaMaker()
    const shop = new TeaShop(teaMaker)
    
    shop.takeOrder('less sugar', 1)
    shop.takeOrder('more milk', 2)
    shop.takeOrder('no sugar', 5)
    
    shop.serve()
    

    References