1. 程式人生 > >C++重寫《大話設計模式》中模式例項三(抽象工廠模式)

C++重寫《大話設計模式》中模式例項三(抽象工廠模式)

(宣告:如果想看例項詳細解析,請看《大話設計模式》,這裡文章只是為了加深學習設計模式印象而自己用C++程式寫一遍,以及把程式碼共享給大家。僅僅是把C#語言換成C++表述,不對書中的程式和例子是否合適做個人表述。)

UML圖:

User.h

#pragma once
#include <iostream>
#include <string>

class User {
private:
	int _id;
	std::string name;

public:
	void SetID(int id) { _id = id; }
	int GetID() { return _id; }
	void SetName(std::string name) { this->name = name; }
	std::string GetName() { return name; }
};

IUser.h

#pragma once

#include "User.h"

class IUser {
public:
	virtual void Insert(User* user) = 0;
	virtual User* GetUser(int id) = 0;
};

class SqlserverUser :public IUser {
public:
	virtual void Insert(User* user) {
		std::cout << "在SQL Server 中給User表增加一條記錄" << std::endl;
	}
	virtual User* GetUser(int id) {
		std::cout << "在SQL Server中根據ID得到User表的一條記錄" << std::endl;
		return NULL;
	}
};

class AccessUser :public IUser {
public:
	virtual void Insert(User* user) {
		std::cout << "在Access中給User表增加一條記錄" << std::endl;
	}
	virtual User* GetUser(int id) {
		std::cout << "在Access中根據ID得到User表一條記錄" << std::endl;
		return NULL;
	}
};

 Department.h

#pragma once
#include <iostream>
#include <string>

class Department {
private:
	int _id;
	std::string deptName;

public:
	void SetID(int id) { _id = id; }
	int GetID() { return _id; }
	void SetDeptName(std::string deptName) { this->deptName = deptName; }
	std::string GetDeptName() { return deptName; }
};

IDepartment.h:

#pragma once

#include "Department.h"

class IDepartment {
public:
	virtual void Insert(Department* department) = 0;
	virtual Department* GetDepartment(int id) = 0;
};

class SqlserverDepartment :public IDepartment{
public:
	virtual void Insert(Department* department) {
		std::cout << "在SQL Sever中給Department表增加一條記錄" << std::endl;
	}
	virtual Department* GetDepartment(int id) {
		std::cout << "在SQL Server中根據ID得到Department表一條記錄" << std::endl;
		return NULL;
	}
};

class AccessDepartment :public IDepartment {
public:
	virtual void Insert(Department* department) {
		std::cout << "在Access中給Department表增加一條記錄" << std::endl;
	}
	virtual Department* GetDepartment(int id) {
		std::cout << "在Access中根據ID得到Department表一條記錄" << std::endl;
		return NULL;
	}
};

 IFactory.h

#pragma once
#include "IUser.h"
#include "IDepartment.h"

class IFactory {
public:
	virtual IUser* CreateUser() = 0;
	virtual IDepartment* CreateDepartment() = 0;
};

class SqlServerFactory :public IFactory {
public:
	virtual IUser* CreateUser() {
		return new SqlserverUser();
	}
	virtual IDepartment* CreateDepartment() {
		return new SqlserverDepartment();
	}
};

class AccessFactory :public IFactory {
public:
	virtual IUser* CreateUser(){
		return new AccessUser();
	}
	virtual IDepartment* CreateDepartment() {
		return new AccessDepartment();
	}
};

 main.cpp

#include "IFactory.h"
#include <cstdlib>

int main() {
	//IFactory* factory = new SqlServerFactory();//選擇使用SqlServer資料庫
	IFactory* factory = new AccessFactory();//選擇和初始化Access資料庫
	User *user = new User();//建立使用者表
	IUser* iu = factory->CreateUser();
	iu->Insert(user);
	iu->GetUser(1);

	Department *dept = new Department();//建立部門表
	IDepartment *id = factory->CreateDepartment();
	id->Insert(dept);
	id->GetDepartment(1);

	delete dept;
	delete iu;
	delete user;
	delete factory;
	system("pause");
	return 0;
}

執行結果: