Science Fair Projects Ideas - Double chance function

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Double chance function

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 more optimal 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.

For those that read C++ easier than English, one typical implementation would be:

class CBaseGfxAPI {
virtual void DrawPoint(int x, int y) { /* 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 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 virtualisation 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

Last updated: 05-09-2005 17:07:53
10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice