- Protecting Valuable Resources
- Interfaces: Action-Packed Entities
- Configuring IP Addresses
- Solution Introduction and the Flyweight Pattern
- A Generic Network Link
- A Generic Network Link Context
- A Real Network Link
- The GenericLink Implementation
- The GenericLinkContext Implementation
- The RealLink Implementation
- Bringing the Code Together
- Additional Reading
Protecting Valuable Resources
Nature is inherently object-oriented. We use computer software to try to model the real world. However, finite processing and storage resources limit our relatively primitive schemes for object-oriented systems. We don’t usually have the luxury of modeling systems as huge lists of interacting objects. The present generation of platforms simply can’t cope—so we need an approximation.
Fortunately, the flyweight design pattern provides just such an approximation. In this article, I’ll discuss a knotty little problem in networking where there is a need for a great many objects: IP numbering of device interfaces. Think about your own PC. Typically a machine has one or more network interfaces, each of which probably has an IP address. If your setup is static, you’ll normally set the IP addresses up once and then forget about them, or you may use dynamically assigned IP addresses.
Now imagine running a network with thousands of IP-capable devices. In large networks, the number of interfaces grows to enormous numbers. Many management systems use scripts to number the interfaces—and renumber them, when required. Problems arise when the script author leaves the organization and later a renumbering exercise is required. The flyweight pattern can help in such situations.
Even though modern platforms appear to have plenty of resources, we can never allow our code to be profligate in its use of these resources. If we encounter algorithmic challenges that suggest a need for excessive numbers of objects, the flyweight design pattern can come to the rescue.
Listing 1 shows a sneak preview of the finished code, which does the following:
- Create a context object for a generic network link.
- Create a real link that uses the link context object.
- Supply a range of IP addresses to use in this link context. (The link context is the flyweight object.)
- Retrieve and print the IP addresses assigned to the network link.
Listing 1 Implementation of the flyweight pattern.
GenericLinkContext* aLinkContext = new GenericLinkContext(); RealLink* aLink = new RealLink("MPLS", "NodeA", "NodeB", "ifA", "ifB", aLinkContext); aLinkContext->SetAddressRange(aLink->mySlotNumber, "10.81.1.x", 0, 6); char ipAddressRange; aLink->GetAllIPAddresses(ipAddressRange); printf ("For aLink, IP Address range is %s\n", ipAddressRange);
Don’t worry about the details for the moment; we’ll cover them soon. The important point is that we’ve separated the complex IP address details from the way we model the network links. Rather than lumping these entities together into large and unwieldy objects, we now have two distinct objects that help to divide and reduce the complexity.
Let’s take a brief detour into the networking domain to see why this is an important problem.