User:Xvedejas/Design Patterns

XVedejas' Guide to Design Patterns
This is a guide to using

Composite
The composite pattern lets us treat an object and a group of these objects as the same thing. This is extremely handy when you want groupings of objects or even groupings of groupings. As an example, we will pretend we are writing a game where you can control army units. The most basic unit (the "component") is a single soldier. Yet what we want to do is to be able to treat a whole group of soldiers as if we were commanding just one. So we will create a soldier interface class called ISoldier that basically lists all the things a soldier (or group of soldiers) might need to do.

Now let's create a soldier class with the appropriate methods.

Simple enough, right? So if we wanted to control this soldier, we could tell it to attack, retreat, or do nothing. But an army has many, many soldiers and we might want to issue all of them a command at once, as a group. You could write a 'for' loop each and every time you wanted to call multiple soldiers, but that doesn't retain information about the group. It is a much wiser decision to abstract this by creating a new class.

Since we want to treat the group as if it were a single soldier, we will also inherit from ISoldier. This will require us to independently redefine all the functions that ISoldier gives us.