In
software engineering
Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
, a double-chance function is a software
design pattern
A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
with a strong application in
cross-platform
Within computing, cross-platform software (also called multi-platform software, platform-agnostic software, or platform-independent software) is computer software that is designed to work in several Computing platform, computing platforms. Some ...
and scalable development.
Examples
Computer graphics
Consider a graphics
API
An application programming interface (API) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build ...
with functions to
DrawPoint
,
DrawLine
, and
DrawSquare
. It is easy to see that
DrawLine
can be implemented solely in terms of
DrawPoint
, and
DrawSquare
can in turn be implemented through four calls to
DrawLine
. If you were porting this API to a new architecture you would have a choice: implement three different functions natively (taking more time to implement, but likely resulting in faster code), or write
DrawPoint
natively, and implement the others as described above using common, cross-platform, code. An important example of this approach is the
X11
The X Window System (X11, or simply X) is a windowing system for bitmap displays, common on Unix-like operating systems.
X originated as part of Project Athena at Massachusetts Institute of Technology (MIT) in 1984. The X protocol has been at ...
graphics system, which can be ported to new graphics hardware by providing a very small number of device-dependent primitives, leaving higher level functions to a hardware-independent layer.
[Susan Angebranndt, Raymond Drewry, Philip Karlton, Todd Newman, "Strategies for Porting the X v11 Sample Server", Mit 1988.]
The double-chance function is an optimal method of creating such an implementation, whereby the first draft of the port can use the "fast to market, slow to run" version with a common
DrawPoint
function, while later versions can be modified as "slow to market, fast to run". Where the double-chance pattern scores high is that the base API includes the self-supporting implementation given here as part of the null driver, and all other implementations are extensions of this. Consequently, the first port is, in fact, the first usable implementation.
One typical implementation in
C++ could be:
class CBaseGfxAPI ;
class COriginalGfxAPI : public CBaseGfxAPI ;
class CNewGfxAPI : public CBaseGfxAPI ;
Note that the
CBaseGfxAPI::DrawPoint
function is never used, per se, as any graphics call goes through one of its derived classes. So a call to
CNewGfxAPI::DrawSquare
would have its first chance to render a square by the
CNewGfxAPI
class. If no native implementation exists, then the base class is called, at which point the
virtualization
In computing, virtualization (abbreviated v12n) is a series of technologies that allows dividing of physical computing resources into a series of virtual machines, operating systems, processes or containers.
Virtualization began in the 1960s wit ...
takes over and means that
CNewGfxAPI::DrawLine
is called. This gives the
CNewGfxAPI
class a “second chance” to use
native code
In computer programming, machine code is computer program, computer code consisting of machine language instruction set architecture, instructions, which are used to control a computer's central processing unit (CPU). For conventional binary ...
, if any is available.
With this method it is, theoretically, possible to build an entire 3D engine (applying
software
Software consists of computer programs that instruct the Execution (computing), execution of a computer. Software also includes design documents and specifications.
The history of software is closely tied to the development of digital comput ...
rasterizing) using only one native function in the form of DrawPoint, with other functions being implemented as and when time permits. In practice this would be hopelessly slow, but it does demonstrate the possibilities for double-chance functions.
References
* {{cite book , author=Goodwin, Steven , title=Cross-Platform Game Programming , publisher=Charles River Media, year=2005 , isbn=1-58450-379-3
Software design patterns
Articles with example C++ code