initialize repository of design pattern template

This commit is contained in:
LU
2025-08-09 08:41:14 +08:00
commit 1727e31b9d
32 changed files with 2149 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
#include "strategy.hpp"
SortPerson::SortPerson(ICompare* pICompare)
{
m_CompareStrategy = pICompare;
}
void SortPerson::sort(vector<StrategyPerson>& personList)
{
for (size_t i = 0; i < personList.size() - 1; i++) // Bubble sort
{
for (size_t j = i + 1; j < personList.size(); j++)
{
if (m_CompareStrategy->comparable(&personList[i], &personList[j]))
{
StrategyPerson temp = personList[i];
personList[i] = personList[j];
personList[j] = temp;
}
}
}
}
bool CompareByWeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
{
return person1->m_weight < person2->m_weight;
}
bool CompareByHeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
{
return person1->m_height < person2->m_height;
}
bool CompareByAge::comparable(StrategyPerson* person1, StrategyPerson* person2)
{
return person1->m_age < person2->m_age;
}
StrategyPerson::StrategyPerson(string name, int age, double weight, double height)
{
m_name = name, m_age = age, m_weight = weight, m_height = height;
}
void StrategyPerson::showMyself()
{
cout << m_name << " ÄêÁ䣺" << m_age << "Ëê, ÌåÖØ£º" << m_weight << "kg, Éí¸ß£º" << m_height << "m" << endl;
}