advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Five years ago, design patterns were the hottest buzzword in the software development world. Today it seems that programmers are less enthusiastic about them. What's your experience with design patterns? Are they a fad or a solid framework that has improved software development time and quality?
Partners & Affiliates
advertisement
advertisement
advertisement
Average Rating: 2.6/5 | Rate this item | 11 users have rated this item.
 
Share Data Among Objects Using the Monostate Design Pattern
When you share data among users via a file, you put down the welcome mat for versioning problems, or worse, a security breach. Solve this problem using a simple design pattern. 

advertisement
ertain applications need to share data among different users or among distributed instances of the same application. Using a file to store the shared data isn't an ideal solution due to security and data integrity reasons. This month I will suggest another solution based on the monostate design pattern.


How can you ensure that all users of the same application access shared data safely?


Use the monostate design pattern to ensure safe and synchronized data sharing.

Step 1: Design
Imagine a large department store that has different departments for multimedia, clothes, and cosmetics. Such stores often enable tourists to pay in their own currencies. How can you ensure that every department has access to the same exchange rate table? Using a shared physical file might cause security and data-integrity problems. For example, the filename and its path might change; worse yet, users might access an outdated version of the file. Besides, this is an implementation detail that should be hidden from end users.

A preferable solution is to encapsulate the exchange rates in a class. You want to ensure that every user and application module gets a private object instance of the class in question while ensuring that these objects always share the same state. Furthermore, any change in the state of the class should propagate to other objects automatically and immediately. This is exactly what the monostate design pattern provides.

A monostate class is a class that implements the monostate design pattern. A monostate class contains only static data members and member functions. This way, all of its object instances share the same state.

Step 2: Implementation
We will create a monostate class, called Exchange, whose static data members represent the exchange rates of a various currencies, say Euros per $1 U.S., etc.:

class Exchange
{
private:
static double Euro; // Euros per $1 U.S.
static double GBP; // British pounds per $1 U.S.
static double Yen;  // Yens per $1 U.S. 
//...
};
The definitions of the static members outside the class also initialize them:

double Exchange::Euro=0.8630; // Euros per $1 U.S.
double Exchange::GBP=0.6017; // British Pounds per $1 U.S.
double Exchange::Yen= 119.34; // Yens per $1 U.S.
In the real world you'd probably use a function that reads the current exchange rates from a remote server or a database to initialize the data. For example:

double Exchange::Euro=
  trade_server.get_euro_rate(now); // Euros per $1 U.S.
//other currencies
 
The class also contains member functions that exchange these currencies to and from U.S. dollars:

class Exchange
{
//
public:
 double Euro_to_USD(double euros) const { return euros/Euro;}
 double US_to_Euro(double us_dollars) const { return us_dollars*Euro;}
 double GBP_to_USD(double pounds) const { return pounds/GBP;}
 double USD_to_GBP(double us_dollars) const { return us_dollars*GBP;}
// 
};


  Next Page: Usage
Page 1: DesignPage 2: Usage
Untitled
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES