7月出生是什么星座| 女人辟邪带什么最好| 喝酒脸红是什么原因造成的| g18k金是什么意思| 米氮平是什么药| 尿钙是什么意思| 排卵期是什么| 小孩睡觉打呼噜是什么原因| 抗核抗体阳性对怀孕有什么影响| 大马士革是什么意思| 尿道口流脓什么病| 为什么科比叫黑曼巴| 吃完杏不能吃什么| 卵巢囊性暗区是什么意思| 年夜饭吃什么| 耳鸣脑鸣是什么原因引起的| 什么是双开| 农历六月十四是什么星座| 丁香茶有什么作用和功效| 夏占生女是什么意思| 孩子b型血父母什么血型| 农历11月25日是什么星座| 吃红薯有什么好处和坏处| 蒙羞是什么意思| 手指头红是什么原因| 胆囊结石用什么药好| 真菌感染用什么药膏| 夜间睡觉出汗是什么原因| crocodile是什么牌子| b型o型生出来的孩子什么血型| 男人鼻子大代表什么| 吃什么可以瘦肚子| 春雨绵绵是什么生肖| 什么炒菜好吃| 左肺下叶钙化灶是什么意思| 嗓子疼喝什么药| 一月底是什么星座| 八七年属兔的是什么命| 谷丙转氨酶是什么意思| 黄柏胶囊主要治什么病| 学痞是什么意思| 太阳线是什么意思| 青出于蓝是什么意思| 女性小腹疼痛是什么原因| 梵行是什么意思| 脚指甲盖凹凸不平是什么原因| 一穷二白是什么意思| 吃什么能减脂肪肝| 呻吟是什么意思| 人为什么需要诗歌| 梦见刷牙是什么预兆| 什么叫免疫组化| 长胸毛的男人代表什么| 排卵期出血有什么症状| 什么是水马| 球菌阳性是什么意思| 什么叫幽门螺旋杆菌| 什么病必须戒酒| 测骨龄去医院挂什么科| 为什么医院开的钙都是碳酸钙| 作精是什么意思| 为什么会长疤痕疙瘩| 什么是小数| 股票填权是什么意思| 三月十号是什么星座| 筝是什么意思| hdr是什么拍照功能| 热退疹出是什么病| 五不遇时是什么意思| 男生爱出汗是什么原因| 草是什么植物| 肝岛是什么意思| 肾积水是什么原因| 蝴蝶喜欢吃什么| 梦到别人给钱是什么意思| 保税区什么意思| 家严是什么意思| 鸡胗炒什么菜好吃| 脾胃虚吃什么水果好| 九死一生是指什么生肖| 清油是什么油| 县委书记属于什么级别| 丧偶什么意思| h5是什么意思| eblan是什么品牌| 查乙肝挂什么科| ab型和b型生的孩子是什么血型| 升白针是什么药| 什么病不能吃秋葵| 病毒性感冒什么症状| 肌肉一跳一跳什么原因| 锁骨下面的骨头叫什么| 横空出世什么意思| 昆仑雪菊有什么作用| 后援团是什么意思| 唐僧成了什么佛| 龙凤呈祥是什么生肖| 男性肛门瘙痒用什么药| 鬼佬是什么意思| 梦到女儿丢了什么预兆| 活血化瘀吃什么| 流萤是什么意思| 黄瓜籽粉有什么功效| 专辑是什么| 很的右边读什么| 田螺姑娘是什么意思| 人格魅力什么意思| 学什么设计最赚钱| 锅贴是什么| 真好是什么意思| 岩茶是什么茶类| 牙齿黄是什么原因| 天妇罗是什么| 脾胃不好能吃什么水果| 手脚冰冷是什么原因| 下丘脑分泌什么激素| 窦性心律早期复极是什么意思| la是什么品牌| bml是什么意思| 鸡冲什么生肖| kalenji是什么品牌| 鱿鱼是什么动物| 双子女和什么座最配对| 梦见蛇代表什么| 梦见吃药是什么意思| 突然头晕冒虚汗什么原因| 扁平疣吃什么药| 倍增是什么意思| 为难是什么意思| 肚子跳动是什么原因| lh是什么意思| 千呼万唤是什么生肖| 慰安妇是什么意思| 十二指肠球部溃疡a1期是什么意思| 调和营卫是什么意思| 丹参有什么作用| 什么的温度| 什么生水| 心跳过速是什么原因| 血压高呕吐是什么征兆| 做脑部ct挂什么科| 同比和环比是什么意思| 药引子是什么意思| 梦见狗熊是什么预兆| 什么情况下用妇炎洁| 枸杞与菊花一起泡水喝有什么功效| 焦虑症吃什么药最好| 粉刺用什么药膏最有效| 眼镜轴位是什么| 吃榴莲有什么坏处| facebook是什么意思| 什么鱼刺少| 吃的少还胖什么原因| 崴脚用什么药| 比音勒芬是什么档次| 1893年属什么生肖| 孩子咳嗽吃什么药效果好| af什么意思| 紧急避孕药有什么危害| 小狗肚子里有虫子吃什么药| 踏板摩托车什么牌子好| 脉浮是什么意思| 梦见很多牛是什么兆头| avg什么意思| 19属什么| atp 是什么| 后脑勺发麻是什么原因| 良善是什么意思| 什么是脊柱侧弯| 癫痫是什么意思| 输血前四项检查是什么| 风寒感冒吃什么药最快| 小人痣代表什么意思| 例假不能吃什么水果| 党的性质是什么| puma是什么品牌| 禅位是什么意思| 印度为什么没把墨脱占领| 压脚背有什么好处| yj是什么意思| 帽缨是什么意思| 吃维生素b6有什么好处和副作用| 塞上是什么意思| 银灰色五行属什么| 什么是中产阶级| 桶状胸常见于什么病| 为什么会长丝状疣| 橘子是什么季节| 10月1日是什么日子| ct与核磁共振有什么区别| 正在值机是什么意思| 舌头有红点是什么原因| 脚扭了挂什么科| 5月10号是什么日子| 什么叫三观不正| 自欺欺人什么意思| 试金石什么意思| 三星是什么军衔| 钟爱一生是什么意思| 尿蛋白阳性是什么意思| 散瞳后需要注意什么| 咀嚼食用是什么意思| 黄毛什么意思| 梦见死人了是什么意思| tc什么意思| 囊性包块是什么| 李连杰为什么不娶丁岚| 重阳节送老人什么礼物| 什么药能治口臭| 形声字是什么意思| 南瓜不能和什么一起吃| 头发为什么会分叉| 梦见虱子是什么意思| 胃火吃什么中成药| 五定是什么| vj是什么意思| 鱼缸摆放什么位置最佳| 水痘长什么样子的图片| 什么食物利尿效果最好| 小猫能吃什么水果| 虎父无犬子什么意思| 兰精莫代尔是什么面料| 肚子咕咕叫吃什么药| 孕晚期为什么会脚肿| 姜枣茶什么季节喝最好| 六月六吃什么| 臀推是什么意思| 房颤吃什么药最好| 什么是冰晶| 会阴是什么部位| pr是什么| 0和1什么意思| 美商是什么意思| 为什么口臭| 7朵玫瑰花代表什么意思| 突然发热是什么原因| 四物汤什么时候喝| 天天流鼻血是什么原因| 全身痒是什么病| 挂匾是什么意思| 半夜容易醒是什么原因| 什么的礼物| 半边屁股疼是什么原因| pct是什么| 属兔配什么属相最好| 什么是剧烈运动| 蛔虫是什么| saucony是什么牌子| 狙击蟹吃什么| 叹气是什么意思| 肌酐高吃什么食物好| 喉咙痒咳嗽有痰是什么原因| 小猫吃什么| 中耳炎吃什么药效果比较好| 孤是什么意思| 血糖高看什么科| 底妆是什么意思| 跑完步头疼是为什么| 香港买什么便宜| 膳食是什么意思| 什么是扁平足图片| 什么叫天干| 不踏实是什么意思| remember是什么意思| 百度Jump to content

崩溃什么意思

From Wikipedia, the free encyclopedia
UML class diagram
百度 平台上提供概念设计\方案设计\扩初设计\施工图设计\施工配合等阶段设计专业服务。

The abstract factory pattern in software engineering is a design pattern that provides a way to create families of related objects without imposing their concrete classes, by encapsulating a group of individual factories that have a common theme without specifying their concrete classes.[1] According to this pattern, a client software component creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the family. The client does not know which concrete objects it receives from each of these internal factories, as it uses only the generic interfaces of their products.[1] This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.[2]

Use of this pattern enables interchangeable concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems that are more difficult to debug and maintain.

Overview

[edit]

The abstract factory design pattern is one of the 23 patterns described in the 1994 Design Patterns book. It may be used to solve problems such as:[3]

  • How can an application be independent of how its objects are created?
  • How can a class be independent of how the objects that it requires are created?
  • How can families of related or dependent objects be created?

Creating objects directly within the class that requires the objects is inflexible. Doing so commits the class to particular objects and makes it impossible to change the instantiation later without changing the class. It prevents the class from being reusable if other objects are required, and it makes the class difficult to test because real objects cannot be replaced with mock objects.

A factory is the location of a concrete class in the code at which objects are constructed. Implementation of the pattern intends to insulate the creation of objects from their usage and to create families of related objects without depending on their concrete classes.[2] This allows for new derived types to be introduced with no change to the code that uses the base class.

The pattern describes how to solve such problems:

  • Encapsulate object creation in a separate (factory) object by defining and implementing an interface for creating objects.
  • Delegate object creation to a factory object instead of creating objects directly.

This makes a class independent of how its objects are created. A class may be configured with a factory object, which it uses to create objects, and the factory object can be exchanged at runtime.

Definition

[edit]

Design Patterns describes the abstract factory pattern as "an interface for creating families of related or dependent objects without specifying their concrete classes."[4]

Usage

[edit]

The factory determines the concrete type of object to be created, and it is here that the object is actually created. However, the factory only returns a reference (in Java, for instance, by the new operator) or a pointer of an abstract type to the created concrete object.

This insulates client code from object creation by having clients request that a factory object create an object of the desired abstract type and return an abstract pointer to the object.[5]

An example is an abstract factory class DocumentCreator that provides interfaces to create a number of products (e.g., createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class such asFancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create corresponding objects such asFancyLetter or ModernResume. Each of these products is derived from a simple abstract class such asLetter or Resume of which the client is aware. The client code would acquire an appropriate instance of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme. The client would only need to know how to handle the abstract Letter or Resume class, not the specific version that was created by the concrete factory.

As the factory only returns a reference or a pointer to an abstract type, the client code that requested the object from the factory is not aware of—and is not burdened by—the actual concrete type of the object that was created. However, the abstract factory knows the type of a concrete object (and hence a concrete factory). For instance, the factory may read the object's type from a configuration file. The client has no need to specify the type, as the type has already been specified in the configuration file. In particular, this means:

  • The client code has no knowledge of the concrete type, not needing to include any header files or class declarations related to it. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interfaces.[6]
  • Adding new concrete types is performed by modifying the client code to use a different factory, a modification that is typically one line in one file. The different factory then creates objects of a different concrete type but still returns a pointer of the same abstract type as before, thus insulating the client code from change. This is significantly easier than modifying the client code to instantiate a new type. Doing so would require changing every location in the code where a new object is created as well as ensuring that all such code locations have knowledge of the new concrete type, for example, by including a concrete class header file. If all factory objects are stored globally in a singleton object, and all client code passes through the singleton to access the proper factory for object creation, then changing factories is as easy as changing the singleton object.[6]

Structure

[edit]

UML diagram

[edit]
A sample UML class and sequence diagram for the abstract factory design pattern. [7]
A sample UML class and sequence diagram for the abstract factory design pattern. [7]

In the above UML class diagram, the Client class that requires ProductA and ProductB objects does not instantiate the ProductA1 and ProductB1 classes directly. Instead, the Client refers to the AbstractFactory interface for creating objects, which makes the Client independent of how the objects are created (which concrete classes are instantiated). The Factory1 class implements the AbstractFactory interface by instantiating the ProductA1 and ProductB1 classes.

The UML sequence diagram shows the runtime interactions. The Client object calls createProductA() on the Factory1 object, which creates and returns a ProductA1 object. Thereafter, the Client calls createProductB() on Factory1, which creates and returns a ProductB1 object.

Variants

[edit]

The original structure of the abstract factory pattern, as defined in 1994 in Design Patterns, is based on abstract classes for the abstract factory and the abstract products to be created. The concrete factories and products are classes that specialize the abstract classes using inheritance.[4]

A more recent structure of the pattern is based on interfaces that define the abstract factory and the abstract products to be created. This design uses native support for interfaces or protocols in mainstream programming languages to avoid inheritance. In this case, the concrete factories and products are classes that realize the interface by implementing it.[1]

Example

[edit]

This C++23 implementation is based on the pre-C++98 implementation in the book.

import std;

enum class Direction {North, South, East, West};

class MapSite {
public:
  virtual void enter() = 0;
  virtual ~MapSite() = default;
};

class Room: public MapSite {
public:
  Room(): roomNumber(0) {}
  Room(int n): roomNumber(n) {}
  void setSide(Direction d, MapSite* ms) {
    std::println("Room::setSide {} ms", d);
  }
  virtual void enter() {}
  Room(const Room&) = delete; // rule of three
  Room& operator=(const Room&) = delete;
private:
  int roomNumber;
};

class Wall: public MapSite {
public:
  Wall() {}
  virtual void enter() {}
};

class Door: public MapSite {
public:
  Door(Room* r1 = nullptr, Room* r2 = nullptr): room1(r1), room2(r2) {}
  virtual void enter() {}
  Door(const Door&) = delete; // rule of three
  Door& operator=(const Door&) = delete;
private:
  Room* room1;
  Room* room2;
};

class Maze {
public:
  void addRoom(Room* r) {
    std::println("Maze::addRoom {}", r);
  }
  Room* roomNo(int) const {
    return nullptr;
  }
};

class MazeFactory {
public:
  MazeFactory() = default;
  virtual ~MazeFactory() = default;

  virtual Maze* makeMaze() const {
    return new Maze;
  }
  virtual Wall* makeWall() const {
    return new Wall;
  }
  virtual Room* makeRoom(int n) const {
    return new Room(n);
  }
  virtual Door* makeDoor(Room* r1, Room* r2) const {
    return new Door(r1, r2);
  }
};

// If createMaze is passed an object as a parameter to use to create rooms, walls, and doors, then you can change the classes of rooms, walls, and doors by passing a different parameter. This is an example of the Abstract Factory (99) pattern.

class MazeGame {
public:
  Maze* createMaze(MazeFactory& factory) {
    Maze* aMaze = factory.makeMaze();
    Room* r1 = factory.makeRoom(1);
    Room* r2 = factory.makeRoom(2);
    Door* aDoor = factory.makeDoor(r1, r2);
    aMaze->addRoom(r1);
    aMaze->addRoom(r2);
    r1->setSide(Direction::North, factory.makeWall());
    r1->setSide(Direction::East, aDoor);
    r1->setSide(Direction::South, factory.makeWall());
    r1->setSide(Direction::West, factory.makeWall());
    r2->setSide(Direction::North, factory.makeWall());
    r2->setSide(Direction::East, factory.makeWall());
    r2->setSide(Direction::South, factory.makeWall());
    r2->setSide(Direction::West, aDoor);
    return aMaze;
  }
};

int main() {
  MazeGame game;
  MazeFactory factory;
  game.createMaze(factory);
}

The program output is:

Maze::addRoom 0x1317ed0
Maze::addRoom 0x1317ef0
Room::setSide 0 0x1318340
Room::setSide 2 0x1317f10
Room::setSide 1 0x1318360
Room::setSide 3 0x1318380
Room::setSide 0 0x13183a0
Room::setSide 2 0x13183c0
Room::setSide 1 0x13183e0
Room::setSide 3 0x1317f10

See also

[edit]

References

[edit]
  1. ^ a b c Freeman, Eric; Robson, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Hendrickson, Mike; Loukides, Mike (eds.). Head First Design Patterns (paperback). Vol. 1. O'REILLY. p. 156. ISBN 978-0-596-00712-6. Retrieved 2025-08-06.
  2. ^ a b Freeman, Eric; Robson, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Hendrickson, Mike; Loukides, Mike (eds.). Head First Design Patterns (paperback). Vol. 1. O'REILLY. p. 162. ISBN 978-0-596-00712-6. Retrieved 2025-08-06.
  3. ^ "The Abstract Factory design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2025-08-06.
  4. ^ a b Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (2025-08-06). "Design Patterns: Abstract Factory". informIT. Archived from the original on 2025-08-06. Retrieved 2025-08-06. Object Creational: Abstract Factory: Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  5. ^ Veeneman, David (2025-08-06). "Object Design for the Perplexed". The Code Project. Archived from the original on 2025-08-06. Retrieved 2025-08-06. The factory insulates the client from changes to the product or how it is created, and it can provide this insulation across objects derived from very different abstract interfaces.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  6. ^ a b "Abstract Factory: Implementation". OODesign.com. Retrieved 2025-08-06.
  7. ^ "The Abstract Factory design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2025-08-06.
[edit]
出球小动脉流什么血 辅酶q10什么时间吃最好 户籍地址是什么 胃炎是什么症状 六味地黄丸什么牌子好
巴沙鱼为什么不能吃 老三篇是什么意思 右肺上叶为什么恶性多 pas什么意思 什么风大雨
眉心长痘是什么原因 眼睛干痒用什么眼药水比较好 孤寡老人国家有什么政策 蛇进家是什么意思 突然头晕是什么情况
部首和偏旁有什么区别 lad是什么意思 亚人是什么意思 领养孩子需要什么条件 牙上有黑渍是什么原因
交界性心律是什么意思hcv9jop0ns5r.cn 白是什么结构的字inbungee.com 什么牌子的助听器最好hcv7jop5ns2r.cn 沄字五行属什么hcv8jop5ns5r.cn 总胆固醇偏高吃什么药hcv8jop9ns2r.cn
darling什么意思hcv9jop7ns9r.cn 年薪10万算什么水平hcv8jop9ns6r.cn 妈妈的哥哥的老婆叫什么hcv9jop2ns2r.cn 小腹左侧疼是什么原因tiangongnft.com 江西庐山产什么茶hcv9jop2ns1r.cn
垂体泌乳素高是什么原因helloaicloud.com 水军是什么意思hcv8jop8ns7r.cn 中耳炎吃什么药最有效inbungee.com 九寨沟在什么地方hcv9jop1ns7r.cn 肉丝炒什么好吃hcv8jop9ns7r.cn
胸腔积液挂什么科hcv7jop6ns2r.cn 小三阳吃什么食物好得快hcv7jop9ns7r.cn 肉毒为什么怕热敷huizhijixie.com 塑料袋属于什么垃圾hcv8jop8ns1r.cn 什么时候拔牙最好hcv9jop4ns2r.cn
百度