C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2004, 05:00 AM   #1
Banned
 
maes's Avatar
 
Join Date: Aug 2001
Posts: 744
Cprog tutorial: Design Patterns

Incognito asked a question on how to improve your programming skills. Personaly I think design patterns is a good way to start.
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
  • Builder
  • Factory Method
  • Prototype
  • Singleton
Stuctural: deal with composition of classes an objects
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy
Behavioural: characterize the ways in which objects or classes interact and distribute responsability
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor
__________________
SVG is the future
maes is offline   Reply With Quote
Old 10-10-2004, 05:43 AM   #2
Banned
 
maes's Avatar
 
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:
mySingleton(void); int data_;
public:
static mySingleton* instance(void); int data(); void data(int data);
}
cpp file:
Code:
mySingleton::mySingleton() : data_(0)
{}
mySingleton* mySingleton::instance()
{
static mySingleton inst; return &inst;
} int mySingleton::data() {
return data_;
} void mySingleton::data(int data) {
data_= data;
}
Now you'll be able to use the getters and setters of data_ in every class/function and data() will always return the same value.

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();
};
cpp file
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_;
}
Now I only have to initialize the class once and I'll be able to write to the log file whenever I want without haveing a global variable or opening the logfile in every function/class.
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   Reply With Quote
Old 10-10-2004, 06:02 AM   #3
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Göteborg, Sweden
Posts: 2,038
Quote:
Originally Posted by maes
Now I only have to initialize the class once and I'll be able to write to the log file whenever I want without haveing a global variable or opening the logfile in every function/class.
So, you prefer a global macro over a global variable?
I don't agree.
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline   Reply With Quote
Old 10-10-2004, 06:10 AM   #4
Banned
 
maes's Avatar
 
Join Date: Aug 2001
Posts: 744
Quote:
Originally Posted by Sang-drax
So, you prefer a global macro over a global variable?
I don't agree.
Why not, it's just for readabilty?

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   Reply With Quote
Old 10-10-2004, 06:39 AM   #5
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Göteborg, Sweden
Posts: 2,038
Quote:
Originally Posted by maes
Why not, it's just for readabilty?
Why not?
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:
Originally Posted by maes
Are macro's evil?
Yes.
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline   Reply With Quote
Old 10-10-2004, 02:13 PM   #6
Banned
 
maes's Avatar
 
Join Date: Aug 2001
Posts: 744
Quote:
Originally Posted by Sang-drax
Yes.
why?
__________________
SVG is the future
maes is offline   Reply With Quote
Old 10-10-2004, 03:56 PM   #7
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
So what's wrong with using macros?
bithub is offline   Reply With Quote
Old 10-11-2004, 01:41 AM   #8
Banned
 
maes's Avatar
 
Join Date: Aug 2001
Posts: 744
Quote:
Originally Posted by bithub
Thanks for the info, changed my code.

Now, who wants to get this tutorial finished with me ?
__________________
SVG is the future
maes is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:18 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22