composite

composite


组合(复合)


has-a

【例3-7】Line & Point

class Line{
public:
    Line(Point p1,Point p2):p1(p1),p2(p2){};
    Line(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2){};
private:
    Point p1,p2;
};
class Point{
public:
    Point(){};
    Point(int x,int y):x(x),y(y){};
private:
    int x = 0,y = 0;
};

【例3-8】Stack + Node

class Stack{
public:
    void push(int item);
    int pop();
    bool isEmpty();
private:
    Node head;
};

Stack+Node

void Stack::push(int item){
    Node * node = new Node(item);
    node->next = head.next;
    head.next = node;
}
int pop(){
    Node *first = head.next;
    int value =first->value;
    head.next = first->next;
    delete first;
    return value;
}
bool isEmpty(){
    return head.next == null;
}

Stack+Node

class Node{
public:
    Node(int value = 0):value(value){};
    int value;
    Node * next = null;
};

Stack+Node

struct Node{
    Node(int value=0):value(value){};
    int value;
    Node * next = null;
};

不限容量的Stack


试试看:多少种不同实现方法?

【例3-9】Stack+Stack

class Stack{
public:
    void push(int item);
    int pop();
    bool isEmpty();
private:
    int data[100];
    int count = 0;
    Stack *next;
};

Stack+Stack

void Stack::push(int item){
    if (count<100){
        data[count]=item;
    }else{
        if(count == 100){
            next = new Stack();
        }
        next->push(item);
    }
    count++;
}

Stack+Stack

int pop(){
    int value;
    count--;
    if (count<100){
        value = data[count];
    }else{
        item = next->pop();
        if(count == 100){
            delete next;
        }
    }
    return value;
}
bool isEmpty(){
    return 0 == count;
}

【例3-10-1】Stack+Vector(stl)

class Stack{
public:
    void push(int item);
    int pop();
    bool isEmpty();
private:
    Vector data;
};

Stack+Vector

void Stack::push(int item){
    data.push_back(item);
}

int Stack::pop(){
    int value = data.back();
    data.pop_back();
    return value;
}
bool Stack::isEmpty(){
    return data.empty();
}

【例3-10-2】private继承 实现组合

class Stack:private Vector<int>{
public:
    void push(int item);
    int pop();
    bool isEmpty();
};

private继承 实现组合

void Stack::push(int item){
    push_back(item);
}

int Stack::pop(){
    int value = back();
    pop_back();
    return value;
}
bool Stack::isEmpty(){
    return empty();
}

UML(Unified Modeling Language)

(统一建模语言)

UML(Unified Modeling Language)

(统一建模语言)

【例3-11】电脑入库及销售 OO版

功能描述:

a) 程序启动时,屏幕上显示菜单:
   1)查看库存
   2)电脑入库
   3)卖出
   4)退出程序”;
   
b) 用户输入菜单编号

c) 系统根据菜单编号,分别作出相应动作
    如果不是1-4,则提示用户:“错误的菜单项”,要求用户重新输入
    
d)系统完成相应功能后,重新显示菜单;用户可再次选择所需要的功能;

1)查看库存 功能描述:

显示库存列表,每行为某种电脑的所有数据,

包括:型号、CPU、内存容量、硬盘容量、库存数量;

2)电脑入库 功能描述:

a)系统提示用户输入电脑型号;

b)用户输入电脑型号;

c)系统提示用户输入入库数量;

d)用户输入数量;

e)系统查找库存:
    如果已有该型号的电脑,则追加新入库的数量;
    否则,新建一条记录;
    

3)卖出 功能描述:

a)系统提示用户输入电脑型号;

b)用户输入电脑型号;

c)系统查找库存:
    如果不存在该型号电脑,则提示用户,并返回主菜单;
    
d)系统提示用户输入卖出数量;

e)用户输入数量;

f)系统减去卖出数量;

电脑销售 class diagram

单一职责原则

  1. 一个模块只做一件事

  2. 一件事只在一个模块中做

【作业3-2】C++考试成绩管理 OO版

The End

目录