Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
Like this article? We recommend

A Generic Network Link Context

Listing 3 shows a context class that represents the flyweight.

Listing 3 The Flyweight class.


class GenericLinkContext
  explicit GenericLinkContext(void);
  virtual ~GenericLinkContext(void);

  virtual int GetNextFreeSlot();
  virtual void SetNextFreeSlot(int);
  virtual void SetAddressRange(int slot, char* baseAddress, int offset, int range);
  virtual char* GetBaseAddress(int slot);
  virtual int GetAddressOffset(int slot);
  virtual int GetAddressRange(int slot);

  int nextFreeSlot;

  struct AddressRanges
    char* baseRangeAddress;
    int addressOffset;
    int addressRange;
  } AddressRangeArray[MAX_ARRAY_LENGTH];

The private array AddressRangeArray stores the details of an IP address range for a given link. The variable baseRangeAddress holds the first address in the range; for example, 10.81.1.x. The variable addressOffset holds the first number in the required range; for example, if this number is 0, it replaces the x in 10.81.1.x to make it Next, the addressRange variable determines the last IP address in the range. If addressRange = 8, for instance, the full address range is as follows: -

In other words, eight IP addresses (,,,, and so on).

Each entry in the array AddressRangeArray stores the IP address range details for a given link. Each link has an associated slot in this array. So when I want to create a new link with an IP address range, I just get the value of nextFreeSlot by invoking GetNextFreeSlot(). If the value is less than the maximum (10), I’ll be allocating a new address range array slot. Once I have a new slot, I can then supply the IP address range by using the method SetAddressRange().

To see this technique in action, I need a concrete link type.

  • + Share This
  • 🔖 Save To Your Account