嵌入式C++(十三)(下)

简介: 嵌入式C++(十三)(下)

3.2 单例模式-懒汉式


保证一个类只能生成唯一的实例对象,也就是说,在整个程序中,只存在一个实例对象。


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mutex;
using namespace std;
class Singleton
{
private:
  static Singleton *m_instance;
  static int count;
  Singleton()
  {
  }
public:
  static Singleton *GetInstance()
  {
  if (NULL == m_instance)
  {
            cout<<"m_instance = NULL!"<<endl;
    usleep(1000);
    m_instance = new Singleton;  //全程只分配一次空间(懒汉式)
  }
  count++;
  return m_instance;
  }
  static int GetCount()
  {
  return count;
  }
  void Release()
  {
  count--;
  if (count == 0 && m_instance != NULL)
  {
    delete m_instance;
  }
  }
};
Singleton *Singleton::m_instance = NULL;
int Singleton::count = 0;
void* CreateInstance(void *arg)
{
    pthread_mutex_lock(&mutex);
    Singleton *s = Singleton::GetInstance();
    cout<<s<<endl;
    pthread_mutex_unlock(&mutex);
}
int main(void)
{
  /*Singleton *s1 = Singleton::GetInstance();
  Singleton *s2 = Singleton::GetInstance();
  Singleton *s3 = Singleton::GetInstance();
  Singleton *s4 = Singleton::GetInstance();
  Singleton *s5 = Singleton::GetInstance();
  Singleton *s6 = Singleton::GetInstance();
  cout << Singleton::GetCount() << endl;
  if (s1 == s2)
  {
  cout << "equal" << endl;
  }*/
    pthread_mutex_init(&mutex,NULL);
    pthread_t tid[10];
    int ret;
    for(int i = 0 ; i < 10;i++)
    {
        ret = pthread_create(&tid[i],NULL,CreateInstance,NULL);
        if(ret != 0)
        {
            perror("pthread_create");
        }
    }
    void *status;
    for(int i = 0;  i < 10;i++)
    {
        pthread_join(tid[i],&status);
    }
    pthread_mutex_destroy(&mutex);
  return 0;
}


3.3 单例模式-饿汉式


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <unistd.h>
#include <pthread.h>
pthread_mutex_t mutex;
using namespace std;
class Singleton
{
private:
  static Singleton *m_instance;
  static int count;
  Singleton()
  {
  }
public:
  static Singleton *GetInstance()
  {
  /*if (NULL == m_instance)
  {
            cout<<"m_instance = NULL!"<<endl;
    usleep(1000);
    m_instance = new Singleton;  //全程只分配一次空间(懒汉式)
  }*/
  count++;
  return m_instance;
  }
  static int GetCount()
  {
  return count;
  }
  void Release()
  {
  count--;
  if (count == 0 && m_instance != NULL)
  {
    delete m_instance;
  }
  }
};
Singleton *Singleton::m_instance = new Singleton;  //先分配内存空间(饿汉式)
int Singleton::count = 0;
int main(void)
{
  Singleton *s1 = Singleton::GetInstance();
  Singleton *s2 = Singleton::GetInstance();
  Singleton *s3 = Singleton::GetInstance();
  Singleton *s4 = Singleton::GetInstance();
  Singleton *s5 = Singleton::GetInstance();
  Singleton *s6 = Singleton::GetInstance();
  cout << Singleton::GetCount() << endl;
  if (s1 == s2)
  {
  cout << "equal" << endl;
  }
  return 0;
}


3.4 工厂模式


0a2653c851af460fa595bd959398a8f1.png

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <windows.h>
using namespace std;
class Fruit
{
public:
  virtual void show() = 0;
};
class Apple :public Fruit
{
public:
  void show()
  {
  cout << "this is apple" << endl;
  }
};
class Banana :public Fruit
{
public:
  void show()
  {
  cout << "this is Banana" << endl;
  }
};
class Pear :public Fruit
{
public:
  void show()
  {
  cout << "this is Pear" << endl;
  }
};
class Factory
{
public:
  virtual Fruit *Create() = 0;
};
class AppleFactory :public Factory
{
public:
  Fruit *Create()
  {
  return new Apple;
  }
};
class BananaFactory :public Factory
{
public:
  Fruit *Create()
  {
  return new Banana;
  }
};
class PearFactory :public Factory
{
public:
  Fruit *Create()
  {
  return new Pear;
  }
};
int main(void)
{
  Factory *f = new AppleFactory;   
  Fruit *fruit;
  fruit = f->Create();
  fruit->show();
  delete f;
  delete fruit;
  f = new BananaFactory;
  fruit = f->Create();
  fruit->show();
  return 0;
}


3.5 抽象工厂模式


0a2653c851af460fa595bd959398a8f1.png

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <windows.h>
using namespace std;
class Fruit
{
public:
  virtual void show() = 0;
};
class NorthApple :public Fruit
{
public:
  void show()
  {
  cout << "this is NorthApple" << endl;
  }
};
class SouthApple :public Fruit
{
public:
  void show()
  {
  cout << "this is SouthApple" << endl;
  }
};
class NorthBanana :public Fruit
{
public:
  void show()
  {
  cout << "this is NorthBanana" << endl;
  }
};
class SouthBanana :public Fruit
{
public:
  void show()
  {
  cout << "this is SouthBanana" << endl;
  }
};
class NorthPear :public Fruit
{
public:
  void show()
  {
  cout << "this is NorthPear" << endl;
  }
};
class SouthPear :public Fruit
{
public:
  void show()
  {
  cout << "this is SouthPear" << endl;
  }
};
class Factory
{
public:
  virtual Fruit *CreateApple() = 0;
  virtual Fruit *CreateBanana() = 0;
  virtual Fruit *CreatePear() = 0;
};
class NorthFactory :public Factory
{
public:
  virtual Fruit *CreateApple()
  {
  return new NorthApple;
  }
  virtual Fruit *CreateBanana()
  {
  return new NorthBanana;
  }
  virtual Fruit *CreatePear()
  {
  return new NorthPear;
  }
};
class SouthFactory :public Factory
{
public:
  virtual Fruit *CreateApple()
  {
  return new SouthApple;
  }
  virtual Fruit *CreateBanana()
  {
  return new SouthBanana;
  }
  virtual Fruit *CreatePear()
  {
  return new SouthPear;
  }
};
void Create(Factory *f)
{
  Fruit *fruit;
  fruit = f->CreateApple();
  fruit->show();
  delete fruit;
  fruit = f->CreateBanana();
  fruit->show();
  delete fruit;
}
int main(void)
{
  Factory *f = new SouthFactory;
  Create(f);
  delete f;
  f = new NorthFactory;
  Create(f);
  Fruit *f1 = new SouthApple;
  f1->show();
  return 0;
}


一、工厂模式

factory是来通过对于的工厂创建apple等水果

在创建好后,用fruit来接,并且调用

二、抽象工厂和工厂的区别

汽车可以分为轿车、SUV、MPV等,也分为奔驰、宝马等。我们可以将奔驰的所有车看作是一个产品族,而将宝马的所有车看作是另一个产品族。分别对应两个工厂,一个是奔驰的工厂,另一个是宝马的工厂。与工厂方法不同,奔驰的工厂不只是生产具体的某一个产品,而是一族产品(奔驰轿车、奔驰SUV、奔驰MPV)。“抽象工厂”的“抽象”指的是就是这个意思。 即相比于工厂方法,抽象工厂定义了一系列的产品,而不是一个产品。


3.6 建造者模式


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class House
{
private:
  string wall;
  string window;
  string door;
public:
  void SetWall(string w)
  {
  wall = w;
  }
  void SetWindow(string w)
  {
  window = w;
  }
  void SetDoor(string d)
  {
  door = d;
  }
};
class Builder
{
protected:
  House *house;
public:
  virtual void Constructor() = 0;
};
class CommonBuilder :public Builder
{
public:
  CommonBuilder(House *h)
  {
  house = h;
  }
  void Constructor()
  {
  house->SetDoor("DOOR");
  house->SetWall("WALL");
  house->SetWindow("WINDOW");
  }
};
class VilliaBuilder :public Builder
{
public:
  VilliaBuilder(House *h)
  {
  house = h;
  }
  void Constructor()
  {
  house->SetDoor("VILLIA-DOOR");
  house->SetWall("VILLIA-WALL");
  house->SetWindow("VILLIA-WINDOW");
  }
};
class Designer
{
private:
  Builder *b;
public:
  void SetBuilder(Builder *Builder)
  {
  b = Builder;
  }
  void Constructor()
  {
  b->Constructor();
  }
};
int main(void)
{
  Designer *d = new Designer;
  House *h = new House;
  Builder *b = new CommonBuilder(h);
  d->SetBuilder(b);
  d->Constructor();
  delete b;
  b = new VilliaBuilder(h);
  d->SetBuilder(b);
  d->Constructor();
  return 0;
}
相关文章
|
7月前
|
开发框架 Linux C语言
C、C++、boost、Qt在嵌入式系统开发中的使用
C、C++、boost、Qt在嵌入式系统开发中的使用
229 1
|
7月前
|
算法 Linux 程序员
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
嵌入式工程师以及C++程序员到公司就业需要掌握那些技术?
|
7月前
|
数据处理 C++ UED
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
如何作为一个嵌入式软件工程师博主获得铁粉:C/C++ 技术分享之道
125 0
|
7月前
|
C语言 数据安全/隐私保护 C++
嵌入式中如何把C++代码改写成C语言代码
嵌入式中如何把C++代码改写成C语言代码
82 0
|
7月前
|
存储 缓存 Java
嵌入式系统中C++内存管理基本方法
嵌入式系统中C++内存管理基本方法
138 0
|
7月前
|
存储 编译器 程序员
嵌入式系统中C++基础知识精髓
嵌入式系统中C++基础知识精髓
114 0
|
7月前
|
关系型数据库 数据库 C++
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
|
7月前
|
存储 编译器 C++
嵌入式中C++ 编程习惯与编程要点分析
嵌入式中C++ 编程习惯与编程要点分析
58 1
|
7月前
|
架构师 数据挖掘 程序员
嵌入式系统中C++ 类的设计和实现分析
嵌入式系统中C++ 类的设计和实现分析
75 1
|
7月前
|
算法 小程序 编译器
嵌入式中C++开发的基本操作方法
嵌入式中C++开发的基本操作方法
63 0