Double-chance function

Double-chance function

In software engineering, a double-chance function is a software design pattern with a strong application in cross-platform and scalable development. It is easiest to explain it with an example.

Consider a graphics API 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, but ultimately providing a better solution), or write DrawPoint natively, and implement the others as described above using common, cross-platform, code.

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 {
     virtual void DrawPoint(int x, int y) = 0; /* Abstract concept for the null driver */
     virtual void DrawLine(int x1, int y1, int x2, int y2) { /* DrawPoint() repeated */}
     virtual void DrawSquare(int x1, int y1, int x2, int y2) { /* DrawLine() repeated */}
 };
 
 class COriginalGfxAPI : public CBaseGfxAPI {
     virtual void DrawPoint(int x, int y) { /* The only necessary native calls */ }
     virtual void DrawLine(int x1, int y1, int x2, int y2) { /* If this function exists a native DrawLine
                                                                routine will be used. Otherwise the base
                                                                implementation is run. */}
 };
 
 class CNewGfxAPI : public CBaseGfxAPI {
     virtual void DrawPoint(int x, int y) { /* The only necessary for native calls */ }
 };

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 takes over and means that CNewGfxAPI::DrawLine is called. This gives the CNewGfxAPI class a “second chance” to use native code, if any is available.

With this method it is, theoretically, possible to build an entire 3D engine (applying software rasterizing) using only one native function in the form of DrawPoint, with other functions being implemented as and when time permits. In practise this would be hopelessly slow, but it does demonstrate the possibilities for double-chance functions.

References

  • Goodwin, Steven (2005). Cross-Platform Game Programming. Charles River Media. ISBN 1-58450-379-3. 

Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Chance Meeting on a Dissecting Table of a Sewing Machine and an Umbrella — Studio album by Nurse With Wound Released …   Wikipedia

  • Double Arts — Infobox animanga/Header name = Double Arts caption = ja name = ダブルアーツ ja name trans = Daburu Ātsu genre = Action, FantasyInfobox animanga/Manga title = author = Naoshi Komi publisher = flagicon|Japan Shueisha demographic = Shōnen magazine =… …   Wikipedia

  • Double bass — Contrabass redirects here. For other uses, see Contrabass (disambiguation). Not to be confused with Acoustic bass guitar. For the technique used in percussion, see Double bass drum. Double Bass Side and front views of a modern double bass with a… …   Wikipedia

  • Wave function — Not to be confused with the related concept of the Wave equation Some trajectories of a harmonic oscillator (a ball attached to a spring) in classical mechanics (A B) and quantum mechanics (C H). In quantum mechanics (C H), the ball has a wave… …   Wikipedia

  • Last Chance Saloon — was a popular name of a type of bar in the United States which began to appear in the 19th Century as an early expression of border economics. Saloons situated near areas where alcohol was not easily obtainable frequently took the name as a… …   Wikipedia

  • Application programming interface — API redirects here. For other uses, see API (disambiguation). An application programming interface (API) is a source code based specification intended to be used as an interface by software components to communicate with each other. An API may… …   Wikipedia

  • Software design pattern — In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a… …   Wikipedia

  • Audition Online — Developer(s) T3 Entertainment Publisher(s) YD Online (Was called YeDang Online in the pas …   Wikipedia

  • North Melbourne Football Club — North Melbourne Names Full name North Melbourne Football Club Ltd[1] Nickname(s) Kangaroos, Shinboners …   Wikipedia

  • Universe — For other uses, see Universe (disambiguation). Physical cosmology …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”