
thanks to comments here
keep classes open to extension but closed to changes
Decorator adds responsibility to an object dynamically. Also provides a flexible alternative to the extension to extend the functionality
class CombatGroup
{public:
CombatGroup ();
virtualGetComponents List ();
getGroupPower virtual int ();
/ / ...
}
AssaultGroup class: public CombatGroup
{private: int
_initialPower;
public: virtual int
getGroupPower ();
virtualGetComponents List ();
//...
}
int AssaultGroup: getGroupPower () {return
_initialPower;}
GroupDecorator class: public CombatGroup
{private:
CombatGroup _group;
public:
GroupDecorator (CombatGroup *);
virtualGetComponents List ();
getGruopPower virtual int ();
//...
}
ListGroupDecorator::getComponents() {
_group.getComponents();
}
int GroupDecorator::getGroupPower() {
_group.getGroupPower();
}
class SoldierDecorator : public GroupDecorator {
private :
// unit rappresenta the drive that goes to join the group. This class is a container
/ / for it
Unit _unit;
int _power;
public:
SoldierDecorator (CombatGroup *);
getGroupPower virtual int ();
virstualGetComponents List ();}
/ / IMPLEMENTATION NOTE! Int
SoldierDecorator: getGroupPower () {return
this._power _group.getGroupPower + ();}
/ / create a group CO 2 soldiers, an engineer and a doctor
CombatGroup aGroup AssaultGroup = new ();
aGroup = new SoldierDecorator (aGroup)
aGroup = new SoldierDecorator (aGroup)
aGroup = new EngineerDecorator (aGroup)
aGroup = new MedicDecorator (aGroup)
aGroup.getGroupPower ();
#include "QApplication"
#include "QPushButton"
int main(int argc, char * argv[])
{
QApplication app(argc, argv);
QPushButton * b = new QPushButton("Ciao Mondo");
b->show();
return app.exec();
}
#include "wx/wx.h"
class HelloWorld : public wxApp {
virtual bool OnInit();
};
IMPLEMENT_APP(HelloWorld)
bool HelloWorld::OnInit()
{wxFrame * f = new wxFrame (NULL, -1, WXT ("Hello world"));
f-> Show (true);
return true;}
classManToSave
{public static final int MAX_LIFE = 100;
private Healer healer;
private int life;
public int getLife () {return
life;
} public void setLife (int l) {
life = l;}
hurt public void (int damage) {
life -= damage;
if (life \u0026lt;20) {
healer.heal (this )
}} {Healer class
Public void
heal (ManToSave mts) {
mts.setLife (ManToSave.MAX_LIFE)
}}
....
if(life < 30) {
healer.update(this);
enemy.update(life);
missionNode.update(life);
}
...
The Observer pattern defines a relazione uno-a-molti tra oggetti in modo che quando un oggetto cambia stato, tutti gli oggetti che dipendono da esso siano notificati e aggiornati automaticamente.
/
* * Subject: I prefer an abstract class just because an interface so I can define methods *
one time, without needing to overload in subclasses. In fact in a java interface would be much more
* indicated, since it allows freedom of inheritance
* / abstract class Subject {
LinkedList \u0026lt;Observer> Observers;
protected Subject () {
Observers = new LinkedList \u0026lt; Observers> ();}
public void attachObs (Observer obs) {
observers.add (obs);}
public void detachObs (Observer obs) {
observers.remove (obs);}
public void notify() {
Iterator<Observer> i = observers.iterator();
while(i.hasNext()) {
i.next().update(this);
}
}
}
/*
* Observer
*/
interface Observer {
public void update(Subject);
}
/*
* Implementazione dell'uomo da salvare
*/
class ManToSave extends Subject {
public static final int MAX_LIFE = 100;
int life;
ManToSave() {
life = MAX_LIFE;
}
public void hurt(int damage) {
this.life -= damage;
if (life \u0026lt;30)
/ / notify observers
notifyAll ();}
public getLife () {return
this.life;}
} / * * Example
observer
* / Enemy
class implements Observer {private int
moral
{
moral = 50;}
public void update (Subject s) {
ManToSave mts = (ManToSave) s;
if (mts.getLife () \u0026lt;20)
moral = 100;}
}
Prefer composition to inheritance
Strategy ha lo scopo di definire a family of algorithms, encapsulate it and make them interchangeable. Strategy lets algorithms vary independently from clients and use them.
Program to interfaces not to implementations
Marine Marine m = new ();
m.shoot ();
Soldier soldier = new Marine();
soldier.shoot();
s = getSoldier();
s.shoot();
public class Soldier {
ShootBehavior shootBehavior;
performShoot public void () {
shootBehavior.shoot ();}
}
Marine extends public class Soldier {public
Marine () {
shootBehavior ShootWithGun = new ();
healBehavior NoHeal = new ();}
public void display
{/ / Prints the marine
}}
public interface ShootBehavior
{public void shoot ();}
public class ShootWithGun
{public void shoot () {
/ / firing the gun
}} public class
ShootWithBazooka
{public void shoot () {
/ / firing the bazooka
}}
public void setShootBehavior (ShootBehavior sb) {
this . ShootBehavior = sb;}
Identify aspects of your application that vary and separate from those that do not vary
OpenGL Data Type | internal representation | equivalent C |
---|---|---|
GLbyte | 8-bit signed char integer | |
GLshort | 16-bit integer | short |
GLint | 32-bit integer | long |
GLfloat | 32-bit floating point | float |
GLdouble | 64 bit floating point | double |
GLbyte | 8 bit integer | signed char |
GLubyte, GLBoolean | 8 bit unsigned integer | unsinged char |
GLushort | 16 bit unsigned integer | unsigned short |
GLuint, GLenum, GLbitfield | 32 bit unsigned char | unsigned long |
GLchar, GLsizeiptr | 8 bit charachter | char |
glColor3f ([....])
glColor4f ([...]);
glColor4d ([....] )
glColor3i ([....]);
CONTACT MSN: ilcuozzo@hotmail.it
LISTED IN THE CUOZZOLOGI!:
Tony Fist, VannyJohn, Jardagorn, Emanuele Waro
RESEARCHERS:
Selphie
APPRENTICES:
Ale (8), Amber (7.8), Charlotte (7), Cyrus (7), Dafide (8.2), Dafide (8.2), Danito (8.3), Emilio (7.5), Francesco FDN (7.5), Hellkite (6.9), Lelluccio neomelodico (8.3), Mirko90 (8.3), Mi®ko (9.5), Morè (7.2), Nando (honorem), Ni7ro (7.6), Reginelf (8.1), Sarny (8), SimGiusi (7.8), Susy (7.3), Yeti77 (7.6), Tato (7.3), Tribillina (honorem), Valerio (6.6), Hoodjunior (8.7), riccia mary riccia (honorem), Paradox (8.2), margie(7,5)