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

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

A C++ Flyweight Pattern for IT Management

  • Print
  • + Share This
Mother Nature likes object-oriented development! C++ programmers often can't afford the luxury of creating a universe of objects; our platforms are intrinsically limited. However, the flyweight design pattern provides an elegant means of sharing objects, with a small price in terms of storage and retrieval. Stephen Morris describes C++ code that uses the flyweight design pattern to solve a knotty problem in networking.
Like this article? We recommend

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:

  1. Create a context object for a generic network link.
  2. Create a real link that uses the link context object.
  3. Supply a range of IP addresses to use in this link context. (The link context is the flyweight object.)
  4. 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);
  "10.81.1.x", 0, 6);
char ipAddressRange[34];
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.

  • + Share This
  • 🔖 Save To Your Account