![]() |
| | #1 |
| Banned Join Date: Aug 2001
Posts: 744
| Cprog tutorial: Design Patterns The book design patterns explains them pretty well I think. I've only used a few patterns in real life and reading the book is a bit boring I think. so instead learning and writing the whole thing myself (I don't have the know-how and experience to do so) I was wondering if anyone else will join me on this tutorial. I sugest the tutorial writers only explain th most important parts of the pattern. If it isn't sufficient for you and you want to learn more about the patterns, you should read the book. Practical issues: *If you write a tutorial about a pattern, fill in the title as follows: "pattern: Singleton" *If you want to add something to a pattern tutorial that someone else has written fill in the title as follows: "addition: singleton" *if you have a question fill in the title follows: "singleton: my question title" *when you answer a question, use the quote button and fill in the tile as the other person had . Maybe remove some text from the quotes. There are about 23 design patterns, so if 23 persons each write one piece of it, we'll have a nice tutorial which will be better written then any book. Introdution Why use desing patterns? design patterns are solutions or tools to solve common problems. There are 3 types of design paterns: Creational: concern the process of object creation
__________________ SVG is the future |
| maes is offline | |
| | #2 |
| Banned Join Date: Aug 2001
Posts: 744
| pattern: Singleton Where to use it: You can use it whenever you need only one instance of a class. But like all things too much isn't good. It can be alot of work to change your code when you decide to have more then one instance of your class in a later stage of development. So be sure you'll only need one instance. Creation: Make your constructor private. You create a static method (preferably called: instance) and this method will call your private constructor. this instance will keep a static member of your class and returns this member whenever it is called. header file: Code: class mySingleton
{
private:Code: mySingleton::mySingleton() : data_(0)
{}
mySingleton* mySingleton::instance()
{example This is the code for a simple logging mechanism : log.h Code: #include <string>
#include <fstream>
using namespace std;
class log
{
private:
log(void);
string filename_;
ofstream filestream_;
public:
~log(void);
static log* instance(void);
void initialize(string filename);
ofstream& debug_log();
};
Code: #include "log.h"
log::log(void) : filename_("")
{
}
log::~log(void)
{
}
log* log::instance(void)
{
static log instance;
return &instance;
}
void log::initialize(string filename)
{
filename_=filename;
filestream_.open(filename.c_str());
}
ofstream& log::debug_log()
{
return filestream_;
}
Code: #include "log.h"
void myfunction(void)
{
log::instance()->debug_log()<<"In function"<<endl;
}
int main()
{
log::instance()->initialize("mylog.txt");
log::instance()->debug_log()<<"Before function"<<endl;
myfunction();
log::instance()->debug_log()<<"After function"<<endl;
return 0;
}
__________________ SVG is the future Last edited by maes; 10-11-2004 at 02:18 AM. |
| maes is offline | |
| | #3 | |
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| Quote:
I don't agree.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling | |
| Sang-drax is offline | |
| | #4 | |
| Banned Join Date: Aug 2001
Posts: 744
| Quote:
instead of writing log::instance()->debug_log()<<"my text"<<endl; you write LOGSTREAM<<"my text"<<endl; Are macro's evil? when you do calculations in macro's, I would agree that's something I wouldn't do.
__________________ SVG is the future | |
| maes is offline | |
| | #5 | ||
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| Quote:
Because you could at least do this, which would be 10^6 times better: Code: std::ostream& LOGSTREAM = log::instance()->debug_log();
int main()
{
LOGSTREAM<<"my text"<<endl;
}
Quote:
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling | ||
| Sang-drax is offline | |
| | #6 | |
| Banned Join Date: Aug 2001
Posts: 744
| Quote:
__________________ SVG is the future | |
| maes is offline | |
| | #7 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| |
| bithub is offline | |
| | #8 | |
| Banned Join Date: Aug 2001
Posts: 744
| Quote:
Now, who wants to get this tutorial finished with me ?
__________________ SVG is the future | |
| maes is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Design patterns in C | sashaKap | C Programming | 2 | 04-26-2009 08:32 AM |
| Design Books | Wraithan | General Discussions | 28 | 09-12-2007 10:51 PM |
| Implementing Inheritence into your design | bobthebullet990 | C++ Programming | 6 | 08-05-2006 04:40 PM |
| My new website | joeprogrammer | General Discussions | 19 | 03-17-2006 07:38 PM |
| My DirectInput tutorial.... | jdinger | A Brief History of Cprogramming.com | 1 | 06-18-2002 11:32 PM |