Welcome to the Blog!
This previous week we mainly focused on code structures, such as "Singleton" and "Mediators." This was harder to grasp than pointers, believe it or not. I get them in concept but using them in practice might prove to be a harder beast to tame. Let's get into it.
Quote of the Week:
Yu-gi-oh is like playing Pokémon without the fun.
-Game Club Member.
So we first started with a singleton. This structure mainly focused on making sure there was only one instance of the code running at once or determining how many instances of the code are currently running. I made my own version of their "planets" code. This was to make it easier to understand and give me more experience with the structure. I'll put both in.
My Code:
#include <iostream>
using namespace std;
class instanceObj {
private:
string catName = "Default";
int instanceCounter = 0;
public:
instanceObj(string name);
int increase(int val);
int showCount();
};
instanceObj::instanceObj(string name)
{
catName = name;
cout << "Let's Begin" << endl;
}
int instanceObj::increase(int val)
{
instanceCounter += val;
return 0;
}
int instanceObj::showCount()
{
cout << catName << " Jumped " << instanceCounter << " Times!" << endl;
return instanceCounter;
}
int main() {
instanceObj catJump("Gerald");
catJump.increase(4);
catJump.showCount();
};
Their Code:
class Planet {
private:
static Planet* inst;
Planet(string name) {
cout << "Welcome to " << name << endl;
} // Pre-Constuctor
~Planet() {} //Deconstructor
public :
static Planet* GetInstance(string name);
};
Planet *Planet:: inst = 0;
Planet* Planet::GetInstance(string name)
{
if (inst == 0) {
inst = new Planet(name);
}
return inst;
}
int main() {
Planet *MyPlanet = Planet::GetInstance("Earth");
cout << "MyPlanet address: " << MyPlanet << endl;
Planet* MyPlanet2 = Planet::GetInstance("Uranus");
cout << "MyPlanet address: " << MyPlanet2 << endl;
return 0;
}
Then I proceeded to go through observers and mediators, theses were initially pretty difficult to grasp, but in the end, I found that they're pretty self-explanatory. The observers look into your code and decide if anything is going unexpectedly or if it takes information from a class to put inside another. The mediators act as a backbone, where you make a flowchart esk diagram and make the program and classes from that.
The example they gave was pretty lengthy, in which you were to design a hypothetical car. Due to the vast size of the example, I'll only include the header file. This should give you an idea of how big the system is.
Code:
#ifndef MEDIATOR_H_INCLUDED
#define MEDIATOR_H_INCLUDED
class CarPart;
class Engine;
class Electric;
class Radio;
class SteeringWheel;
class Wheels;
class Brakes;
class Headlights;
class AirConditioner;
class Road;
class Mediator {
public:
Engine* myEngine;
Electric* myElectric;
Radio* myRadio;
SteeringWheel* mySteeringWheel;
Wheels* myWheels;
Brakes* myBrakes;
Headlights* myHeadlights;
AirConditioner* myAirConditioner;
Road* myRoad;
Mediator();
void partChanged(CarPart* part);
};
class CarControls : public Mediator {
public:
void startCar();
void stopCar();
void pushGasPedal(int amount);
void releaseGasPedal(int amount);
void pressBrake(int amount);
void turn(int amount);
void turnOnRadio();
void turnOffRadio();
void adjustRadioVolume(int amount);
void turnOnHeadlights();
void turnOffHeadlights();
void climbHill(int angle);
void descendHill(int angle);
void turnOnAC();
void turnOffAC();
void adjustAC(int amount);
int getSpeed();
CarControls() : Mediator() {}
};
#endif // MEDIATOR_H_INCLUDED
Other than that, nothing code focused really happened. Other than new ideas for robots and tweaking some older projects here and there. Maybe next week, I'll post about one of those.
Until Next Time, Peace!💣
No comments:
Post a Comment