嵌入式C++(十五)

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

一 享元模式


主要通过与其它类对象共享数据来减少内存的使用
适用情况:有大量对象需要重复创建的时候,或者以共享内存的方式

0a2653c851af460fa595bd959398a8f1.png

#include <iostream>
#include <map>
#include <string>
using namespace std;
class Student
{
private:
  string name;
  int age;
  int id;
public:
  Student()
  {
  }
  Student(string n, int a, int i)
  {
  name = n;
  age = a;
  id = i;
  }
  string GetName()
  {
  return name;
  }
  int GetAge()
  {
  return age;
  }
  int GetId()
  {
  return id;
  }
};
class FwFactory
{
private:
  multimap<int, Student*> *m;
public:
  FwFactory()
  {
  m = new multimap<int, Student *>;
  }
  ~FwFactory()
  {
  while (!m->empty())
  {
    Student *tmp = m->begin()->second;
    delete tmp;
    m->erase(m->begin());
  }
  delete m;
  }
  void GetPerson(int id)
  {
  string name;
  int age;
  multimap<int, Student *>::iterator it;
  it = m->find(id);
  if (it == m->end())
  {
    cout << "input the name age..." << endl;
    cin >> name >> age;
    Student *s = new Student(name, age, id);
    m->emplace(make_pair(id, s));
  }
  else
  {
    Student *s = it->second;
    cout << s->GetName() << " " << s->GetAge() << " " << s->GetId() << endl;
  }
  }
};
int main(void)
{
  FwFactory *f = new FwFactory;
  f->GetPerson(1);
  f->GetPerson(2);
  f->GetPerson(3);
  cout << "**************" << endl;
  f->GetPerson(2);
  return 0;
}


二 桥接模式


0eacb84100b54626af849e6b562bf92a.png


将抽象和实现分离,使他们可以独立变化。
基于类的最小设计原则
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Phone
{
public:
  virtual void func() = 0;
};
class Iphone :public Phone
{
public:
  void func()
  {
  cout << "this is IPHONE...." << endl;
  }
};
class HUAWEI :public Phone
{
public:
  void func()
  {
  cout << "this is HUAWEI" << endl;
  }
};
class Soft  //实现两种从产品的链接,把不同的产品剥离出来,耦合度低
{
protected:
  Phone *phone;
public:
  virtual void func() = 0;
};
class QQ :public Soft 
{
public:
  QQ(Phone *p)
  {
  phone = p;
  }
  void func()
  {
  phone->func();
  cout << "this is QQ" << endl;
  }
};
class Vchat :public Soft
{
public:
  Vchat(Phone *p)
  {
  phone = p;
  }
  void func()
  {
  phone->func();
  cout << "this is Vchat" << endl;
  }
};
int main(void)
{
  Phone *p = new Iphone;
  Soft *a = new QQ(p);
  a->func();
  return 0;
}


三 外观模式


提供一个简单一致的界面,为了系统中统一的一套接口


#include <iostream>
#include <map>
#include <string>
using namespace std;
class SystemA
{
public:
  void func()
  {
  cout << "this is A" << endl;
  }
};
class SystemB
{
public:
  void func()
  {
  cout << "this is B" << endl;
  }
};
class SystemC
{
public:
  void func()
  {
  cout << "this is C" << endl;
  }
};
class Facade
{
private:
  SystemA *a;
  SystemB *b;
  SystemC *c;
public:
  Facade()
  {
  a = new SystemA;
  b = new SystemB;
  c = new SystemC;
  }
  ~Facade()
  {
  delete a;
  delete b;
  delete c;
  }
  void func()
  {
  a->func();
  b->func();
  c->func();
  }
};
int main(void)
{
  Facade *f = new Facade;
  f->func();
  return 0;
}


四 观察者模式


当对象存在一对多的关系的时候,使用观察者模式,当一个对象被修改的时候,则会自动通知
依赖他的所有对象。
#include <iostream>
#include <map>
#include <string>
#include <list>
using namespace std;
class Infom;
class Observer  //观察者 员工
{
public:
  virtual void Subscribe(Infom *i) = 0;  //订阅
  virtual void UnSubscribe(Infom *i) = 0;  //取消订阅
  virtual void Update(string str) = 0;  //更新状态
};
class Infom  //通知者  秘书
{
public:
  virtual void Add(Observer *o) = 0;
  virtual void Remove(Observer *o) = 0;
  virtual void Notify(string str) = 0;
};
class Secretary :public Infom
{
private:
  list <Observer *> l;
public:
  void Add(Observer *o)
  {
  l.emplace_back(o);
  }
  void Remove(Observer *o)
  {
  l.remove(o);
  }
  void Notify(string str)
  {
  for (auto &it : l)
  {
    it->Update(str);
  }
  }
};
class Staff :public Observer
{
public:
  virtual void Subscribe(Infom *i)  //订阅
  {
  i->Add(this);
  }
  virtual void UnSubscribe(Infom *i) //取消订阅
  {
  i->Remove(this);
  }
  virtual void Update(string str)  //更新状态
  {
  if (str == "WARNING")
  {
    cout << "boss is comming" << endl;
  }
  else
  {
    cout << "continue to play games" << endl;
  }
  }
};
int main(void)
{
  Infom *i = new Secretary;
  Observer *o1 = new Staff;
  Observer *o2 = new Staff;
  Observer *o3 = new Staff;
  Observer *o4 = new Staff;
  o1->Subscribe(i);
  o2->Subscribe(i);
  o3->Subscribe(i);
  o4->Subscribe(i);
  i->Notify("WARNING");
  return 0;
}
相关文章
|
3月前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
91 1
|
3月前
|
算法 Linux 程序员
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
|
3月前
|
数据处理 C++ UED
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
89 0
|
3月前
|
C语言 数据安全/隐私保护 C++
嵌入式中如何把C++代码改写成C语言代码
嵌入式中如何把C++代码改写成C语言代码
51 0
|
3月前
|
存储 缓存 Java
嵌入式系统中C++内存管理基本方法
嵌入式系统中C++内存管理基本方法
113 0
|
3月前
|
存储 编译器 程序员
嵌入式系统中C++基础知识精髓
嵌入式系统中C++基础知识精髓
79 0
|
3月前
|
关系型数据库 数据库 C++
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
|
3月前
|
存储 编译器 C++
嵌入式中C++ 编程习惯与编程要点分析
嵌入式中C++ 编程习惯与编程要点分析
32 1
|
3月前
|
架构师 数据挖掘 程序员
嵌入式系统中C++ 类的设计和实现分析
嵌入式系统中C++ 类的设计和实现分析
48 1
|
3月前
|
算法 小程序 编译器
嵌入式中C++开发的基本操作方法
嵌入式中C++开发的基本操作方法
38 0