Skip to content

Commit b1915b4

Browse files
committed
last
1 parent 1dfe60c commit b1915b4

14 files changed

Lines changed: 280 additions & 57 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}

assignment_4/c++/builder.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
class Person {
5+
public:
6+
std::string name;
7+
int age;
8+
std::string address;
9+
};
10+
11+
class PersonBuilder {
12+
private:
13+
Person person;
14+
15+
public:
16+
PersonBuilder& withName(const std::string& name) {
17+
person.name = name;
18+
return *this;
19+
}
20+
21+
PersonBuilder& withAge(int age) {
22+
person.age = age;
23+
return *this;
24+
}
25+
26+
PersonBuilder& withAddress(const std::string& address) {
27+
person.address = address;
28+
return *this;
29+
}
30+
31+
Person build() {
32+
return person;
33+
}
34+
};
35+
36+
void runBuilder() {
37+
std::cout << "=== Builder Pattern ===" << std::endl;
38+
Person person = PersonBuilder()
39+
.withName("홍길동")
40+
.withAge(20)
41+
.withAddress("부산대학교 양산캠퍼스")
42+
.build();
43+
std::cout << "Name: " << person.name << std::endl;
44+
std::cout << "Age: " << person.age << std::endl;
45+
std::cout << "Address: " << person.address << std::endl;
46+
}

assignment_4/c++/decorator.cpp

Lines changed: 0 additions & 44 deletions
This file was deleted.

assignment_4/c++/decorator.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Coffee {
5+
public:
6+
virtual int cost() = 0;
7+
virtual ~Coffee() {}
8+
};
9+
10+
class BasicCoffee : public Coffee {
11+
public:
12+
int cost() override {
13+
return 5;
14+
}
15+
};
16+
17+
class CoffeeDecorator : public Coffee {
18+
protected:
19+
Coffee* coffee;
20+
public:
21+
CoffeeDecorator(Coffee* c) : coffee(c) {}
22+
};
23+
24+
class MilkDecorator : public CoffeeDecorator {
25+
public:
26+
MilkDecorator(Coffee* c) : CoffeeDecorator(c) {}
27+
int cost() override {
28+
return coffee->cost() + 2;
29+
}
30+
};
31+
32+
class SugarDecorator : public CoffeeDecorator {
33+
public:
34+
SugarDecorator(Coffee* c) : CoffeeDecorator(c) {}
35+
int cost() override {
36+
return coffee->cost() + 1;
37+
}
38+
};
39+
40+
void runDecorator() {
41+
cout << "=== Decorator Pattern ===" << endl;
42+
43+
Coffee* coffee = new BasicCoffee();
44+
cout << "coffee = BasicCoffee()" << endl;
45+
cout << "coffee.cost() = " << coffee->cost() << endl << endl;
46+
47+
Coffee* coffee_with_milk = new MilkDecorator(new BasicCoffee());
48+
cout << "coffee_with_milk = MilkDecorator(BasicCoffee())" << endl;
49+
cout << "coffee_with_milk.cost() = " << coffee_with_milk->cost() << endl << endl;
50+
51+
Coffee* coffee_with_sugar = new SugarDecorator(new BasicCoffee());
52+
cout << "coffee_with_sugar = SugarDecorator(BasicCoffee())" << endl;
53+
cout << "coffee_with_sugar.cost() = " << coffee_with_sugar->cost() << endl << endl;
54+
55+
Coffee* coffee_with_milk_and_sugar = new SugarDecorator(new MilkDecorator(new BasicCoffee()));
56+
cout << "coffee_with_milk_and_sugar = SugarDecorator(MilkDecorator(BasicCoffee()))" << endl;
57+
cout << "coffee_with_milk_and_sugar.cost() = " << coffee_with_milk_and_sugar->cost() << endl;
58+
59+
delete coffee;
60+
delete coffee_with_milk;
61+
delete coffee_with_sugar;
62+
delete coffee_with_milk_and_sugar;
63+
}

assignment_4/c++/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ void runEigen();
88
void runFacade();
99
void runFactory();
1010
void runMethodChain();
11+
void runBuilder();
1112

1213
int main() {
1314
runAdapter();
@@ -16,5 +17,6 @@ int main() {
1617
runFacade();
1718
runFactory();
1819
runMethodChain();
20+
runBuilder();
1921
return 0;
2022
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ class Calculator {
3535

3636
void runMethodChain() {
3737
Calculator calc;
38-
double result = calc.add(10).subtract(2).multiply(3).divide(4).getResult();
39-
cout << "[Method Chaining] 계산 결과: " << result << endl;
40-
}
38+
39+
calc.add(10);
40+
cout << "add(10): " << calc.getResult() << endl;
41+
42+
calc.subtract(2);
43+
cout << "subtract(2): " << calc.getResult() << endl;
44+
45+
calc.multiply(3);
46+
cout << "multiply(3): " << calc.getResult() << endl;
47+
48+
calc.divide(4);
49+
cout << "divide(4): " << calc.getResult() << endl;
50+
51+
cout << "최종 계산 결과: " << calc.getResult() << endl;
52+
}

0 commit comments

Comments
 (0)