background preloader

Design Patterns

Facebook Twitter

The Static Gateway Pattern | Jean-Paul S. Boodhoo. Now I am by no means claiming to be any sort of patterns naming authority, but after you see the same occurent pattern in your applications you often will try to formulate some sort of vocabulary among the team to express the concept that you see occurring over and over again. This is the very nature of how patterns popped up in the first place. This is a pet name that I have come up with for the “recurring” theme you will see demonstrated here. If you are not familiar with the Gateway pattern check out the definition here. I am going to demonstrate this pattern with an example that most people should have in place at the beginning of a project. Logging. One of the things about Logging, is that it should be simple. Public class Calculator { private ILog log; public Calculator(ILog log) { this.log = log; } public int Add(int number1,int number2) { log.InformationalMessage("About to add two numbers"); return number1 + number2; } }

Duck typing. When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.[1] In duck typing, a programmer is only concerned with ensuring that objects behave as demanded of them in a given context, rather than ensuring that they are of a specific type. For example, in a non-duck-typed language, one would create a function that requires that the object passed into it be of type Duck, in order to ensure that that function can then use the object's walk and quack methods. In a duck-typed language, the function would take an object of any type and simply call its walk and quack methods, producing a run-time error if they are not defined. Instead of specifying types formally, duck typing practices rely on documentation, clear code, and testing to ensure correct use. Concept examples[edit] Consider the following pseudo-code for a duck-typed language: 9 [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] apples and oranges, apples and oranges, apples and oranges, In C#[edit]

Design Principles from Design Patterns. Leading-Edge JavaDesign Principles from Design PatternsA Conversation with Erich Gamma, Part IIIby Bill VennersJune 6, 2005 Page 1 of 4 >> Summary In this interview, Erich Gamma, co-author of the landmark book, Design Patterns, talks with Bill Venners about two design principles: program to an interface, not an implementation, and favor object composition over class inheritance.

Erich Gamma lept onto the software world stage in 1995 as co-author of the best-selling book Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995) [1]. This landmark work, often referred to as the Gang of Four (GoF) book, cataloged 23 specific solutions to common design problems. In 1998, he teamed up with Kent Beck to produce JUnit [2], the de facto unit testing tool in the Java community. On October 27, 2004, Bill Venners met with Erich Gamma at the OOPSLA conference in Vancouver, Canada. Bill Venners: Why? Bill Venners: How's that? Erich Gamma: Thousands of years, right? Game Object Structure: Inheritance vs. Aggregation. Game Object Structure: Inheritance vs.

Aggregation By Kyle Wilson Wednesday, July 03, 2002 Every game engine I've encountered has had some notion of a game object, or a scene object, or an entity. A game object may be an animated creature, or an invisible box that sets off a trigger when entered, or an invisible hardpoint to which a missile attaches. At Interactive Magic, and later at HeadSpin/Cyan, I worked with inheritance-based game objects. There are several problems with a game object design based on inheritance. The Diamond of Death. So if an inheritance-based game object design is riddled with problems, what is a better approach? In a post to Sweng-Gamedev back in early 2001[1] -- around the time Cyan started rearchitecting Plasma -- Scott Bilas of Gas Powered Games described changing the Dungeon Siege engine from a "static class hierarchy" to a "component based" design.

From these data points, I'm willing to interpolate a trend. Virtual Inheritance in C++, and solving the diamond problem. By Andrei Milea Multiple inheritance in C++ is a powerful, but tricky tool, that often leads to problems if not used carefully. This article will teach you how to use virtual inheritance to solve some of these common problems programmers run into. If you're not familiar with multiple inheritance, check out this article on multiple inheritance. The diamond problem One of the problems that arises due to multiple inheritance is the diamond problem. Since both transmitter and receiver classes are using the method write() from the base class, when calling the method write() from a radio object the call is ambiguous; the compiler can't know which implementation of write() to use, the one from the transmitter class or the one from the receiver class.

To understand how this is works, let's take a look at how the objects are represented in memory. Fortunately, C++ allows us to solve this problem by using virtual inheritance. Memory Layout in Virtual Inheritance Constructors and Virtual Inheritance. Null Object pattern. In object-oriented computer programming, a Null Object is an object with defined neutral ("null") behavior. The Null Object design pattern describes the uses of such objects and their behavior (or lack thereof). It was first published in the Pattern Languages of Program Design book series.[1] Motivation[edit] The Objective-C language takes another approach to this problem and does not invoke methods on nil but instead returns nil for all such invocations.

Description[edit] Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty. For example, a function may retrieve a list of files in a folder and perform some action on each. By returning a null object (i.e. an empty list) instead, there is no need to verify that the return value is in fact a list.

Example[edit] Given a binary tree, with this node structure: class node { node left node right } C++[edit] Separation of concerns. The value of separation of concerns is simplifying development and maintenance of computer programs. When concerns are well-separated, individual sections can be reused, as well as developed and updated independently. Of special value is the ability to later improve or modify one section of code without having to know the details of other sections, and without having to make corresponding changes to those sections. Implementation[edit] Separation of concerns is an important design principle in many other areas as well, such as urban planning, architecture and information design.[5] The goal is to more effectively understand, design, and manage complex interdependent systems, so that functions can be reused, optimized independently of other functions, and insulated from the potential failure of other functions.

Origin[edit] The term separation of concerns was probably coined by Edsger W. Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. Chain-of-responsibility pattern. In a variation of the standard chain-of-responsibility model, some handlers may act as dispatchers, capable of sending commands out in a variety of directions, forming a tree of responsibility. In some cases, this can occur recursively, with processing objects calling higher-up processing objects with commands that attempt to solve some smaller part of the problem; in this case recursion continues until the command is processed, or the entire tree has been explored.

An XML interpreter might work in this manner. This pattern promotes the idea of loose coupling, which is considered a programming best practice. Example[edit] The following code illustrates the pattern with the example of a logging class. Each logging handler acts as the processing object. Also, note that in a 'pure' implementation of the chain-of-responsibility pattern, a processing object may not pass responsibility further down the chain after handling a message.

Java[edit] C#[edit] Another Java example[edit] See also[edit] Open/closed principle. In object-oriented programming, the open/closed principle states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification";[1] that is, such an entity can allow its behaviour to be modified without altering its source code.

This is especially valuable in a production environment, where changes to source code may necessitate code reviews, unit tests, and other such procedures to qualify it for use in a product: code obeying the principle doesn't change when it is extended, and therefore needs no such effort. Meyer's open/closed principle[edit] Meyer's definition advocates implementation inheritance. Implementation can be reused through inheritance but interface specifications need not be. The existing implementation is closed to modifications, and new implementations need not implement the existing interface. Polymorphic open/closed principle[edit] Robert C. See also[edit] References[edit] External links[edit] Single responsibility principle. In object-oriented programming, the single responsibility principle states that every context (class, function, variable, etc.) should define a single responsibility, and that responsibility should be entirely encapsulated by the context.

All its services should be narrowly aligned with that responsibility. Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to change. As an example, consider a module that compiles and prints a report. Such a module can be changed for two reasons. First, the content of the report can change. The reason it is important to keep a class focused on a single concern is that it makes the class more robust. The responsibility is defined as a charge assigned to a unique actor to signify its accountabilities concerning a unique business task.[4] References[edit] Patterns in Practice: The Open Closed Principle. Patterns in Practice The Open Closed Principle Jeremy Miller This is the first installment of a new MSDN® Magazine column on software design fundamentals.

My marching orders are to discuss design patterns and principles in a manner that isn't bound to a specific tool or lifecycle methodology. In other words, my plan is to talk about the bedrock knowledge that can lead you to better designs in any technology or project. I'd like to start with a discussion of the Open Closed Principle and other related ideas popularized by Robert C. Martin in his book, Agile Software Development, Principles, Patterns, and Practices. Ask yourself this: how many times do you start writing a brand new application from nothing versus the number of times you start by adding new functionality to an existing codebase?

Then ask yourself another question: is it easier to write all new code or to make changes to existing code? It sounds like a contradiction in terms, but it's not. Single Responsibility Principle.