Skip to content

Commit 3038796

Browse files
author
XuhuaHuang
committed
Upload strategy design pattern example
1 parent e2d988f commit 3038796

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

DesignPattern/strategy.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @file strategy.cpp
3+
* @author Xuhua Huang
4+
* @brief
5+
* https://compiler-explorer.com/z/eKfjKscPv
6+
*
7+
* @version 0.1
8+
* @date 2026-04-12
9+
*
10+
* @copyright Copyright (c) 2026
11+
*
12+
*/
13+
14+
#include <iomanip>
15+
#include <iostream>
16+
#include <memory>
17+
#include <sstream>
18+
#include <string>
19+
20+
class date_t;
21+
22+
class date_formatter_t {
23+
public:
24+
virtual ~date_formatter_t() = default;
25+
26+
virtual std::string to_string(const date_t& date) const = 0;
27+
};
28+
29+
class date_t {
30+
public:
31+
date_t(int y, int m, int d, int h, int min, int s, std::unique_ptr<date_formatter_t> formatter)
32+
: m_year(y)
33+
, m_month(m)
34+
, m_day(d)
35+
, m_hour(h)
36+
, m_minute(min)
37+
, m_second(s)
38+
, m_formatter(std::move(formatter)) {}
39+
40+
void set_formatter(std::unique_ptr<date_formatter_t> formatter) { m_formatter = std::move(formatter); }
41+
42+
std::string to_string() const { return m_formatter->to_string(*this); }
43+
44+
inline int year() const { return m_year; }
45+
inline int month() const { return m_month; }
46+
inline int day() const { return m_day; }
47+
inline int hour() const { return m_hour; }
48+
inline int minute() const { return m_minute; }
49+
inline int second() const { return m_second; }
50+
51+
private:
52+
int m_year;
53+
int m_month;
54+
int m_day;
55+
int m_hour;
56+
int m_minute;
57+
int m_second;
58+
59+
std::unique_ptr<date_formatter_t> m_formatter;
60+
};
61+
62+
class date_only_formatter_t : public date_formatter_t {
63+
public:
64+
std::string to_string(const date_t& date) const override {
65+
std::ostringstream os;
66+
os << std::setw(2) << std::setfill('0') << date.month() << "-" << std::setw(2) << std::setfill('0') << date.day()
67+
<< "-" << date.year();
68+
return os.str();
69+
}
70+
};
71+
72+
class date_time_formatter_t : public date_formatter_t {
73+
public:
74+
std::string to_string(const date_t& date) const override {
75+
std::ostringstream os;
76+
os << std::setw(2) << std::setfill('0') << date.month() << "-" << std::setw(2) << std::setfill('0') << date.day()
77+
<< "-" << date.year() << " " << std::setw(2) << std::setfill('0') << date.hour() << ":" << std::setw(2)
78+
<< std::setfill('0') << date.minute() << ":" << std::setw(2) << std::setfill('0') << date.second();
79+
return os.str();
80+
}
81+
};
82+
83+
class time_only_formatter_t : public date_formatter_t {
84+
public:
85+
std::string to_string(const date_t& date) const override {
86+
std::ostringstream os;
87+
os << std::setw(2) << std::setfill('0') << date.hour() << ":" << std::setw(2) << std::setfill('0') << date.minute()
88+
<< ":" << std::setw(2) << std::setfill('0') << date.second();
89+
return os.str();
90+
}
91+
};
92+
93+
int main() {
94+
date_t d(2011, 11, 5, 9, 52, 0, std::make_unique<date_only_formatter_t>());
95+
96+
std::cout << "date: " << d.to_string() << "\n";
97+
98+
d.set_formatter(std::make_unique<time_only_formatter_t>());
99+
std::cout << "time: " << d.to_string() << "\n";
100+
101+
d.set_formatter(std::make_unique<date_time_formatter_t>());
102+
std::cout << "datetime: " << d.to_string() << "\n";
103+
104+
return 0;
105+
}

0 commit comments

Comments
 (0)