Lear design patterns with Kendi — part 2 structural patterns

This article is a continuation of design patterns learning. This time we’ll explore the structural patterns. If you’ve missed the first part about the creational patterns, we encourage you to read this article as well:

As previously, we’re going to start from recognizing the problem and we’ll try to find a solution.

Kendi as a model ;-)

Case no. 1 — We’ve got a simple object. We want to dynamically add new states and tasks to it. How to achieve it without class overriding or class inheritance? The solution is the Decorator pattern.

Use case: Together with Kendi we set up an online shop, in which we sell clothes for pets. Our clients can design clothes for their pets. They choose the standard product and put some decorations on it:

Case no. 2 — We want to use existing class, but its interface doesn’t match to that one we need. How to achieve compatible interface without class overwriting? The solution is the Adapter pattern.

Use case: The user made and save a design of cloth for its pet. Struct Design contains a lot of information needed to order, but it doesn’t contains the whole needed information.

Case no. 3 — We want to make proxy, that will pretend the real object till the time the real object is really needed. The solution is the Proxy pattern.

Use case: We want to optimize loading of the order’s preview. At the entrance, the user sees the order details such as the order id and the list of ordered products (the item in the list contains only the type and size). All details of the ordered product are available after transition to its preview. We don’t want to load the real products and related operations (such as creating a photo from product url) on the order entry.

To sum up:

  • the structural patterns compose classes and objects into the larger structures.
  • The Decorator is the object wrapper. Using this pattern let you avoid class overloading with all possible tasks that the object might do. According to the principle “pay only for that what you use” the decorator adds the concrete task to the object in the right time. The disadvantage of the decorator pattern is creating many small objects, that differ only connection, not class or variables, which make the diagnostic harder.
  • Adapter allows you to perform tasks that are not supported by the custom class. This allows classes with incompatible interfaces to work together. There are two types of adapters: class and object. Class adapters cannot adapt subclasses of an adapted class, while an object adapter will work for a subclasses.
  • The proxy replaces the real object until we really need it. There are remote, virtual, and security proxies. The remote proxy passes encoded requests to real objects in another address space. A virtual proxy can store additional information about the real object. The security proxy checks if the sender of the request has access to it.
  • There are other structural patterns not mentioned in this article: Facade, Composite, Bridge and Flyweight.