Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Flyweight pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == === C# === In this example, every instance of the <code>MyObject</code> class uses a <code>Pointer</code> class to provide data. <syntaxhighlight lang="csharp"> // Defines Flyweight object that repeats itself. public class Flyweight { public string Name { get; set; } public string Location { get; set; } public string Website { get; set; } public byte[] Logo { get; set; } } public static class Pointer { public static readonly Flyweight Company = new Flyweight {"ABC","XYZ","www.example.com"}; } public class MyObject { public string Name { get; set; } public string Company => Pointer.Company.Name; } </syntaxhighlight> === C++ === The C++ [[Standard Template Library]] provides several containers that allow unique objects to be mapped to a key. The use of containers helps further reduce memory usage by removing the need for temporary objects to be created. <syntaxhighlight lang="c++">#include <iostream> #include <map> #include <string> // Instances of Tenant will be the Flyweights class Tenant { public: Tenant(const std::string& name = "") : m_name(name) {} std::string name() const { return m_name; } private: std::string m_name; }; // Registry acts as a factory and cache for Tenant flyweight objects class Registry { public: Registry() : tenants() {} Tenant& findByName(const std::string& name) { if (!tenants.contains(name)) { tenants[name] = Tenant{name}; } return tenants[name]; } private: std::map<std::string, Tenant> tenants; }; // Apartment maps a unique tenant to their room number. class Apartment { public: Apartment() : m_occupants(), m_registry() {} void addOccupant(const std::string& name, int room) { m_occupants[room] = &m_registry.findByName(name); } void tenants() { for (const auto &i : m_occupants) { const int& room = i.first; const auto& tenant = i.second; std::cout << tenant->name() << " occupies room " << room << std::endl; } } private: std::map<int, Tenant*> m_occupants; Registry m_registry; }; int main() { Apartment apartment; apartment.addOccupant("David", 1); apartment.addOccupant("Sarah", 3); apartment.addOccupant("George", 2); apartment.addOccupant("Sarah", 12); apartment.addOccupant("Michael", 10); apartment.tenants(); return 0; } </syntaxhighlight> === PHP === <syntaxhighlight lang="php"><?php class CoffeeFlavour { private static array $CACHE = []; private function __construct(private string $name) {} public static function intern(string $name): self { self::$CACHE[$name] ??= new self($name); return self::$CACHE[$name]; } public static function flavoursInCache(): int { return count(self::$CACHE); } public function __toString(): string { return $this->name; } } class Order { private function __construct( private CoffeeFlavour $flavour, private int $tableNumber ) {} public static function create(string $flavourName, int $tableNumber): self { $flavour = CoffeeFlavour::intern($flavourName); return new self($flavour, $tableNumber); } public function __toString(): string { return "Serving {$this->flavour} to table {$this->tableNumber}"; } } class CoffeeShop { private array $orders = []; public function takeOrder(string $flavour, int $tableNumber) { $this->orders[] = Order::create($flavour, $tableNumber); } public function service() { print(implode(PHP_EOL, $this->orders).PHP_EOL); } } $shop = new CoffeeShop(); $shop->takeOrder("Cappuccino", 2); $shop->takeOrder("Frappe", 1); $shop->takeOrder("Espresso", 1); $shop->takeOrder("Frappe", 897); $shop->takeOrder("Cappuccino", 97); $shop->takeOrder("Frappe", 3); $shop->takeOrder("Espresso", 3); $shop->takeOrder("Cappuccino", 3); $shop->takeOrder("Espresso", 96); $shop->takeOrder("Frappe", 552); $shop->takeOrder("Cappuccino", 121); $shop->takeOrder("Espresso", 121); $shop->service(); print("CoffeeFlavor objects in cache: ".CoffeeFlavour::flavoursInCache().PHP_EOL); </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)