Some Popular Object-Oriented Design Patterns
- Builder
- Object Pool
- Singleton (a special type of Object Pool)
- Decorator
Some Popular Functional Design Patterns
- Event
- Recursion
- Pipes and Filters
- Bridge
Many of these design patterns are simply more popular in one paradigm over another. For example Pipes and Filters and Bridge can be used in OOP, while Decorator can be used in functional programming. These are just some examples. While many design patterns can be used in multiple paradigms it may be simpler to implement a design pattern a certain way that involves a specific paradigm over another. Recursion is however an example of a multi-paradigm design pattern that is generally implemented in the same fashion in both paradigms.
Since I have been studying design patterns, I have often asked myself why don't schools teach design patterns first. And I remember that I used to hate programming theory, not understanding the purpose behind learning about how a bunch of really old guys' programming patterns would help me to learn how to program. But now more than ever I understand that programming theory (like design patterns) is a foundation for computer science. It includes proofs, mathematics, discrete math, computer architecture and organization, O-notation, and design patterns. One very simple and basic design pattern, and yet one of the most important and highly used design patterns, is the Interface design pattern. If you are using C# or Java then interfaces are part of the programming language. Recently I dug a little deeper into what interfaces really were.
The simple definition of an interface (generically in computer science) is:
A contract defining that any class that implements an interface must implement all the method definitions given in the interface. (The Code Project - The Interface Construct in C#)
As I said C# and Java define the Interface Pattern explicitly. Here are some example interfaces in C# and Java (they should look very similar):
Given the enumerated type WeaponType:
(Language-independent)
- Code: Select all
WeaponType = Firearm | Melee | Missile;
(Java)
- Code: Select all
package com.nathandelane.interfaces;
public interface IWeapon
{
String getName();
double getWeight();
WeaponType getWeaponType();
long getDamageIncurance();
}
(C#)
- Code: Select all
public namespace Nathandelane.Interfaces
{
public interface IWeapon
{
string GetName();
double GetWeight();
WeaponType GetWeaponType();
long GetDamageIncurance();
}
}
(Note that methods of an interface in C# and Java are inherently public and it is a syntax error to include an access modifier in a method declaration in an interface.)
In these languages and some modern dynamic languages like Ruby interfaces are trivial, and simple to implement. Once I realized that, I wondered how one does it in C++. I read a lot of information and learned that at some point Microsoft added a __interface keyword that can be used similarly to the C# and Java variants, but that wasn't exactly what I wanted, so I consulted with Bjarne Stroustrup, the inventor of C++ to see what he said, and I learned that an interface is really just a completely abstract or (in C++) virtual class. The class itself is not what is virtual but all of its members are. They must also be public. Anyway this is the basic result:
- Code: Select all
#include <string>
class IWeapon
{
public:
virtual std::string GetName() = 0; // The '= 0' makes this a pure virtual function.
virtual double GetWeight() = 0;
virtual WeaponType GetWeaponType() = 0;
virtual long GetDamageIncurance() = 0;
};
This pattern is more similar to a abstract class in high-level strongly-typed languages like C# and Java. An abstract class is also, by definition, and interface, however an abstract class may define some methods and leave other methods virtual or abstract. Abstract classes may also define a constructor and fields, though typically the constructor or constructors are protected a little more by giving it protected access. Here are some examples in Java and C#:
(Java)
- Code: Select all
package com.nathandelane.interfaces;
abstract class AbstractWeapon
{
private String name;
private double weight;
private WeaponType type;
private long damageIncurance;
protected AbstractWeapon(String name, double weight, WeaponType type, long damageIncurance)
{
this.name = name;
this.weight = weight;
this.type = type;
this.damageIncurance = damageIncurance;
}
abstract String getName(); // Note that these methods can also have a body.
abstract double getWeight();
abstract WeaponType getWeaponType();
abstract long getDamageIncurance();
}
(C#)
- Code: Select all
public namespace Nathandelane.Interfaces
{
public abstract class AbstractWeapon
{
private String _name;
private double _weight;
private WeaponType _type;
private long _damageIncurance;
protected AbstractWeapon(String name, double weight, WeaponType type, long damageIncurance)
{
_name = name;
_weight = weight;
_type = type;
_damageIncurance = damageIncurance;
}
public virtual String getName(); // Now this method doesn't need to be overridden because it is virtual.
public abstract double getWeight(); // Note that these methods can also have a body.
public abstract WeaponType getWeaponType();
public abstract long getDamageIncurance();
}
So there are a couple of different rules in C#. It has both virtual and abstract keywords. A virtual function and an abstract function are both by definition virtual or abstract, but the virtual keyword makes it so that the implementing class is not required to override the method. An abstract method must be overridden by an implementing class. Also both abstract (and virtual methods in C#) may have a body in an abstract class in C# or Java. Sometimes you will see that the default body throws an exception, such as a new NotImplementedException, in case somebody tries to use the method before it is implemented.
Anyway, besides practicing programming now, I am suggesting more and more understanding why, when, and how to correctly implement design patterns. If anybody wants to pick my brain a little more on design patterns or any of the other ideas I mentioned in this post, feel free to contact me. I should be more available these days.
Thanks.
Nathandelane

