Article Preview
Buy Now
FEATURE
Design Patterns Part 2
The Decorator (Wrapper) Pattern
Issue: 22.1 (January/February 2024)
Author: Marc Zeedar
Author Bio: Marc taught himself programming in high school when he bought his first computer but had no money for software. He's had fun learning ever since.
Article Description: No description available.
Article Length (in bytes): 25,697
Starting Page Number: 20
Article Number: 22103
Resource File(s):
project 22103.zip Updated: 2023-12-31 23:50:47
Related Link(s): None
Excerpt of article text...
Last issue (
xDev 21.6) I began a series on Design Patterns, based on my XDC London 2023 talk last spring. If you're new to this series, I highly recommend beginning with that first part, as I cover the basics of object-oriented programming (OOP), which is crucial for understanding patterns.Remember, design patterns aren't some magical solution you can just apply to your app and make it work better. Design patterns are a
strategy , and basically make OOP better (and force you to use more OOP in your projects). As one example, OOP strives to help youdecouple code —break the links between code to make it more flexible and easier to change and prevent spaghetti code. However, it's easy to still write OO code that's too linked —many Design Patterns will help you decouple code more effectively.The Decorator Pattern
Today we're going to tackle the Decorator pattern. This is sometimes called the Wrapper pattern; I personally like that better. To me Decorator sounds like it changes the
appearance of an item, like how a style sheet in word processor controls a font choice. Really what it does is wrap around an item to make it seem like something else.Why is that useful? Specifically because it lets us create a more generic routine capable of handling many different kinds of objects.
For example, say you've got a printing routine that accepts text. What happens when you want to print a graphic? Now you've got to create a new routine for graphics printing (and maybe ones for different types of graphics, such as vector versus bitmap).
...End of Excerpt. Please purchase the magazine to read the full article.