嵌入式C++(十四)

简介: 嵌入式C++(十四)

一、原型模式


#include <iostream>
#include <string>
using namespace std;
class Person
{
protected:
    string name;
    int age;
public:
    Person()
    {
    }
    Person(string s,int a)
    {
        this->name = s;
        this->age = a;
    }
public:
    virtual void show() = 0;
    virtual Person *clone() = 0;
};
class Student :public Person
{
private:
    int id;
public:
    Student()
    {
    }
    Student(string s,int a,int i):Person(s,a)
    {
        this->id = i;
    }
    void show()
    {
        cout << "name = " << name << " age = " << age << " id = " << id << endl;
    }
    Person *clone()
    {
        Student *tmp = new Student;
        *tmp = *this;
        return tmp;
    }
};
int main(int argc, char const *argv[])
{
    Person *p1 = new Student("zzz", 18, 21);
    Person *p2 = p1->clone();
    p2->show();
    return 0;
}


二、组合模式


#include <iostream>
#include <string>
#include <list>
using namespace std;
class iFile
{
public:
    virtual int Add(iFile *file) = 0;
    virtual string Getname() = 0;
    virtual int remove(iFile *file) = 0;
    virtual list<iFile *> *GetChild() = 0;
};
class File:public iFile
{
private:
    string name;
public:
    File(string n)
    {
        name = n;
    }
    virtual int Add(iFile *file)
    {
        return -1;
    }
    virtual string Getname()
    {
        return name;
    }
    virtual int remove(iFile *file) 
    {
        return -1;
    }
    virtual list<iFile *> *GetChild() 
    {
        return nullptr;
    }
};
class Dir:public iFile
{
private:
    string name;
    list<iFile *> *l;
public:
    Dir(string n)
    {
        name = n;
        l = new list<iFile *>;
    }
    virtual int Add(iFile *file)
    {
        l->emplace_back(file);
        return 1;
    }
    virtual string Getname()
    {
        return name;
    }
    virtual int remove(iFile *file) 
    {
        l->remove(file);
        return 1;
    }
    virtual list<iFile *> *GetChild() 
    {
        return l;
    }
};
void show(iFile *root, int gap)
{
    for (size_t i = 0; i < gap; i++)
    {
        cout << "---";
    }
    if (root != NULL)
    {
        cout << root->Getname() << endl;
    }
    else
    {
        return;
    }
    auto l = root->GetChild();
    if (l != nullptr)
    {
        for (auto it = l->begin(); it != l->end();it++)
        {
            show((*it), gap + 1);
        }
    }
}
int main(int argc, char const *argv[])
{
    iFile *dir1 = new Dir("day1");
    iFile *dir2 = new Dir("day2");
    iFile *dir3 = new Dir("day3");
    iFile *file1 = new File("1.c");
    iFile *file2 = new File("2.c");
    iFile *file3 = new File("3.c");
    iFile *file4 = new File("4.c");
    iFile *file5 = new File("5.c");
    iFile *file6 = new File("6.c");
    iFile *file7 = new File("7.c");
    dir2->Add(file1);
    dir2->Add(file2);
    dir2->Add(file3);
    dir2->Add(file4);
    dir2->Add(file5);
    dir2->Add(file6);
    dir3->Add(file1);
    dir3->Add(file3);
    dir3->Add(file5);
    dir3->Add(file7);
    dir1->Add(dir2);
    dir1->Add(dir3);
    show(dir1, 0);
    return 0;
}


三、代理模式


#include <iostream>
#include <string>
using namespace std;
class Sale
{
public:
    virtual void SaleBook() = 0;
};
class BookStore:public Sale
{
private:
    static int count;
public:
    void SaleBook()
    {
        cout << "书店卖书" << endl;
        count++;
    }
    static int GetCount()
    {
        return count;
    }
};
class TaoBao:public Sale
{
private:
    Sale *sale;
public:
    TaoBao(Sale *s)
    {
        sale = s;
    }
    void SaleBook()
    {
        cout << "网上卖书" << endl;
        sale->SaleBook();
    }
};
int BookStore::count = 0;
int main(int argc, char const *argv[])
{
    Sale *store = new BookStore;
    Sale *taobao = new TaoBao(store);
    taobao->SaleBook();
    store->SaleBook();
    cout << BookStore::GetCount() << endl;
    return 0;
}


四、装饰模式


#include <iostream>
#include <string>
using namespace std;
class Phone
{
private:
    string name;
public:
    virtual void function() = 0;
};
class CallPhone:public Phone
{
private:
    Phone *phone;
public:
    // CallPhone(Phone *p)
    // {
    //     phone = p;
    // }
    void function()
    {
        // phone->function();
        cout << "打电话的功能" << endl;
    }
};
class MusicPhone:public Phone
{
private:
    Phone *phone;
public:
    MusicPhone(Phone *p)
    {
        phone = p;
    }
    void function()
    {
        phone->function();
        cout << "听音乐的功能" << endl;
    }
};
class PhotoPhone:public Phone
{
private:
    Phone *phone;
public:
    PhotoPhone(Phone *p)
    {
        phone = p;
    }
    void function()
    {
        phone->function();
        cout << "相机的功能" << endl;
    }
};
int main(int argc, char const *argv[])
{
    cout << "******************************" << endl;
    Phone *callPhone = new CallPhone();
    callPhone->function();
    cout << "******************************" << endl;
    Phone *musicphone = new MusicPhone(callPhone);
    musicphone->function();
    cout << "******************************" << endl;
    Phone *photophone = new PhotoPhone(musicphone);
    photophone->function();
    return 0;
}


五、适配器模式


#include <iostream>
using namespace std;
class Current
{
public:
    virtual int GetCurrent() = 0;
};
class Current_220:public Current
{
public:
    int GetCurrent()
    {
        return 220;
    }
};
class Adapter:public Current
{
private:
    Current *current;
public:
    Adapter(Current *c)
    {
        current = c;
    }
    int GetCurrent()
    {
        return 12;
    }
};
class Phone
{
private:
    Current *current;
public:
    Phone(Current *c)
    {
        current = c;
    }
    void check()
    {
        if (current->GetCurrent() == 220)
        {
            cout << "手机爆炸" << endl;
        }
        else if(current->GetCurrent() == 12)
        {
            cout << "手机正在充电" << endl;
        }
    }
};
int main(int argc, char const *argv[])
{
    Current *c = new Current_220();
    Current *a = new Adapter(c);
    Phone *p = new Phone(a);
    p->check();
    return 0;
}
相关文章
|
7月前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
224 1
|
7月前
|
算法 Linux 程序员
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
|
7月前
|
数据处理 C++ UED
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
125 0
|
7月前
|
C语言 数据安全/隐私保护 C++
嵌入式中如何把C++代码改写成C语言代码
嵌入式中如何把C++代码改写成C语言代码
80 0
|
7月前
|
存储 缓存 Java
嵌入式系统中C++内存管理基本方法
嵌入式系统中C++内存管理基本方法
138 0
|
7月前
|
存储 编译器 程序员
嵌入式系统中C++基础知识精髓
嵌入式系统中C++基础知识精髓
113 0
|
7月前
|
关系型数据库 数据库 C++
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
|
7月前
|
存储 编译器 C++
嵌入式中C++ 编程习惯与编程要点分析
嵌入式中C++ 编程习惯与编程要点分析
57 1
|
7月前
|
架构师 数据挖掘 程序员
嵌入式系统中C++ 类的设计和实现分析
嵌入式系统中C++ 类的设计和实现分析
75 1
|
7月前
|
算法 小程序 编译器
嵌入式中C++开发的基本操作方法
嵌入式中C++开发的基本操作方法
61 0