The complexities of today's materials simulations demand computer codes which are both powerful and highly flexible. A researcher should be able to readily choose different geometries, different materials and different algorithms without having to write low-level code and recompile each time. We describe a molecular dynamics (MD) code, called Digital Material, in which we have sought to maximize flexibility without sacrificing efficiency. Our approach starts from the software engineering concept of Design Patterns and involves dividing the work of an MD simulation into well-defined components. The bulk of this paper is taken up with a detailed description of the different components, their interfaces and implementations and the reasoning behind these. The level of detail is not at the line-by-line level, but at such a level that a reader could implement a similar code sharing the same design principles.