Cprog tutorial: Design Patterns

This is a discussion on Cprog tutorial: Design Patterns within the C++ Programming forums, part of the General Programming Boards category; Incognito asked a question on how to improve your programming skills. Personaly I think design patterns is a good way ...

  1. #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

  2. #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;
    }
    Last edited by maes; 10-11-2004 at 03:18 AM.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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

  4. #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.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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

  6. #6
    Banned maes's Avatar
    Join Date
    Aug 2001
    Posts
    744
    Quote Originally Posted by Sang-drax
    Yes.
    why?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265

  8. #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 ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design patterns in C
    By sashaKap in forum C Programming
    Replies: 2
    Last Post: 04-26-2009, 09:32 AM
  2. Design Books
    By Wraithan in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 09-12-2007, 11:51 PM
  3. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 05:40 PM
  4. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-19-2002, 12:32 AM

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