地接是什么意思| 双肺局限性气肿是什么病| 房间为什么有蟑螂| 尿里带血是什么原因男性| 口干舌燥是什么病| 做爱什么姿势| 毛豆炒什么好吃| 老年人腿疼是什么原因引起的| 头部神经痛吃什么药好| 什么口服液补血补气最好| 绿豆和什么食物相克| 舌头上有黑苔是什么原因| 躯体症状是什么| 晚上尿次数多什么原因| 扶苏是什么意思| 熬夜吃什么水果好| 吃奇亚籽有什么好处| 用什么洗脸可以美白| 什么叫智齿牙| 什么病不能熬夜| 检查肺部挂什么科室| 边际贡献是什么意思| 局部癌变是什么意思| 脸部麻木是什么原因引起的| 梅花三弄是什么意思| 天上的星星像什么| 送朋友鲜花送什么花| 什么叫脂溢性脱发| 什么样的肚子疼是癌| ab和a型血生的孩子是什么血型| 割包皮应该挂什么科| 狮子吃什么| 补气血吃什么食物最好| 荞麦是什么| 脚疼是什么原因| gxg是什么牌子| 地包天是什么意思| 鲲之大的之是什么意思| 脚干脚裂用什么药| 慢阻肺是什么病| 后脚跟疼是什么原因| 庚申五行属什么| 长期喝蜂蜜水有什么好处| 凤凰单丛属于什么茶| 血压表什么牌子的好最准确最耐用| 梦见自己得绝症了是什么预兆| 因数是什么意思| 吃什么能去湿气| 内科是看什么病的| 发烧应该吃什么药| 什么是内分泌失调| 二月什么星座| 右眼皮上长痣代表什么| 怀孕为什么会肚子痛| ok镜是什么| 崖柏手串有什么功效| 升米恩斗米仇什么意思| 井柏然原名叫什么| 红色学士服是什么学位| 公安局属于什么机关| 88属什么生肖| 南京市徽为什么是貔貅| 智利说什么语言| 热玛吉是什么意思| 不作为什么意思| 梦见自己死了预示什么| 神经性头痛吃什么药效果好| 一台什么| 肺在什么位置图片| 沙眼衣原体是什么意思| 胎方位roa是什么意思| 吃什么养肾| 喉咙有痰吃什么药| 人中浅的女人代表什么| 三天打鱼两天晒网什么意思| 一碗香是什么菜| 喉咙发痒咳嗽吃什么药| 世界上最小的花是什么花| 什么眠什么睡| 子宫内膜息肉样增生是什么意思| 南非叶有什么功效| 醋泡什么壮阳最快| 共工是什么神| 舌头裂纹是什么病| 东莞五行属什么| kenzo是什么牌子| 扁桃体有什么用| 拉钩为什么要上吊| 吃什么可降低胆固醇| 什么是天干地支| 吃什么肉不会胖又减肥| 手上的月牙代表什么意思| 荔枝不能与什么一起吃| 胃胀吃什么药效果好| 笑字五行属什么| 多金是什么意思| 天雨粟鬼夜哭什么意思| 姑姑的老公叫什么| 什么化妆品好用哪个牌子的| 胃疼吃什么消炎药| 女人细菌感染什么原因引起的| 禁锢是什么意思| 采阴补阳是什么意思| 已知晓是什么意思| 不完全骨折是什么意思| 翌字五行属什么| 侄女叫我什么| 喉咙发甜是什么原因| qq是什么| 无利不起早是什么意思| 2002年出生属什么| 你为什么背着我爱别人| 什么原因引起脑梗| 除氯是什么意思| 羊肉和什么菜包饺子好吃| 日单是什么意思| 早上起来口干口苦口臭是什么原因| 地主之谊是什么意思| 总是耳鸣是什么原因| 健康证需要检查什么| 贫血喝什么口服液最好| 舌头干燥是什么原因| 顽疾是什么意思| 双肺纹理增多增粗是什么病| 吃过期药有什么危害| 紧急避孕药什么时候吃有效| 甲状腺低密度结节是什么意思| 肾病什么东西不能吃| 非分之想什么意思| 甘油三脂是什么意思| 马云是什么大学毕业的| 你的脚步流浪在天涯是什么歌曲| 微信英文名叫什么| ct是检查什么的| 蓝颜知己是什么关系| 佐匹克隆片是什么药| 颈椎病是什么症状| 11月28日是什么星座| 脚裂口子是什么原因| 跖疣去医院挂什么科室| 卵泡长得慢是什么原因造成的| 土命缺什么| 月经2天就没了什么原因| 林黛玉属什么生肖| 狗剩是什么意思| 瑞士移民需要什么条件| low什么意思| 皮下囊肿是什么原因引起的| 蜜蜡脱毛有什么危害吗| 透明的什么填词语| 肾炎吃什么好| 九月份是什么季节| 友人是什么意思| boys是什么意思| 郁闷是什么意思| us是什么单位| 吃中药不能吃什么东西| 核桃和什么一起打豆浆| 什么人需要做心脏造影| 共襄盛举是什么意思| 生气什么什么| 什么级别| 潘驴邓小闲什么意思| 坐月子吃什么好| 熬夜喝什么提神醒脑| 混合型高脂血症是什么意思| 什么叫冷暴力| viscose是什么面料| 男人吃荔枝有什么好处| 白蛋白低吃什么补得快| iruri 什么意思| 升白细胞的针剂叫什么| 潜血阳性是什么意思| 血糖高是什么意思| 胃幽门螺杆菌有什么症状| 冻顶乌龙茶是什么茶| 双侧卵巢多囊样改变是什么意思| 辞退和开除有什么区别| 紧张性头痛吃什么药| 单独粘米粉能做什么| 月经推迟吃什么| 胎盘低是什么原因造成的| 10月13号是什么星座| 晚上一点多是什么时辰| 尿液检查能查出什么病| 不举什么意思| 云南古代叫什么| 二尖瓣少量反流是什么意思| 月经有点黑是什么原因| 风麻疹是什么引起的| 长痣是什么原因引起的| 鱼腥草有什么功效| 金青什么字| 子宫腺肌症吃什么药| 中的反义词是什么| 春天的花开秋天的风是什么歌| hardly什么意思| 单身贵族什么意思| 肾亏是什么原因造成的| 果糖是什么糖| 香奈儿是什么牌子| 什么的挑选| 蜗牛的触角有什么作用| 水加人念什么| zoom 是什么意思| 敏感的反义词是什么| 妍五行属性是什么| 什么是胆红素| 神经疼痛吃什么药| 老鼠人是什么意思| 梦到前妻预示什么| 转氨酶升高有什么症状| 静脉曲张挂什么号| 什么是翻新机| 吃完就拉是什么原因| 鳞状上皮细胞是什么意思| 脸上过敏是什么症状| 捣碎东西的器皿叫什么| 风起云涌是什么生肖| 11月16号是什么星座| 成人受到惊吓吃什么药| 乳腺癌吃什么好| 尹什么意思| 缠绵是什么意思| 什么app可以买烟| 土豆可以做什么美食| 什么自行车最贵| 透亮是什么意思| viomi是什么牌子| 2026年是什么生肖年| 胃不好吃什么养胃水果| 豆工念什么| 腰疼挂什么科| 惊为天人是什么意思| 双腿酸软无力吃什么药| 黄色搭配什么颜色| 子宫回声欠均匀是什么意思| 云南的特产是什么| 豪爽是什么意思| 产后检查挂什么科| 屎壳郎是什么意思| 乳腺导管扩张吃什么药| 房产税什么时候开始征收| 包含是什么意思| 2月25日什么星座| 蜂鸟是什么鸟| 广东有什么特色美食| positive是什么意思| 胡萝卜和什么榨汁好喝| 一级甲等医院是什么意思| 鬼长什么样子| 知了为什么要叫| 特需门诊是什么意思| 吃了头孢不能吃什么| 为什么黄瓜是绿色的却叫黄瓜| 人为什么有五根手指| ori是什么意思| 开字加一笔是什么字| 公道自在人心是什么意思| 肾积水是什么原因| 强肉弱食是什么意思| 古代男宠叫什么| 属鸡的适合干什么行业最赚钱| 早孕反应最早什么时候出现| 百度Jump to content

岁寒三友是什么意思

From Wikipedia, the free encyclopedia
百度 在快节奏的现代生活中,快餐和外卖日益流行。

In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.[1]

Overview

[edit]

The Composite[2] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

Problems the Composite design pattern can solve

[edit]
  • Represent a part-whole hierarchy so that clients can treat part and whole objects uniformly.
  • Represent a part-whole hierarchy as tree structure.

When defining (1) Part objects and (2) Whole objects that act as containers for Part objects, clients must treat them separately, which complicates client code.[3]

Solutions the Composite design pattern describes

[edit]
  • Define a unified Component interface for part (Leaf) objects and whole (Composite) objects.
  • Individual Leaf objects implement the Component interface directly, and Composite objects forward requests to their child components.

This enables clients to work through the Component interface to treat Leaf and Composite objects uniformly: Leaf objects perform a request directly, and Composite objects forward the request to their child components recursively downwards the tree structure. This makes client classes easier to implement, change, test, and reuse.

See also the UML class and object diagram below.

Motivation

[edit]

When dealing with Tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, more error prone. The solution is an interface that allows treating complex and primitive objects uniformly. In object-oriented programming, a composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality. This is known as a "has-a" relationship between objects.[4] The key concept is that you can manipulate a single instance of the object just as you would manipulate a group of them. The operations you can perform on all the composite objects often have a least common denominator relationship. For example, if defining a system to portray grouped shapes on a screen, it would be useful to define resizing a group of shapes to have the same effect (in some sense) as resizing a single shape.

When to use

[edit]

Composite should be used when clients ignore the difference between compositions of objects and individual objects.[1] If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous.

Structure

[edit]

UML class and object diagram

[edit]
A sample UML class and object diagram for the Composite design pattern. [5]

In the above UML class diagram, the Client class doesn't refer to the Leaf and Composite classes directly (separately). Instead, the Client refers to the common Component interface and can treat Leaf and Composite uniformly.
The Leaf class has no children and implements the Component interface directly.
The Composite class maintains a container of child Component objects (children) and forwards requests to these children (for each child in children: child.operation()).

The object collaboration diagram shows the run-time interactions: In this example, the Client object sends a request to the top-level Composite object (of type Component) in the tree structure. The request is forwarded to (performed on) all child Component objects (Leaf and Composite objects) downwards the tree structure.

Defining Child-Related Operations
Defining child-related operations in the Composite design pattern. [6]

There are two design variants for defining and implementing child-related operations like adding/removing a child component to/from the container (add(child)/remove(child)) and accessing a child component (getChild()):

  • Design for transparency: Child-related operations are defined in the Component interface. This enables clients to treat Leaf and Composite objects uniformly. But type safety is lost because clients can perform child-related operations on Leaf objects.
  • Design for type safety: Child-related operations are defined only in the Composite class. Clients must treat Leaf and Composite objects differently. But type safety is gained because clients cannot perform child-related operations on Leaf objects.

The GoF authors present a variant of the Composite design pattern that emphasizes transparency over type safety and discuss the tradeoffs of the two approaches.[1]

The type-safe approach is particularly palatable if the composite structure is fixed post construction: the construction code does not require transparency because it needs to know the types involved in order to construct the composite. If downstream, the code does not need to modify the structure, then the child manipulation operations do not need to be present on the Component interface.

UML class diagram

[edit]
Composite pattern in UML.
Component
  • is the abstraction for all components, including composite ones
  • declares the interface for objects in the composition
  • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate
Leaf
  • represents leaf objects in the composition
  • implements all Component methods
Composite
  • represents a composite Component (component having children)
  • implements methods to manipulate children
  • implements all Component methods, generally by delegating them to its children
Composite pattern in LePUS3.

Variation

[edit]

As it is described in Design Patterns, the pattern also involves including the child-manipulation methods in the main Component interface, not just the Composite subclass. More recent descriptions sometimes omit these methods.[7]

Example

[edit]

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

#include <iostream>
#include <string>
#include <list>
#include <memory>
#include <stdexcept>

typedef double Currency;

// declares the interface for objects in the composition.
class Equipment { // Component
public:
  // implements default behavior for the interface common to all classes, as appropriate.
  virtual const std::string& getName() {
    return name;
  }
  virtual void setName(const std::string& name_) {
    name = name_;
  }
  virtual Currency getNetPrice() {
    return netPrice;
  }
  virtual void setNetPrice(Currency netPrice_) {
    netPrice = netPrice_;
  }
  // declares an interface for accessing and managing its child components.
  virtual void add(std::shared_ptr<Equipment>) = 0;
  virtual void remove(std::shared_ptr<Equipment>) = 0;
  virtual ~Equipment() = default;
protected:
  Equipment() :name(""), netPrice(0) {}
  Equipment(const std::string& name_) :name(name_), netPrice(0) {}
private:
  std::string name;
  Currency netPrice;
};

// defines behavior for components having children.
class CompositeEquipment : public Equipment { // Composite
public:
  // implements child-related operations in the Component interface.
  virtual Currency getNetPrice() override {
    Currency total = Equipment::getNetPrice();
    for (const auto& i:equipment) {
      total += i->getNetPrice();
    }
    return total;
  }
  virtual void add(std::shared_ptr<Equipment> equipment_) override {
    equipment.push_front(equipment_.get());
  }
  virtual void remove(std::shared_ptr<Equipment> equipment_) override {
    equipment.remove(equipment_.get());
  }
protected:
  CompositeEquipment() :equipment() {}
  CompositeEquipment(const std::string& name_) :equipment() {
    setName(name_);
  }
private:
  // stores child components.
  std::list<Equipment*> equipment;
};

// represents leaf objects in the composition.
class FloppyDisk : public Equipment { // Leaf
public:
  FloppyDisk(const std::string& name_) {
    setName(name_);
  }
  // A leaf has no children.
  void add(std::shared_ptr<Equipment>) override {
    throw std::runtime_error("FloppyDisk::add");
  }
  void remove(std::shared_ptr<Equipment>) override {
    throw std::runtime_error("FloppyDisk::remove");
  }
};

class Chassis : public CompositeEquipment {
public:
  Chassis(const std::string& name_) {
    setName(name_);
  }
};

int main() {
  // The smart pointers prevent memory leaks.
  std::shared_ptr<FloppyDisk> fd1 = std::make_shared<FloppyDisk>("3.5in Floppy");
  fd1->setNetPrice(19.99);
  std::cout << fd1->getName() << ": netPrice=" << fd1->getNetPrice() << '\n';

  std::shared_ptr<FloppyDisk> fd2 = std::make_shared<FloppyDisk>("5.25in Floppy");
  fd2->setNetPrice(29.99);
  std::cout << fd2->getName() << ": netPrice=" << fd2->getNetPrice() << '\n';

  std::unique_ptr<Chassis> ch = std::make_unique<Chassis>("PC Chassis");
  ch->setNetPrice(39.99);
  ch->add(fd1);
  ch->add(fd2);
  std::cout << ch->getName() << ": netPrice=" << ch->getNetPrice() << '\n';

  fd2->add(fd1);
}

The program output is

3.5in Floppy: netPrice=19.99
5.25in Floppy: netPrice=29.99
PC Chassis: netPrice=89.97
terminate called after throwing an instance of 'std::runtime_error'
  what():  FloppyDisk::add

See also

[edit]

References

[edit]
  1. ^ a b c Gamma, Erich; Richard Helm; Ralph Johnson; John M. Vlissides (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. pp. 395. ISBN 0-201-63361-2.
  2. ^ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 163ff. ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link)
  3. ^ "The Composite design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2025-08-06.
  4. ^ Scott Walters (2004). Perl Design Patterns Book. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  5. ^ "The Composite design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2025-08-06.
  6. ^ "The Composite design pattern - Implementation". w3sDesign.com. Retrieved 2025-08-06.
  7. ^ Geary, David (13 September 2002). "A look at the Composite design pattern". Java Design Patterns. JavaWorld. Retrieved 2025-08-06.
[edit]
孕妇吃什么容易滑胎 鲁迅的原名叫什么 无大碍是什么意思 男性湿热吃什么药最好 透骨草长什么样
腹水是什么病 chocker是什么 吃什么治白头发 什么是回迁房 格列卫是什么药
什么是纳米 为什么喝牛奶会拉肚子 甲状腺4级是什么意思 什么是腹式呼吸的正确方法 肌酐低什么原因
积家手表什么档次 什么水果美白 尼泊尔人是什么人种 尿床去医院挂什么科 1969年属鸡是什么命
坐以待毙是什么意思hcv7jop5ns6r.cn 姐姐的儿子叫什么hcv7jop9ns1r.cn 母的第三笔是什么hcv9jop3ns6r.cn 红线女是什么意思luyiluode.com 胎盘下缘覆盖宫颈内口是什么意思hcv9jop5ns0r.cn
心宽是什么意思helloaicloud.com 警察是什么生肖hcv9jop8ns2r.cn 五险一金什么时候开始交hcv8jop5ns0r.cn 小巫见大巫是什么意思hcv9jop8ns1r.cn 默念是什么意思hcv7jop4ns5r.cn
桂枝是什么hcv9jop5ns2r.cn 中国最长的河流是什么河shenchushe.com a1是什么hcv8jop7ns2r.cn 蹉跎是什么意思hcv7jop6ns5r.cn 手上蜕皮是什么原因hcv9jop3ns2r.cn
1999年发生了什么事dajiketang.com 半夜惊醒是什么原因hcv9jop0ns8r.cn 皲裂什么意思hcv7jop9ns3r.cn ab和b型血生的孩子是什么血型hcv8jop2ns0r.cn 荣膺是什么意思hcv8jop0ns0r.cn
百度