Today I have written some C++ code after a long time.
Although i am in constant touch with tech…
These days i mainly get my son do the coding and me as a guru of him.
So, today i am presenting the Builder Pattern.
As the name suggests - it is part of the creational pattern
The idea is that if a class has many member variables - and the final object may not depend all of them - and the number of parametrs that the constructor takes vary - instead of making a lot of overloaded constructors, we use this Builder pattern.
The code footprint is heavily reduced - as these is no need to create a matching number of overloaded constructors to match each and every permutation and combination of the member variables passed in the constructors.
In the example, we have created a common Student class which may represent a 10th std, a 12th std or an engineering student - may be in 1st yr or 2nd year or third year or may be in the final fourth year.
In case of a student who is a qualified engineer, we need to supply all the marks…
But in case of a 2nd year student - we don’t need to bother about third year marks or the fourth year marks.
Here goes the code for the CS students of the UNIVERSE.
/*
* Student.h
*
* Created on: Jan 24, 2023
* Author: som
*/
#ifndef STUDENT_H_
#define STUDENT_H_
#include<iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
string address;
int tenthMarks;
int twelevethMarks;
int FirstYrMarks;
int secondYrMarks;
int thirdYrMarks;
int fourthYrMarks;
public:
class Builder {
public:
string name = "";
string address = "";
int tenthMarks = 0;
int twelevethMarks = 0;;
int FirstYrMarks = 0;
int secondYrMarks = 0;
int thirdYrMarks = 0;
int fourthYrMarks = 0;
Builder* setName(string inName) {
this->name = inName;
return this;
}
Builder* setAddress(string inAddr) {
this->address = inAddr;
return this;
}
Builder* setTenthMarks(int in10thMarks) {
this->tenthMarks = in10thMarks;
return this;
}
Builder* setTwelevethMarks(int in12thMarks) {
this->twelevethMarks = in12thMarks;
return this;
}
Builder* setFirstYrMarks(int in1stYrMarks) {
this->FirstYrMarks = in1stYrMarks;
return this;
}
Builder* setSecondYrMarks(int in2ndYrMarks) {
this->secondYrMarks = in2ndYrMarks;
return this;
}
Builder* setThirdYrMarks(int in3rdYrMarks) {
this->thirdYrMarks = in3rdYrMarks;
return this;
}
Builder* setFourthYrMarks(int in4thYrMarks) {
this->fourthYrMarks = in4thYrMarks;
return this;
}
Student build() {
return Student(*this);
}
};
Student(Builder builder)
: name(builder.name), address(builder.address), tenthMarks(builder.tenthMarks), twelevethMarks(builder.twelevethMarks), FirstYrMarks(builder.FirstYrMarks),
secondYrMarks(builder.secondYrMarks), thirdYrMarks(builder.thirdYrMarks),fourthYrMarks(builder.fourthYrMarks) {}
void display(){
if(this->name != ""){
cout <<"Name : "<<this->name <<endl;
}
if(this->address !=""){
cout<<"Address : " <<this->address <<endl;
}
if(this->tenthMarks != 0){
cout<<"Tenth Marks " <<this->tenthMarks<<endl;
}
if(this->twelevethMarks != 0){
cout<<"Twelvth Marks: " <<this->twelevethMarks<<endl;
}
if(this->FirstYrMarks != 0){
cout<<"First Year Marks: " <<this->FirstYrMarks<<endl;
}
if(this->secondYrMarks !=0){
cout<<"Second Year Marks: " <<this->secondYrMarks<<endl;
}
if(this->thirdYrMarks != 0){
cout<<"Third Year Marks: " <<this->thirdYrMarks<<endl;
}
if(this->fourthYrMarks != 0){
cout<<"Fourth Year Marks: " <<this->fourthYrMarks<<endl;
}
}
};
#endif /* STUDENT_H_ */
And here goes the main method
//============================================================================
// Name : BuilderPattern.cpp
// Author : Som
// Version :
// Copyright : som-itsolutions
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "Student.h"
using namespace std;
int main() {
Student secondYrStud = Student::Builder().setName("Ram")->setAddress("Kolkata")
->setTenthMarks(50)->setTwelevethMarks(60)->setFirstYrMarks(70)->build();
secondYrStud.display();
Student thirdYrStud = Student::Builder().setName("Shyam")->setAddress("Lucknow")
->setTenthMarks(50)->setTwelevethMarks(60)->setFirstYrMarks(70)->setSecondYrMarks(65)->build();
thirdYrStud.display();
}
Top comments (0)