Design patterns

Posted on 25 Şubat 2014 in İnternet, Programlama by

patternGüzel açıklamaların olduğu ve aynı zamanda farklı programlama dillerinde örnek kaynak kodlarını içeren güzel bir site.

http://sourcemaking.com/design_patterns

Behavioral design patterns

State Pattern

http://www.javacodegeeks.com/2013/08/state-design-pattern-in-java-example-tutorial.html

Observer pattern written in c++ by Kadir Gönül
For java version follow>>
#include <iostream>
#include <list>
using namespace std;

class observer_b {
public:
	virtual void notify() = 0;
	virtual ~observer_b() {}
};

class observer : public observer_b {
private:
	string name;
public:
	observer(string n) {
		this->name = n;
	}
	void notify() {
		std::cout<<this->name<<" : something changed"<<std::endl;
	}
};

class observer_special : public observer_b {
private:
	string name;
public:
	observer_special(string n) {
		this->name = n;
	}
	void notify() {
		std::cout<<this->name<<" : something changed for special object"<<std::endl;
	}
};

class subject {
private:
	list <observer_b *> L;
public:
	void registerObserver (observer_b *o);
	void notifyObserver ();
};

void subject::registerObserver(observer_b *o)
{
	L.insert(L.end()++, o);
}

void subject::notifyObserver ()
{
	list<observer_b *>::iterator i;
	for (i = L.begin(); i != L.end(); i++) {
		(*i)->notify();
	}
}

int main() {
	subject *s = new subject;
	// Create observers
	observer_b *o1 = new observer("observer1");
	observer_b *o2 = new observer("observer2");
	observer_b *o3 = new observer_special("observer3");
	// Register observers
	s->registerObserver(o1);
	s->registerObserver(o2);
	s->registerObserver(o3);
	// Notify subject so that it can notify registered objects
	s->notifyObserver();
	return 0;
}

Please give us your valuable comment

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

This site uses Akismet to reduce spam. Learn how your comment data is processed.