What is a wrapper? What is a Decorator?

Started by
11 comments, last by Tangletail 8 years, 8 months ago
Because wrapper is already mentioned so many times. I'll just try my best to define decorator.

A decorator is a specialized bit of code that is used to add functionality to ANY modular bit of code without the need to know about it.

I think it's term came from thinking of it like a cake.

So... lets say you have the base of a choclate cake. On it's own it's not very interesting. And you want to add a new flavor to it. Well that would normally require you to rebake the cake.

What you can instead do is you can decorate the chocolate cake with strawberry flavored icing.

The end result is you taste the strawberry icing and the cake.

You can use decorators for a lot of things. In game design, it's mostly seen in AI development, or any sort of logic using trees.
Advertisement
Thanks Tangletail! Nice and easy analogy. And it goes perfectly with my analogy for programming as a whole (a program as a recipe). I am teaching this to kids, and I need to make it very simple.

I guess a wrapper doesn't pretty much the same thing, and a decorator is just a better way to implement it.

They call me the Tutorial Doctor.

Mm... well the two are different things. And not neccessairly better than everything else.

As mentioned before a Wrapper makes a common grounds for different similar APIs. Or an API from a completely different language.

Lets just think of the wrapper like candy.

What if you have a magic candy wrapper that automatically changes the flavor of the candy to something you want? If you say I want Caramel, then the wrapper morphs the candy underneath to caramel. If you want chocolate, then the candy is chocolate. This is how the programmer's wrapper do.

So instead of having to write something like this...

D3DSYSTEM.Draw() For Directx Systems
and
OGLSYSTEM.Draw() for Open GL systems

all over your code with #if preprocessors all over the place... you can just you have a wrapper that contains the mess, and gives you what you want at compile time.

RenderSystem.Draw() This will automatically handle the guess work for Rendering API.

It's general purpose is to keep things neat.

To Recap.

Decorator: Adds Functionality. Add flavor to everything.
Wrapper: Branches Functionality. Pick your flavor without guesswork.

You can use this to give you flavors from different but similar systems.

This topic is closed to new replies.

Advertisement