init commit

This commit is contained in:
2024-03-19 01:05:51 +08:00
commit 199bbf2628
393 changed files with 34883 additions and 0 deletions

View File

@ -0,0 +1,80 @@
#include <iostream>
using namespace std;
class Encode1
{
public:
void print()
{
cout << "编码1\n";
}
};
class Encode2
{
public:
void printf()
{
cout << "编码2\n";
}
};
class Encrypt
{
public:
virtual void Encode() = 0;
};
class Encode1Adapter : public Encrypt
{
private:
Encode1 *encode;
public:
Encode1Adapter(Encode1 *e)
{
encode = e;
}
void Encode()
{
encode->print();
}
};
class ClassEncode2Adapter : public Encode2, public Encrypt
{
public:
void Encode()
{
this->printf();
}
};
class User
{
private:
Encrypt *encrypt;
public:
User(Encrypt *e)
{
this->encrypt = e;
}
void Use()
{
this->encrypt->Encode();
}
};
int main()
{
//对象适配器
Encrypt *e = new Encode1Adapter(new Encode1());
User *u = new User(e);
u->Use();
//类适配器
e = new ClassEncode2Adapter();
u = new User(e);
u->Use();
return 0;
}

View File

@ -0,0 +1,66 @@
#include <iostream>
using namespace std;
class DB
{
public:
virtual string data() = 0;
};
class File
{
protected:
DB *db;
public:
void setDb(DB *db)
{
this->db = db;
}
virtual void print() = 0;
};
class Mysql : public DB
{
public:
string data()
{
return "mysql";
};
};
class Oracle:public DB{
public:
string data(){
return "oracle";
}
};
class Txt : public File
{
public:
void print()
{
cout << "" << this->db->data() << "提取数据转换为TXT格式\n";
}
};
class Pdf:public File{
public:
void print()
{
cout << "" << this->db->data() << "提取数据转换为PDF格式\n";
}
};
int main()
{
DB *db = new Mysql();//选择数据库
File *file=new Txt();//选择输出格式
file->setDb(db);
file->print();
file=new Pdf();
db=new Oracle();
file->setDb(db);
file->print();
return 0;
}

View File

@ -0,0 +1,109 @@
#include <iostream>
using namespace std;
class PlayWindow
{
public:
string menu;
string list;
string window;
string control;
void print()
{
cout << "菜单:" << menu << "\t列表:" << list << "\t窗口:" << window << "\t控制条:" << control << "\n";
}
};
class UI
{
protected:
PlayWindow *window;
public:
UI()
{
this->window = new PlayWindow;
}
virtual void buildMenu() = 0;
virtual void buildList() = 0;
virtual void buildWindow() = 0;
virtual void buildControl() = 0;
PlayWindow *create()
{
return this->window;
}
};
class Complete : public UI
{
public:
Complete() : UI() {}
void buildMenu()
{
this->window->menu = "菜单";
}
void buildList()
{
this->window->list = "播放菜单";
}
void buildWindow()
{
this->window->window = "完整模式";
}
void buildControl()
{
this->window->control = "控制条";
}
};
class Thin : public UI
{
public:
Thin() : UI() {}
void buildMenu()
{
this->window->menu = "";
}
void buildList()
{
this->window->list = "";
}
void buildWindow()
{
this->window->window = "精简窗口";
}
void buildControl()
{
this->window->control = "精简控制条";
}
};
// 其它模式窗口
class UIController
{
private:
UI *ui;
public:
UIController(UI *ui)
{
this->ui = ui;
}
PlayWindow *build()
{
this->ui->buildControl();
this->ui->buildWindow();
this->ui->buildList();
this->ui->buildMenu();
return this->ui->create();
}
};
int main()
{
UI *ui = new Complete(); //窗口样式
UIController *uictl = new UIController(ui);
PlayWindow *window = uictl->build();
window->print();
return 0;
}

View File

@ -0,0 +1,106 @@
#include <iostream>
#include <unordered_map>
using namespace std;
class Operate
{
public:
void Open()
{
cout << "打开" << endl;
}
void Create()
{
cout << "创建\n";
}
void Edit()
{
cout << "编辑\n";
}
};
class Command
{
public:
virtual void execute() = 0;
};
class OpenCommand : public Command
{
Operate oper;
public:
void execute()
{
oper.Open();
}
};
class EditCommand : public Command
{
Operate oper;
public:
void execute()
{
oper.Edit();
}
};
class CreateCommand : public Command
{
Operate oper;
public:
void execute()
{
oper.Create();
}
};
class MenuItem
{
Command *command;
public:
void setCommand(Command *command)
{
this->command = command;
}
void click()
{
command->execute();
}
};
class Menu
{
unordered_map<string, MenuItem *> items;
public:
void add(string name, MenuItem *m)
{
items[name] = m;
}
void click(string name)
{
items[name]->click();
}
};
int main()
{
Menu *menu = new Menu();
MenuItem *open = new MenuItem(), *create = new MenuItem(), *edit = new MenuItem();
open->setCommand(new OpenCommand());
menu->add("open", open);
create->setCommand(new CreateCommand());
menu->add("create", create);
edit->setCommand(new EditCommand());
menu->add("edit", edit);
menu->click("open");
menu->click("create");
menu->click("edit");
return 0;
}

View File

@ -0,0 +1,84 @@
#include <iostream>
#include <vector>
using namespace std;
class Component
{
public:
virtual void click() = 0;
};
class Button : public Component
{
public:
void click()
{
cout << "按钮按下\n";
}
};
class Text : public Component
{
public:
void click()
{
cout << "文本框按下\n";
}
};
class Window : public Component
{
protected:
vector<Component *> vec;
public:
void add(Component *component)
{
vec.push_back(component);
}
void remove(Component *component)
{
for (int i = 0; i < vec.size(); i++)
{
if (vec[i] == component)
{
vec.erase(vec.begin() + i);
}
}
}
void click()
{
cout << "窗口按下\n";
for (int i = 0; i < vec.size(); i++)
{
vec[i]->click();
}
}
};
class Container : public Window
{
public:
void click()
{
cout << "容器按下\n";
for (int i = 0; i < vec.size(); i++)
{
vec[i]->click();
}
}
};
int main()
{
Window *w = new Window();
w->add(new Text());
w->add(new Button());
Container *c = new Container();
w->add(c);
c->add(new Button());
c->add(new Text());
c->add(new Button());
w->click();
cout << "\n\n";
c->click();
return 0;
}

View File

@ -0,0 +1,69 @@
#include <iostream>
using namespace std;
class text
{
public:
virtual void display() = 0;
};
class original : public text
{
public:
void display()
{
cout << "简单加密\n";
}
};
class encrypt : public text
{
protected:
text *t;
public:
encrypt(text *t)
{
this->t = t;
}
void display()
{
t->display();
}
};
class rsa : public encrypt
{
public:
rsa(text *t) : encrypt(t)
{
}
void display()
{
encrypt::display();
cout << "rsa加密\n";
}
};
class bit : public encrypt
{
public:
bit(text *t) : encrypt(t)
{
}
void display()
{
encrypt::display();
cout << "位加密\n";
}
};
int main()
{
text *a, *b, *c;
a = new original();
b = new rsa(a);
c = new bit(b);
c->display();
return 0;
}

View File

@ -0,0 +1,79 @@
#include <iostream>
using namespace std;
class A
{
public:
void printA()
{
cout << "A\n";
}
};
class B
{
public:
void printB()
{
cout << "B\n";
}
};
class C
{
public:
void printC()
{
cout << "C\n";
}
};
class AbstractSystemFacade
{
public:
virtual void Func() = 0;
};
class ABFacade : public AbstractSystemFacade
{
A *a;
B *b;
public:
ABFacade()
{
a = new A();
b = new B();
}
void Func()
{
a->printA();
b->printB();
}
};
class ACFacade : public AbstractSystemFacade
{
A *a;
C *c;
public:
ACFacade()
{
a = new A();
c = new C();
}
void Func()
{
a->printA();
c->printC();
}
};
int main()
{
AbstractSystemFacade *f = new ABFacade();
f->Func();
f=new ACFacade();
f->Func();
return 0;
}

View File

@ -0,0 +1,62 @@
#include <iostream>
#include <string>
using namespace std;
class Init
{
public:
virtual void init() = 0;
};
class ArrowKeys
{
public:
virtual void arrowKeys() = 0;
};
class SystemFactory
{
public:
virtual Init *CreateInit() = 0;
virtual ArrowKeys *CreateArrowKeys() = 0;
};
class AndroidInit : public Init
{
public:
void init()
{
cout << "安卓初始化" << endl;
}
};
class AndroidArrowKeys : public ArrowKeys
{
public:
void arrowKeys()
{
cout << "安卓方向键" << endl;
}
};
class AndroidFactory : public SystemFactory
{
public:
Init *CreateInit()
{
return new AndroidInit();
}
ArrowKeys *CreateArrowKeys()
{
return new AndroidArrowKeys();
}
};
int main()
{
SystemFactory *s = new AndroidFactory();
s->CreateInit()->init();
s->CreateArrowKeys()->arrowKeys();
return 0;
}

View File

@ -0,0 +1,91 @@
#include <iostream>
#include <string>
using namespace std;
class Image
{
public:
virtual void read() = 0;
};
class ImageFactory
{
public:
virtual Image *create() = 0;
virtual void read() = 0;
};
class JpgImage : public Image
{
public:
string filename;
public:
JpgImage(string filename)
{
this->filename = filename;
}
void read()
{
cout << "读取jpg文件:" << this->filename << endl;
}
};
class PngImage : public Image
{
public:
void read()
{
cout << "读取png图片" << endl;
}
};
class JpgImageFactory : public ImageFactory
{
private:
Image *img;
public:
Image *create()
{
this->img = new JpgImage("file-name");
return this->img;
}
void read()
{
img->read();
}
};
class PngImageFactory : public ImageFactory
{
private:
Image *img;
public:
Image *create()
{
this->img = new PngImage();
return this->img;
}
void read()
{
img->read();
}
};
int main()
{
ImageFactory *factory = new JpgImageFactory();
Image *img = factory->create();
img->read();
factory->read();
factory = new PngImageFactory();
img = factory->create();
img->read();
factory->read();
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -0,0 +1,8 @@
## 简单工厂模式
> 一个工厂生产多个产品
## 工厂方法模式
> 多个工厂生产多个产品
![](img/1.png)

View File

@ -0,0 +1,52 @@
#include <iostream>
#include <string>
class Graph
{
public:
virtual void show() = 0;
};
class Triangle : public Graph
{
public:
void show()
{
std::cout << "三角形" << std::endl;
}
};
class Round : public Graph
{
public:
void show()
{
std::cout << "圆形" << std::endl;
}
};
class GraphFactory
{
public:
static Graph *CreateGraph(std::string name)
{
if (name == "triangle")
{
return new Triangle();
}
else if (name == "round")
{
return new Round();
}
return NULL;
}
};
int main()
{
Graph *a = GraphFactory::CreateGraph("triangle");
a->show();
a = GraphFactory::CreateGraph("round");
a->show();
return 0;
}

View File

@ -0,0 +1,98 @@
#include <iostream>
#include <unordered_map>
using namespace std;
class info
{
public:
int x;
int y;
info(int x, int y)
{
this->x = x;
this->y = y;
}
};
class resource
{
public:
virtual void display(info *) = 0;
};
class img : public resource
{
string image;
public:
img(string img)
{
this->image = img;
}
void display(info *i)
{
cout << "图片:" << image << "位置:" << i->x << "," << i->y << endl;
}
};
class video : public resource
{
string v;
public:
video(string video)
{
this->v = video;
}
void display(info *i)
{
cout << "视频:" << v << "位置:" << i->x << "," << i->y << endl;
}
};
class resourceFactory
{
protected:
static unordered_map<string, resource *> map;
static resourceFactory *res;
public:
static resourceFactory *getResourceFactory()
{
return resourceFactory::res;
}
resource *getResource(string key)
{
if (map.find(key) == map.end())
{
return NULL;
}
return map[key];
}
void add(string key, resource *res)
{
map[key] = res;
}
};
unordered_map<string, resource *> resourceFactory::map;
resourceFactory *resourceFactory::res = new resourceFactory();
int main()
{
resource *img1 = new img("1"), *img2 = new img("2");
resource *video1 = new video("1"), *video2 = new video("2");
resourceFactory *factory = resourceFactory::getResourceFactory();
factory->add("img1", img1);
factory->add("img2", img2);
factory->add("video1", video1);
factory->add("video2", video2);
factory->getResource("img1")->display(new info(1, 2));
factory->getResource("video1")->display(new info(4, 2));
return 0;
}

View File

@ -0,0 +1,104 @@
#include <iostream>
using namespace std;
//expression=command';'*
//command=action object 'from' databse 'to' database
//action='copy'|'move'
//object='view'|'table' name
//database=string
class AbstractExpression
{
public:
virtual string interpert() = 0;
};
class Object : public AbstractExpression
{
string str;
public:
Object(string str)
{
this->str=str;
}
string interpert()
{
if (str.substr(0, 4) == "view")
{
return "视图";
}
else
{
return "" + str.substr(6);
}
}
};
class Database : public AbstractExpression
{
string db;
public:
Database(string db)
{
this->db = db;
}
string interpert()
{
return db;
}
};
class Action : public AbstractExpression
{
string action;
public:
Action(string action)
{
this->action = action;
}
string interpert()
{
if (action == "copy")
{
return "复制";
}
else
{
return "移动";
}
}
};
class Command : public AbstractExpression
{
AbstractExpression *action;
AbstractExpression *object;
AbstractExpression *db1;
AbstractExpression *db2;
public:
Command(AbstractExpression *action,
AbstractExpression *object,
AbstractExpression *db1,
AbstractExpression *db2)
{
this->action = action;
this->object = object;
this->db1 = db1;
this->db2 = db2;
}
string interpert()
{
return "" + db1->interpert() + "" + action->interpert() + object->interpert() + "" + db2->interpert();
}
};
int main()
{
string cmd = "copy view from db1 to db2;move table a from db1 to db2;";
AbstractExpression *exp = new Command(new Action("copy"), new Object("view"), new Database("db1"), new Database("db2"));
cout << exp->interpert() << endl;
return 0;
}

View File

@ -0,0 +1,102 @@
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
class AbstractExpression
{
public:
virtual int value() = 0;
};
class ValueExpression : public AbstractExpression
{
int val;
public:
ValueExpression(string val)
{
this->val = atoi(val.c_str());
}
int value()
{
return this->val;
}
};
class SymbolExpression : public AbstractExpression
{
AbstractExpression *left, *right;
string symbol;
public:
SymbolExpression(string symbol, AbstractExpression *left, AbstractExpression *right)
{
this->symbol = symbol;
this->left = left;
this->right = right;
}
int value()
{
// 非加即减
if (symbol == "+")
{
return left->value() + right->value();
}
else
{
return left->value() - right->value();
}
}
};
//expression=value|operator
//value=number
//operator='+'|'-'
int main()
{
char str[] = "1+4+5+6-7";
AbstractExpression *exp = new SymbolExpression("+", new ValueExpression("1"), new SymbolExpression("+", new ValueExpression("4"), new SymbolExpression("+", new ValueExpression("5"), new SymbolExpression("-", new ValueExpression("6"), new ValueExpression("7"))))); //反了
cout << "手动构建:" << exp->value() << endl;
stack<AbstractExpression *> stack;
AbstractExpression *expression;
for (int i = 0; i < strlen(str); i++)
{
if (str[i] >= '0')
{
string val;
for (; i < strlen(str); i++)
{
if (str[i] < '0')
{
break;
}
val += str[i];
}
i--;
stack.push(new ValueExpression(val));
}
else
{
string symbol;
symbol += str[i++];
AbstractExpression *exp = stack.top();
stack.pop();
string val;
for (; i < strlen(str); i++)
{
if (str[i] < '0')
{
break;
}
val += str[i];
}
i--;
stack.push(new SymbolExpression(symbol, exp, new ValueExpression(val)));
}
}
expression = stack.top();
cout << "value:" << expression->value() << endl;
return 0;
}

View File

@ -0,0 +1,71 @@
#include <iostream>
#include <vector>
using namespace std;
class AbstractIterator
{
public:
virtual void *next() = 0;
};
class PageIterator : public AbstractIterator
{
protected:
vector<void *> v;
int pos = 0;
public:
PageIterator(vector<void *> v)
{
this->v = v;
}
void *next()
{
if (pos >= v.size())
{
return NULL;
}
return v[pos++];
}
};
class List
{
protected:
vector<void *> v;
public:
void add(void *data)
{
v.insert(v.end(), data);
}
AbstractIterator *page(int page)
{
vector<void *> p(v.begin() + ((page - 1) * 5), v.begin() + (page * 5));
return new PageIterator(p);
}
};
int main()
{
List *list = new List();
for (int i = 0; i < 8; i++)
{
int *tmp = (int *)malloc(sizeof(int));
*tmp = i;
list->add(tmp);
}
AbstractIterator *iterator = list->page(1);
void *next;
cout << "page1:\n";
while ((next = iterator->next()) != NULL)
{
cout << *(int *)next << "\t";
}
cout << "\npage2:\n";
iterator = list->page(2);
while ((next = iterator->next()) != NULL)
{
cout << *(int *)next << "\t";
}
return 0;
}

View File

@ -0,0 +1,85 @@
#include <iostream>
#include <vector>
using namespace std;
class Observer
{
public:
virtual void sell(int) = 0;
};
class Subject
{
protected:
vector<Observer *> vec;
public:
void add(Observer *o)
{
vec.push_back(o);
}
virtual void notify(int) = 0;
};
class DJIA : public Subject
{
public:
void notify(int price)
{
cout << "道琼斯发生了熔断,通知" << endl;
for (int i = 0; i < vec.size(); i++)
{
vec[i]->sell(price);
}
}
};
class SSE : public Subject
{
public:
void notify(int price)
{
cout << "上证突破3000点了,通知" << endl;
for (int i = 0; i < vec.size(); i++)
{
vec[i]->sell(price);
}
}
};
class Chives : public Observer
{
string name;
public:
Chives(string name)
{
this->name = name;
}
void sell(int price)
{
cout << name << ":买就行了,加仓干\n";
}
};
class Dalao : public Observer
{
public:
void sell(int price)
{
cout << "大佬:分析一下\n";
};
};
int main()
{
Subject *a = new DJIA(), *b = new SSE();
Observer *c1 = new Chives("韭菜1"), *c2 = new Chives("韭菜2"), *dl = new Dalao();
a->add(c1);
a->add(dl);
b->add(c2);
a->notify(10000);
b->notify(3000);
return 0;
}

View File

@ -0,0 +1,53 @@
#include <iostream>
#include <string>
using namespace std;
class Object
{
public:
virtual Object *colne() { return NULL; };
virtual void display() = 0;
};
class Address
{
public:
string address;
Address(string addr)
{
this->address = addr;
}
};
class Customer : public Object
{
public:
Address *address;
string user;
Object *clone() // 深拷贝
{
Customer *a = new Customer();
a->user = this->user;
a->address = new Address(this->address->address);
return a;
}
void display()
{
cout << this->user << ":" << this->address->address << endl;
}
};
int main()
{
Customer *a = new Customer();
a->user = "test";
a->address = new Address("haha");
Customer *b = (Customer *)a->clone();
b->user = "test123";
a->display();
b->display();
return 0;
}

View File

@ -0,0 +1,42 @@
#include <iostream>
using namespace std;
class method
{
public:
void Method()
{
cout << "方法Method调用成功" << endl;
}
};
class methodProxy
{
protected:
method *m;
public:
methodProxy()
{
m = new method();
}
void Method(int uid)
{
cout << "用户" << uid << "方法Method()被调用" << time(NULL) << endl;
if (uid > 10)
{
cout << "用户" << uid << "方法Method()调用失败" << time(NULL) << endl;
return;
}
this->m->Method();
cout << "用户" << uid << "方法Method()调用结束" << time(NULL) << endl;
}
};
int main()
{
methodProxy *proxy = new methodProxy();
proxy->Method(1);
proxy->Method(11);
return 0;
}

View File

@ -0,0 +1,38 @@
#include <iostream>
using namespace std;
class Pic
{
public:
string download(string url)
{
cout << "图片下载中...";
return url;
}
};
class PicProxy
{
protected:
Pic *pic;
public:
PicProxy()
{
pic = new Pic();
}
void download(string url)
{
cout<<"显示默认图片到列表\n";
cout<<"开启线程下载图片\n";
cout<<this->pic->download(url);
}
};
int main()
{
PicProxy* p=new PicProxy();
p->download("img1");
return 0;
}

View File

@ -0,0 +1,22 @@
## 单一职责原则
一个类只负责一个功能领域中的相应职责
## 开闭原则
软件实体应对扩展开放,而对修改关闭
## 里氏替换原则
> 将父类替换为子类,逻辑依旧通过
所有引用基类对象的地方能够透明地使用其子类的对象
## 依赖倒置原则
抽象不应该依赖于细节,细节应该依赖于抽象
## 接口隔离原则
> 接口版的单一职责原则
使用多个专门的接口,而不使用单一的总接口
## 合成复用原则
尽量使用对象组合,而不是继承来达到复用的目的
## 迪米特法则
一个软件实体应当尽可能少地与其他实体发生相互作用

View File

@ -0,0 +1,89 @@
#include <iostream>
using namespace std;
class Info
{
public:
string name;
int day;
Info(string name, int day)
{
this->name = name;
this->day = day;
}
};
class Approval
{
protected:
string name;
Approval *next;
public:
Approval(string name)
{
this->name = name;
}
void setSuccessor(Approval *next)
{
this->next = next;
}
virtual void process(Info *info) = 0;
};
class Director : public Approval
{
public:
Director(string name) : Approval(name) {}
void process(Info *info)
{
if (info->day <= 3)
{
cout << this->name << "同意" << info->name << "请假" << info->day << "" << endl;
}
else
{
this->next->process(info);
}
}
};
class Manager : public Approval
{
public:
Manager(string name) : Approval(name) {}
void process(Info *info)
{
if (info->day <= 10)
{
cout << this->name << "同意" << info->name << "请假" << info->day << "" << endl;
}
else
{
this->next->process(info);
}
}
};
class Boss : public Approval
{
public:
Boss(string name) : Approval(name) {}
void process(Info *info)
{
cout << this->name << "同意" << info->name << "请假" << info->day << "" << endl;
}
};
int main()
{
Director *d = new Director("主管A");
Manager *m = new Manager("经理B");
Boss *b = new Boss("老板C");
d->setSuccessor(m);
m->setSuccessor(b);
d->process(new Info("员工A", 2));
d->process(new Info("员工B", 4));
d->process(new Info("员工C", 15));
return 0;
}