当前位置: 首页 > news >正文

怎么做可以聊天的网站吗网站建设术语解释

怎么做可以聊天的网站吗,网站建设术语解释,网页游戏修改器,设计本官方网站案例设计模式 文章目录 设计模式创建型模式单例模式 [1-小明的购物车](https://kamacoder.com/problempage.php?pid1074)工厂模式 [2-积木工厂](https://kamacoder.com/problempage.php?pid1076)抽象⼯⼚模式 [3-家具工厂](https://kamacoder.com/problempage.php?pid1077)建造者…设计模式 文章目录 设计模式创建型模式单例模式 [1-小明的购物车](https://kamacoder.com/problempage.php?pid1074)工厂模式 [2-积木工厂](https://kamacoder.com/problempage.php?pid1076)抽象⼯⼚模式 [3-家具工厂](https://kamacoder.com/problempage.php?pid1077)建造者模式 [4-⾃⾏⻋加⼯](https://kamacoder.com/problempage.php?pid1077)原型模式 [5-矩形原型](https://kamacoder.com/problempage.php?pid1083) 创建型模式 单例模式 1-小明的购物车 单例模式是⼀种创建型设计模式 它的核⼼思想是保证⼀个类只有⼀个实例并提供⼀个全局访问点来访问这个实 例。 只有⼀个实例的意思是在整个应⽤程序中进程只存在该类的⼀个实例对象⽽不是创建多个相同类型的对象。全局访问点的意思是为了让其他类能够获取到这个唯⼀实例该类提供了⼀个全局访问点通常是⼀个静态 ⽅法通过这个⽅法就能获得实例 #include iostream #include unordered_map #include vector #includemutex #includeatomicclass ShoppingCartManager { private:std::unordered_mapstd::string, int cart;std::vectorstd::string order; // 保持顺序// 私有构造函数ShoppingCartManager() {}static std::mutex m_mutex;//static ShoppingCartManager* m_cart;static std::atomicShoppingCartManager* m_atomic; //使用原子变量将对象指针保存public:// 获取购物车实例static ShoppingCartManager* getInstance() {ShoppingCartManager* m_cart m_atomic.load();if (m_cart nullptr) {m_mutex.lock();m_cart m_atomic.load();if (m_cart nullptr) {m_cart new ShoppingCartManager;m_atomic.store(m_cart); //保存对象指针}m_mutex.unlock();}return m_cart;}// 添加商品void addToCart(const std::string itemName, int quantity) {if (cart.find(itemName) cart.end()) { //如果cart里面没有该货物order.push_back(itemName); //注意这里是order为了使得货物和数量能对齐}cart[itemName] quantity;}// 查看商品void viewCart() const {for (const auto itemName : order) { //这里按照顺序读取货物保证有序输出若只用unordered_map无法保证有序std::cout itemName cart.at(itemName) std::endl;}} };//ShoppingCartManager* ShoppingCartManager::m_cart nullptr; std::atomicShoppingCartManager* ShoppingCartManager::m_atomic; std::mutex ShoppingCartManager::m_mutex;int main() {std::string itemName;int quantity;ShoppingCartManager* cart ShoppingCartManager::getInstance();while (std::cin itemName quantity) {cart-addToCart(itemName, quantity);}cart-viewCart();return 0; }使用静态局部变量保证多线程下的资源安全 工厂模式 2-积木工厂 ⼯⼚⽅法模式也是⼀种创建型设计模式简单⼯⼚模式只有⼀个⼯⼚类负责创建所有产品如果要添加新的产品通常需要修改⼯⼚类的代码。⽽⼯⼚⽅法模式引⼊了抽象⼯⼚和具体⼯⼚的概念每个具体⼯⼚只负责创建⼀个具体产品添加新的产品只需要添加新的⼯⼚类⽽⽆需修改原来的代码这样就使得产品的⽣产更加灵活⽀持扩展符合开闭原则。 ⼯⼚⽅法模式分为以下⼏个⻆⾊ 抽象⼯⼚⼀个接⼝包含⼀个 抽象的⼯⼚⽅法⽤于创建产品对象。具体⼯⼚实现抽象⼯⼚接⼝创建具体的产品。抽象产品定义产品的接⼝。具体产品实现抽象产品接⼝是⼯⼚创建的对象。 // 表驱动 lambda 表达式#includeiostream #includebits/stdc.husing namespace std;class Shape { public:virtual void show() 0; };class Circle : public Shape { public:void show() {cout Circle Block endl;} };class Square : public Shape { public:void show () {cout Square Block endl;} };class ShapeFactory { public:virtual Shape* produce() 0; };class CircleFactory : public ShapeFactory { public:Shape* produce() {return new Circle();} };class SquareFactory : public ShapeFactory { public:Shape* produce() {return new Square();} };unordered_mapstring, functionShapeFactory*() m {{Circle, [](){ return new CircleFactory();}},{Square, [](){ return new SquareFactory();}}, };class BlocksFactory { public:vectorShape* blocks;void CreateBlocks(string type, int count) {ShapeFactory* sf m[type]();Shape* s sf - produce();for (int i 0; i count; i) {blocks.push_back(s);}}void Print() {for (const auto s : blocks) {s - show();}} };int main() {int produceTime;cin produceTime;string type;int count;BlocksFactory* bf new BlocksFactory();for (int i 0; i produceTime; i) {cin type count;bf-CreateBlocks(type, count);}bf-Print();return 0; }抽象⼯⼚模式 3-家具工厂 抽象⼯⼚模式包含多个抽象产品接⼝多个具体产品类⼀个抽象⼯⼚接⼝和多个具体⼯⼚每个具体⼯⼚负责创建⼀组相关的产品。 抽象产品接⼝ AbstractProduct : 定义产品的接⼝可以定义多个抽象产品接⼝⽐如说沙发、椅⼦、茶⼏都是抽象产品。具体产品类 ConcreteProduct : 实现抽象产品接⼝产品的具体实现古典⻛格和沙发和现代⻛格的沙发都是具体产品。抽象⼯⼚接⼝AbstractFactory : 声明⼀组⽤于创建产品的⽅法每个⽅法对应⼀个产品。具体⼯⼚类 ConcreteFactory 实现抽象⼯⼚接⼝,负责创建⼀组具体产品的对象在本例中⽣产古典⻛格的⼯⼚和⽣产现代⻛格的⼯⼚都是具体实例. #include iostream #include string #include unordered_map #include functionalusing namespace std;class Sofa { public:virtual void info() 0; };class ModernSofa : public Sofa { public:void info() override {cout modern sofa endl;}; };class ClassicalSofa : public Sofa { public:void info() override {cout classical sofa endl;}; };class Chair { public:virtual void info() 0; };class ModernChair : public Chair { public:void info() override {cout modern chair endl;}; };class ClassicalChair : public Chair { public:void info() override {cout classical chair endl;}; };class Factory { public:virtual Sofa *buildSofa() 0;virtual Chair *buildChair() 0; };class ModernFactory : public Factory { public:Sofa *buildSofa() override{return new ModernSofa();}Chair *buildChair() override{return new ModernChair();} };class ClassicalFactory : public Factory { public:Sofa *buildSofa() override{return new ClassicalSofa();}Chair *buildChair() override{return new ClassicalChair();} };unordered_mapstring, functionFactory*() factoryTable {{modern, [](){ return new ModernFactory(); }},{classical, [](){ return new ClassicalFactory(); }} };int main() {int loop;cin loop;string type;while (loop--){cin type;Factory *f factoryTable[type]();Chair *c f-buildChair();Sofa *s f-buildSofa();c-info();s-info();}return 0; }建造者模式 4-⾃⾏⻋加⼯ 建造者模式也被成为⽣成器模式是⼀种创建型设计模式软件开发过程中有的时候需要创建很复杂的对象⽽建造者模式的主要思想是将对象的构建过程分为多个步骤并为每个步骤定义⼀个抽象的接⼝。具体的构建过程由实现了这些接⼝的具体建造者类来完成。同时有⼀个指导者类负责协调建造者的⼯作按照⼀定的顺序或逻辑来执⾏构建步骤最终⽣成产品。 产品Product被构建的复杂对象, 包含多个组成部分。抽象建造者 Builder : 定义构建产品各个部分的抽象接⼝和⼀个返回复杂产品的⽅法具体建造者 getResult Concrete Builder 实现抽象建造者接⼝构建产品的各个组成部分并提供⼀个⽅法返回最终的产品。指导者 Director 调⽤具体建造者的⽅法按照⼀定的顺序或逻辑来构建产品。 使⽤建造者模式有下⾯⼏处优点 使⽤建造者模式可以将⼀个复杂对象的构建与其表示分离通过将构建复杂对象的过程抽象出来可以使客户端代码与具体的构建过程解耦同样的构建过程可以创建不同的表示可以有多个具体的建造者(相互独⽴可以更加灵活地创建不同组合的对象 #include iostream #include string #include unordered_map #include functionalusing namespace std;class Sofa { public:virtual void info() 0; };class ModernSofa : public Sofa { public:void info() override {cout modern sofa endl;}; };class ClassicalSofa : public Sofa { public:void info() override {cout classical sofa endl;}; };class Chair { public:virtual void info() 0; };class ModernChair : public Chair { public:void info() override {cout modern chair endl;}; };class ClassicalChair : public Chair { public:void info() override {cout classical chair endl;}; };class Factory { public:virtual Sofa *buildSofa() 0;virtual Chair *buildChair() 0; };class ModernFactory : public Factory { public:Sofa *buildSofa() override{return new ModernSofa();}Chair *buildChair() override{return new ModernChair();} };class ClassicalFactory : public Factory { public:Sofa *buildSofa() override{return new ClassicalSofa();}Chair *buildChair() override{return new ClassicalChair();} };unordered_mapstring, functionFactory*() factoryTable {{modern, [](){ return new ModernFactory(); }},{classical, [](){ return new ClassicalFactory(); }} };int main() {int loop;cin loop;string type;while (loop--){cin type;Factory *f factoryTable[type]();Chair *c f-buildChair();Sofa *s f-buildSofa();c-info();s-info();}return 0; }原型模式 5-矩形原型 如果⼀个对象的创建过程⽐较复杂时⽐如需要经过⼀系列的计算和资源消耗那每次创建该对象都需要消耗资源⽽通过原型模式就可以复制现有的⼀个对象来迅速创建/克隆⼀个新对象不必关⼼具体的创建细节可以降低对象创建的成本。 原型模式的基本结构 实现原型模式需要给【原型对象】声明⼀个克隆⽅法执⾏该⽅法会创建⼀个当前类的新对象并将原始对象中的成员变量复制到新⽣成的对象中⽽不必实例化。并且在这个过程中只需要调⽤原型对象的克隆⽅法⽽⽆需知道原型对象的具体类型。 原型模式包含两个重点模块 抽象原型接⼝ prototype : 声明⼀个克隆⾃身的⽅法具体原型类 ConcretePrototype : 实现 clone ⽅法复制当前对象并返回⼀个新对象。在客户端代码中可以声明⼀个具体原型类的对象然后调⽤clone() ⽅法复制原对象⽣成⼀个新的对象 #includeiostream #includestring using namespace std;class Prototype { public:virtual Prototype* clone() 0;virtual void print() 0;virtual ~Prototype() {} };class Triangle : public Prototype{ private:string color;int width, height; public:Triangle(const string color, int width, int height): color(color), width(width), height(height) {}Prototype* clone() override {return new Triangle(this-color, this-width, this-height);}void print() override {coutColor: this-color, Width: this-width, Height: this-heightendl;} };int main(){int n;string color;int w,h;cin color w h;cin n;while(n--){Prototype* original new Triangle(color, w, h);Prototype* cloned original-clone();cloned-print();delete original;delete cloned;}return 0; }
http://www.hkea.cn/news/14580087/

相关文章:

  • 济南网站建设工作室西安网页设计培训费用
  • 无锡网站关键词优化程序员做个网站要多少钱呢
  • 网站建设方案设计是什么意思做网站看什么书
  • 摄影师 网站 模板c 网站开发案例详解光盘
  • 如何做网站流量买卖工业设计师年薪60万
  • 软件网站开发团队名称网站内部链接怎麽做
  • 天津中小企业网站制作东营网站开发招聘
  • 做网站用什么开源python是什么
  • 网站外链建设可以提升网站做兼职在线抠图网站
  • 做网站程序看什么书装修网站怎么做的好
  • 域名解析到服务器ip然后服务器怎么弄才能跳转到那个网站建设百度网站多少钱
  • 网站建设中的英文无锡高端网站建设
  • 米趋外贸网站建设室内装饰设计培训机构
  • dtcms网站开发教程法治网站的建设整改措施
  • 服装设计师参考的网站网站建设用户需求调查
  • 为公司做的图可以上传网站吗云服务器 wordpress
  • 什么网站值得做上海设计网站方法
  • 网站更改备案信息在哪维拓设计在北京排名
  • 怎么做公司网站制作亚马逊 怎么做国外网站
  • 常州外贸网站建设vi设计包含哪些内容
  • 用php做视频网站有哪些dw下载手机版
  • qq自动发货平台网站怎么做茂名网站制作计划
  • 织梦做企业网站教程肥西网站推广公司
  • 网站开发课程心得做网站go和php用哪个好
  • 如何在社交网站上做视频推广深圳做网站seo
  • 叫别人做网站需要注意什么郑州计算机网站公司
  • 建网站赚钱吗网站建设教程资源
  • 中国职业培训在线沧州网站推广优化商集客电话
  • 学校建设网站重要性权威的扬中网站建设
  • 保康网站建设开个网址多少钱