Opdracht 4 code en design
Co-authored-by: Aryan Lala (19097727) <A.E.Lala@student.hhs.nl>
This commit is contained in:
		
							
								
								
									
										20
									
								
								Opdracht_4/inc/DieselMotor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Opdracht_4/inc/DieselMotor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#include "DieselMotor.h"
 | 
			
		||||
#include "Tsensor.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
DieselMotor::DieselMotor(Tsensor* t): t1(t){
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Tsensor* DieselMotor::tsensor(){
 | 
			
		||||
    return t1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DieselMotor::start(){
 | 
			
		||||
    std::cout <<"Diesel motor has started"<<std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void DieselMotor::stop(){
 | 
			
		||||
    std::cout <<"Diesel motor has stopped"<<std::endl;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								Opdracht_4/inc/DieselMotor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Opdracht_4/inc/DieselMotor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#ifndef __DieselMotor_H
 | 
			
		||||
#define __DieselMotor_H
 | 
			
		||||
 | 
			
		||||
#include "Mone.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DieselMotor : public Mone{
 | 
			
		||||
private:
 | 
			
		||||
    Tsensor* t1;
 | 
			
		||||
public:
 | 
			
		||||
    DieselMotor(Tsensor*);
 | 
			
		||||
    virtual void start();
 | 
			
		||||
    virtual void stop();
 | 
			
		||||
    virtual Tsensor* tsensor();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										20
									
								
								Opdracht_4/inc/ElectrischeMotor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								Opdracht_4/inc/ElectrischeMotor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#include "ElectrischeMotor.h"
 | 
			
		||||
#include "Tsensor.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
ElectrischeMotor::ElectrischeMotor(Tsensor* t): t1(t){
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Tsensor* ElectrischeMotor::tsensor(){
 | 
			
		||||
    return t1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ElectrischeMotor::start(){
 | 
			
		||||
    std::cout <<"Elektrische motor has started"<<std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void ElectrischeMotor::stop(){
 | 
			
		||||
    std::cout <<"Elektrische motor has stopped"<<std::endl;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								Opdracht_4/inc/ElectrischeMotor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Opdracht_4/inc/ElectrischeMotor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
#ifndef __ElectrischeMotor_H
 | 
			
		||||
#define __ElectrischeMotor_H
 | 
			
		||||
 | 
			
		||||
#include "Mtwo.h"
 | 
			
		||||
 | 
			
		||||
class ElectrischeMotor : public Mtwo{
 | 
			
		||||
private:
 | 
			
		||||
    Tsensor* t1;
 | 
			
		||||
public:
 | 
			
		||||
    ElectrischeMotor(Tsensor*);
 | 
			
		||||
    virtual void start();
 | 
			
		||||
    virtual void stop();
 | 
			
		||||
    virtual Tsensor* tsensor();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										1
									
								
								Opdracht_4/inc/Machine.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Opdracht_4/inc/Machine.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
#include "Machine.h"
 | 
			
		||||
							
								
								
									
										19
									
								
								Opdracht_4/inc/Machine.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Opdracht_4/inc/Machine.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#ifndef __Machine_H
 | 
			
		||||
#define __Machine_H
 | 
			
		||||
 | 
			
		||||
class Motor;
 | 
			
		||||
class Mone;
 | 
			
		||||
class Mtwo;
 | 
			
		||||
 | 
			
		||||
class Machine {
 | 
			
		||||
    private:
 | 
			
		||||
        Motor* m1;
 | 
			
		||||
        Motor* m2;
 | 
			
		||||
    public:
 | 
			
		||||
        virtual Motor* motor1() = 0;
 | 
			
		||||
        virtual Motor* motor2() = 0;
 | 
			
		||||
        virtual void run() = 0;
 | 
			
		||||
        virtual void halt() = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										28
									
								
								Opdracht_4/inc/MachineImpl.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								Opdracht_4/inc/MachineImpl.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#include "MachineImpl.h"
 | 
			
		||||
#include "DieselMotor.h"
 | 
			
		||||
#include "ElectrischeMotor.h"
 | 
			
		||||
#include "TsensorImpl.h"
 | 
			
		||||
 | 
			
		||||
MachineImpl::MachineImpl(Motor* m1, Motor* m2): m1(m1), m2(m2){};
 | 
			
		||||
 | 
			
		||||
void MachineImpl::run(){
 | 
			
		||||
    t1 = m1->tsensor();
 | 
			
		||||
    t1->read();
 | 
			
		||||
    m1->start();
 | 
			
		||||
    t2 = m2->tsensor();
 | 
			
		||||
    t2->read();
 | 
			
		||||
    m2->start();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MachineImpl::halt(){
 | 
			
		||||
    m1->stop();
 | 
			
		||||
    m2->stop();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Motor* MachineImpl::motor1(){
 | 
			
		||||
    return m1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Motor* MachineImpl::motor2(){
 | 
			
		||||
    return m2;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								Opdracht_4/inc/MachineImpl.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Opdracht_4/inc/MachineImpl.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
#ifndef __MachioneImpl_H
 | 
			
		||||
#define __MachineImpl_H
 | 
			
		||||
 | 
			
		||||
#include "Machine.h"
 | 
			
		||||
 | 
			
		||||
class Tsensor;
 | 
			
		||||
 | 
			
		||||
class MachineImpl : public Machine 
 | 
			
		||||
{
 | 
			
		||||
    private:
 | 
			
		||||
        Tsensor* t1;
 | 
			
		||||
        Tsensor* t2;
 | 
			
		||||
        Motor* m1;
 | 
			
		||||
        Motor* m2;
 | 
			
		||||
    public:
 | 
			
		||||
        MachineImpl(Motor*,Motor*);
 | 
			
		||||
        ~MachineImpl(){};
 | 
			
		||||
        virtual Motor* motor1();
 | 
			
		||||
        virtual Motor* motor2();
 | 
			
		||||
        virtual void run();
 | 
			
		||||
        virtual void halt();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										10
									
								
								Opdracht_4/inc/Mone.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Opdracht_4/inc/Mone.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
#ifndef __Mone_H
 | 
			
		||||
#define __Mone_H
 | 
			
		||||
 | 
			
		||||
#include "Motor.h"
 | 
			
		||||
 | 
			
		||||
class Mone : public Motor{
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										14
									
								
								Opdracht_4/inc/Motor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Opdracht_4/inc/Motor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
#ifndef __Motor_H
 | 
			
		||||
#define __Motor_H
 | 
			
		||||
 | 
			
		||||
class Tsensor;
 | 
			
		||||
 | 
			
		||||
class Motor{
 | 
			
		||||
public:
 | 
			
		||||
    virtual ~Motor() {};
 | 
			
		||||
    virtual Tsensor* tsensor() = 0;
 | 
			
		||||
    virtual void start() = 0;
 | 
			
		||||
    virtual void stop() = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										10
									
								
								Opdracht_4/inc/Mtwo.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Opdracht_4/inc/Mtwo.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
#ifndef __Mtwo_H
 | 
			
		||||
#define __Mtwo_H
 | 
			
		||||
 | 
			
		||||
#include "Motor.h"
 | 
			
		||||
 | 
			
		||||
class Mtwo : public Motor{
 | 
			
		||||
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										13
									
								
								Opdracht_4/inc/Observer.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Opdracht_4/inc/Observer.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
#include "Observer.h"
 | 
			
		||||
 | 
			
		||||
void Subject::notify ( ){
 | 
			
		||||
for ( list<Observer*>::iterator i=L.begin( ); i!=L.end( ); ++i ) (*i)->update( );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Observer::Observer ( Subject* s ) : S(s){
 | 
			
		||||
getSubject( )->insert(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Observer::~Observer ( ){
 | 
			
		||||
getSubject( )->remove ( this );
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								Opdracht_4/inc/Observer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								Opdracht_4/inc/Observer.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#ifndef __Observer_H
 | 
			
		||||
#define __Observer_H
 | 
			
		||||
 | 
			
		||||
#include <list>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
class Subject;
 | 
			
		||||
 | 
			
		||||
class Observer{
 | 
			
		||||
    private: Subject* S;
 | 
			
		||||
    protected: Subject* getSubject ( ) const { return S; }
 | 
			
		||||
    public: Observer (Subject* s);
 | 
			
		||||
        virtual ~Observer ( );
 | 
			
		||||
        virtual void update ( ) = 0;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Subject{
 | 
			
		||||
    private: list<Observer*> L;
 | 
			
		||||
    protected: virtual void notify ( );
 | 
			
		||||
    public: Subject ( ) { }
 | 
			
		||||
        virtual ~Subject ( ) { }
 | 
			
		||||
        virtual void insert (Observer* s) { L.push_front(s); }
 | 
			
		||||
        virtual void remove (Observer* s) { L.remove(s); }
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										1
									
								
								Opdracht_4/inc/Tsensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Opdracht_4/inc/Tsensor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
#include "Tsensor.h"
 | 
			
		||||
							
								
								
									
										10
									
								
								Opdracht_4/inc/Tsensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Opdracht_4/inc/Tsensor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
#ifndef __Sensor_H
 | 
			
		||||
#define __Sensor_H
 | 
			
		||||
 | 
			
		||||
class Tsensor{
 | 
			
		||||
public:
 | 
			
		||||
    virtual ~Tsensor(){}; 
 | 
			
		||||
    virtual void read() = 0; 
 | 
			
		||||
    virtual int temperature() = 0;
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										17
									
								
								Opdracht_4/inc/TsensorImpl.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Opdracht_4/inc/TsensorImpl.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#include "TsensorImpl.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
TsensorImpl::TsensorImpl(): temperatuur(0){
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TsensorImpl::read(){
 | 
			
		||||
    std::cout << "Voer een temperatuur in: ";
 | 
			
		||||
    std::cin >> temperatuur;
 | 
			
		||||
    std::cout << endl;
 | 
			
		||||
    notify();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int TsensorImpl::temperature(){
 | 
			
		||||
    return temperatuur;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										17
									
								
								Opdracht_4/inc/TsensorImpl.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Opdracht_4/inc/TsensorImpl.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#ifndef __TsensorImpl_H
 | 
			
		||||
#define __TsensorImpl_H
 | 
			
		||||
 | 
			
		||||
#include "Tsensor.h"
 | 
			
		||||
#include "Observer.h"
 | 
			
		||||
 | 
			
		||||
class TsensorImpl : public Tsensor, public Subject{
 | 
			
		||||
    private:
 | 
			
		||||
        int temperatuur;
 | 
			
		||||
    public:
 | 
			
		||||
        TsensorImpl();
 | 
			
		||||
        virtual ~TsensorImpl(){};
 | 
			
		||||
        virtual void read();
 | 
			
		||||
        virtual int temperature();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										10
									
								
								Opdracht_4/inc/UI.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								Opdracht_4/inc/UI.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
#include "UI.h"
 | 
			
		||||
#include "TsensorImpl.h"
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
UI::UI(TsensorImpl* t) : Observer(t){}
 | 
			
		||||
 | 
			
		||||
void UI::update() {cout << dynamic_cast<TsensorImpl*>(getSubject())->temperature() << endl;}
 | 
			
		||||
							
								
								
									
										17
									
								
								Opdracht_4/inc/UI.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Opdracht_4/inc/UI.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#ifndef __UI_H
 | 
			
		||||
#define __UI_H
 | 
			
		||||
 | 
			
		||||
class TsensorImpl;
 | 
			
		||||
 | 
			
		||||
#include "Observer.h"
 | 
			
		||||
 | 
			
		||||
class UI : public Observer{
 | 
			
		||||
private:
 | 
			
		||||
    TsensorImpl* t;
 | 
			
		||||
public:
 | 
			
		||||
    UI(TsensorImpl*);
 | 
			
		||||
    virtual ~UI(){};
 | 
			
		||||
    virtual void update();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Reference in New Issue
	
	Block a user