SoFunction
Updated on 2024-11-19

Python Design Patterns Structural Hedonic Patterns

I. The heng yuan model

Heung Yen, which can be interpreted asPython The metaclasses, minimum granularity classes in the system, when there are a large number of similar objects in the system, the hedonic pattern can be chosen to improve the resource utilization.

Hedonic elements have two states:

Embodied state:The storage is shared within the hentai and does not vary with the environment.
Externally embedded states:is not shareable, it changes as the environment changes, so the externally embedded state is maintained by the client (because changes in the environment are caused by the client).

II. Application scenarios

If an application uses a large number of objects that cause significant storage overhead consider whether you can use the hedonic pattern.

For example, if it is found that a large number of fine-grained instances of a particular object have been generated, and that these instances are essentially the same except for a few parameters, it is possible to drastically increase the number of individual instances through sharing by moving those shared parameters outside of the class, and passing them in on method calls.

III. Code examples

class FlyweightBase:
    """Hedonic base class"""
    def offer(self):
        pass


class Flyweight(FlyweightBase):
    """Shared hedonic categories"""
    def __init__(self, name):
         = name

    def get_price(self, price):
        print('Product Type:{} particulars:{}'.format(, price))


class FactoryFlyweight:
    """Heung Yen Factory Class"""
    def __init__(self):
         = {}

    def Getproduct(self, key):
        if not (key, None):
            [key] = Flyweight(key)
        return [key]


if __name__ == '__main__':
    test = FactoryFlyweight()
    A = ("High-end.")
    A.get_price("Perfume: 80")
    B = ("High-end.")
    B.get_price("Mask: 800")

This article on Python Design Patterns Structural Hedonic Patterns is introduced to this article, more related to the content of the Python Hedonic Patterns, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!