Ma’lumotlarlar modeli va ularni ishlatish. Uml modellashtirish tili Reja


Download 54.77 Kb.
bet7/7
Sana18.06.2023
Hajmi54.77 Kb.
#1558734
1   2   3   4   5   6   7
Bog'liq
15 Ma

int getValue() { return value; }
};
class MeasureCount
{
int number;
static int total;
public:
MeasureCount() { number = 0; }
void increment() { number++; total++; }
int getNumber() { return number; }
static int getTotal() { return total; }
};
int MeasureCount::total = 0;
class ITemperatureMeasure // интерфейс
{
public:
virtual void setValue() = 0;
virtual double getValue() = 0;
};
class TemperatureMeasure : public virtual ITemperatureMeasure
{ // реализация интерфейса
private:
Sensor* h; // агрегация
MeasureCount* measure; // композиция
public:
TemperatureMeasure(Sensor* h)
{
measure = new MeasureCount();
this->h = h;
}
void setValue()
{
int value;
measure->increment();
cout << "t[" << measure->getNumber() << "]= ";
cin >> value;
h->setValue(value);
}
double getValue() { return (double)h->getValue() / measure->getNumber(); }
int getNumber() { return measure->getNumber(); }
static int getTotal() { return MeasureCount::getTotal(); }
};


class ShowTemperature // зависимость
{
public:
static void Show(TemperatureMeasure t)
{
cout << t.getNumber() << ": ";
cout << t.getValue() << " oC" << endl;
}
};


int main()
{
Sensor* h1 = new Sensor();
TemperatureMeasure tc1(h1);
for (int i = 0; i < 5; i++)
tc1.setValue(); // требуется ввести температуру за 5 дней
ShowTemperature::Show(tc1); // вывод средней температуры
cout << endl;
Sensor* h2 = new Sensor();
TemperatureMeasure tc2(h2);
for (int i = 0; i < 5; i++)
tc2.setValue(); // требуется ввести температуру за 5 дней
ShowTemperature::Show(tc2); // вывод средней температуры
cout << endl;
cout << "Total: " << TemperatureMeasure::getTotal() << endl;
system("pause");
return 0;
}
Listing 1 - Kod namunasi
Yuqoridagi kod uchun UML sinf diagrammasi quyidagicha ko‘rinadi:

12-rasm. Listing 1 ning UML diagrammasi
Sinf diagrammasida asosiy sinf TemperatureMeasure sinfidir, bu harorat o‘lchagichidir. Barcha o‘lchovlarning o‘rtacha arifmetikasi o‘lchov qiymati sifatida hosil bo‘ladi - barcha o‘lchovlarning yig‘indisi ularning soniga bo‘linadi.
Sensor sinfi (harorat sensori sifatida) o‘lchovlarni qabul qilish va ularni qo‘shish uchun ishlatiladi. Konsol topshirig‘ida o‘lchovlarning o‘zi ushbu sinfga yig‘indiga o‘tkaziladi. Sinf asosiy TemperatureMeasure klassi bilan birlashma munosabatlariga ega: biz avval Sensor sinfining obyektini yaratamiz, so‘ngra uni sinfning bir qismi sifatida foydalanish uchun TemperatureMeasure sinfiga parametr sifatida konstruktorga o‘tkazamiz.
ITemperatureMeasure klassi TemperatureMeasure sinfining interfeysi bo‘lib, uni amalga oshiruvchi provayder hisoblanadi .
Va nihoyat, ShowTemperature klassi TemperatureMeasure klassi bilan bog‘liqlik munosabatlarida, chunki ShowTemperature-ning yagona Show usulini amalga oshirish TemperatureMeasure tuzilishiga bog‘liq.
Download 54.77 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling