การวิเคราะห์และออกแบบระบบเชิงวัตถุ

UML Basics and Fundamental Diagram Types

What's UML ? (UML คืออะไร)

  • การเรียนรู้ระบบโดยการศึกษา Source Code มักใช้เวลานาน

  • การสื่อสารแนวคิดเพื่อออกแบบระบบซอฟ์ทแวร์ ยิ่งท้าทายมากกว่า หากหาข้อสรุปมิได้
     

  • UML ได้ถูกแนะนำขึ้นเพื่อใช้แก้ไขปัญหาเหล่านี้ UML ไม่ใช่การเขียนโปรแกรม แต่เป็นการนำเสนอด้วยรูปภาพ ประกอบด้วยแผนภาพต่าง ๆ เพื่อใช้อธิบายระบบ

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

class Item {
public:
    Item(std::string name, std::string description) : name(name), description(description) {}
    std::string getName() const { return name; }
    std::string getDescription() const { return description; }

private:
    std::string name;
    std::string description;
};

class Room {
public:
    Room(std::string name, std::string description) : name(name), description(description) {}
    void addItem(Item item) { items.push_back(item); }
    void removeItem(int index) { items.erase(items.begin() + index); }
    std::string getName() const { return name; }
    std::string getDescription() const { return description; }
    std::vector<Item> getItems() const { return items; }

private:
    std::string name;
    std::string description;
    std::vector<Item> items;
};

class Player {
public:
    Player(std::string name) : name(name), health(100) {}
    void addItem(Item item) { inventory.push_back(item); }
    void removeItem(int index) { inventory.erase(inventory.begin() + index); }
    std::string getName() const { return name; }
    int getHealth() const { return health; }
    void setHealth(int newHealth) { health = newHealth; }
    std::vector<Item> getInventory() const { return inventory; }

private:
    std::string name;
    int health;
    std::vector<Item> inventory;
};

class Game {
public:
    Game() {
        initializeRooms();
        currentRoom = &rooms[0];
    }

    void play() {
        std::cout << "Welcome to the Text Adventure Game!" << std::endl;
        std::cout << "Enter your name: ";
        std::string playerName;
        std::getline(std::cin, playerName);
        player = Player(playerName);

        while (true) {
            displayStatus();
            std::string command;
            std::cout << "> ";
            std::getline(std::cin, command);
            processCommand(command);

            if (player.getHealth() <= 0) {
                std::cout << "Game Over! You have died." << std::endl;
                break;
            }
        }
    }

private:
    std::vector<Room> rooms;
    Room* currentRoom;
    Player player;

    void initializeRooms() {
        rooms.push_back(Room("Entrance", "You are at the entrance of a dark cave."));
        rooms.push_back(Room("Cavern", "You enter a large cavern with stalactites hanging from the ceiling."));
        rooms.push_back(Room("Underground Lake", "You find yourself at the shore of an underground lake."));

        rooms[0].addItem(Item("Torch", "A wooden torch that provides light."));
        rooms[1].addItem(Item("Sword", "A rusty old sword."));
        rooms[2].addItem(Item("Boat", "A small wooden boat."));
    }

    void displayStatus() {
        std::cout << "\n=== " << currentRoom->getName() << " ===" << std::endl;
        std::cout << currentRoom->getDescription() << std::endl;
        std::cout << "Items in the room:" << std::endl;
        for (const auto& item : currentRoom->getItems()) {
            std::cout << "- " << item.getName() << std::endl;
        }
        std::cout << "\nYour health: " << player.getHealth() << std::endl;
        std::cout << "Your inventory:" << std::endl;
        for (const auto& item : player.getInventory()) {
            std::cout << "- " << item.getName() << std::endl;
        }
    }

    void processCommand(const std::string& command) {
        if (command == "quit") {
            std::cout << "Thanks for playing!" << std::endl;
            exit(0);
        } else if (command == "look") {
            // Do nothing, status will be displayed on next loop
        } else if (command.substr(0, 4) == "take") {
            takeItem(command.substr(5));
        } else if (command.substr(0, 4) == "drop") {
            dropItem(command.substr(5));
        } else if (command == "north" || command == "south" || command == "east" || command == "west") {
            move(command);
        } else {
            std::cout << "I don't understand that command." << std::endl;
        }
    }

    void takeItem(const std::string& itemName) {
        for (size_t i = 0; i < currentRoom->getItems().size(); ++i) {
            if (currentRoom->getItems()[i].getName() == itemName) {
                player.addItem(currentRoom->getItems()[i]);
                currentRoom->removeItem(i);
                std::cout << "You took the " << itemName << "." << std::endl;
                return;
            }
        }
        std::cout << "There's no " << itemName << " here." << std::endl;
    }

    void dropItem(const std::string& itemName) {
        for (size_t i = 0; i < player.getInventory().size(); ++i) {
            if (player.getInventory()[i].getName() == itemName) {
                currentRoom->addItem(player.getInventory()[i]);
                player.removeItem(i);
                std::cout << "You dropped the " << itemName << "." << std::endl;
                return;
            }
        }
        std::cout << "You don't have a " << itemName << "." << std::endl;
    }

    void move(const std::string& direction) {
        // This is a simplified movement system
        int newRoomIndex = std::rand() % rooms.size();
        currentRoom = &rooms[newRoomIndex];
        std::cout << "You move " << direction << "." << std::endl;
        
        // Random chance of taking damage
        if (std::rand() % 4 == 0) {
            int damage = std::rand() % 20 + 1;
            player.setHealth(player.getHealth() - damage);
            std::cout << "You stumble and take " << damage << " damage!" << std::endl;
        }
    }
};

int main() {
    std::srand(std::time(nullptr));
    Game game;
    game.play();
    return 0;
}

What's UML ? (UML คืออะไร)

  • เราสามารถใช้แผนภาพ (Diagram) เหล่านี้เพื่ออธิบายรูปร่างของระบบ รวมถึงความสัมพันธ์ในระบบด้วย

  • UML ประกอบด้วยแผนภาพหลายประเภท ในที่นี้เราจะศึกษาเฉพาะแผนภาพที่จำเป็น

Lamborghini

Toyota

Volkswagen

Use Case Diagram

แผนภาพ
Use Case
ใช้อธิบาย ฟังก์-
ชั่นการทำงานของระบบ จากมุมมองของผู้ใช้งาน

Functional

Class Diagram

Class Diagram
ใช้อธิบายโครงสร้างของระบบเชิงวัตถุ คุณลักษณะ การทำงาน และความสัมพันธ์

Structural

Sequence Diagram

แผนภาพ Sequence ใช้อธิบายการทำงานของระบบ โดยมุ่งเน้นกิจกรรมและความสัมพันธ์ที่่เกิดขึ้นระหว่างวัตถุ

Dynamic

Models

functional

structural

dynamic

UML to Source Code

#include <iostream>
using namespace std;

class myCar{
    public:
        string brand;
        string color;
        string type="Standard Car";
        int price;
};

class mySuperCar: public myCar{
    public:
        string type="Super Car";
};

int main() {
    // Write C++ code here
    mySuperCar car1;
    car1.brand = "Lamborghini";
    car1.color = "Red";
    car1.price = 24000000;
    cout << "Car1: brand=" << car1.brand << " type=" << car1.type << " color=" << car1.color << " price=" << car1.price << endl;
    
    myCar car2;
    car2.brand = "Toyota";
    car2.color = "White";
    car2.price = 1400000;
    cout << "Car2: brand=" << car2.brand << " type=" << car2.type << " color=" << car2.color << " price=" << car2.price << endl;
    
    myCar car3;
    car1.brand = "Volkswagen";
    car1.color = "Yellow";
    car1.price = 1200000;
    cout << "Car3: brand=" << car3.brand << " type=" << car3.type << " color=" << car3.color << " price=" << car3.price << endl;
    
    cout << "Program terminated..";

    return 0;
}

Aj. Krit Th.

https://www.kritth.com