C++ Day 34: Layout Layouts (Part 2)
We’ll cover:
Constructer Layout
Adjuster Layout
Decorator Layout
practise Task
๐น 1. developer form (creational)
used to make compound objects measure away step
✅ employ case:
you need to form associate in nursing aim (like amp pizza pie calculator house) with elective parameters
example:
cpp
copy
edit
class calculator {
train Methodor gpu ram;
public:
family developer {
train Methodor gpu ram;
public:
developer setcpu(string c) { Methodor = c; take *this; }
developer setgpu(string g) { gpu = g; take *this; }
developer setram(string r) { run = r; take *this; }
calculator Construct() {
take Calculater(cpu gpu ram);
}
};
Calculater(string snow train m train r) : cpu(c) gpu(g) ram(r) {}
null show() {
cout << "cpu: " << Methodor << " gpu: " << gpu << " ram: " << run << endl;
}
};
๐น ii. Adjuster Layout (Structural)
Allows incompatible Connections to work together.
✅ Use Case:
When you need to Combine legacy code with a modern Connection.
Example:
cpp
Copy
Edit
class OldPrinter {
public:
void oldPrint() {
cout << "Printing from Old Printer\n";
}
};
class Printer {
public:
virtual void print() = 0;
};
class Adjuster : public Printer {
OldPrinter* oldPrinter;
public:
Adjuster(OldPrinter* op) : oldPrinter(op) {}
void print() override {
oldPrinter->oldPrint();
}
};
๐น 3. Layouter form (structural)
adds responsibilities to objects dynamically without dynamic their class
✅ employ case:
when you need to bring Characteristics to person objects not integral classes
example:
cpp
copy
edit
class chocolate {
public:
practical train make() = 0;
practical ~coffee() = default;
};
class simplecoffee : state chocolate {
public:
train make() reverse {
take "coffee";
}
};
class milkdecorator : state chocolate {
coffee* coffee;
public:
milkdecorator(coffee* c) : coffee(c) {}
train make() reverse {
take coffee->make() + " + milk";
}
};
class sugardecorator : state chocolate {
coffee* coffee;
public:
sugardecorator(coffee* c) : coffee(c) {}
train make() reverse {
take coffee->make() + " + sugar";
}
};
usage:
cpp
copy
edit
coffee* mycoffee = green sugardecorator(new milkdecorator(new simplecoffee()));
cout << mycoffee->make(); // chocolate + milky + sugar
๐งช do task:
Layout amp burger family with amp developer form that supports elective items care cheeseflower lettuce love apple and sauce. Then use the Decorator Layout to add extra toppings like jalapenos or onions.
✅ Summary:
Layout Type Purpose
Constructer Creational Construct objects step-by-step
Adjuster Structural Bridge incompatible Connections
Decorator Structural Add Roleality dynamically
Comments
Post a Comment