国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

《設計模式的藝術》筆記 - 組合模式

這篇具有很好參考價值的文章主要介紹了《設計模式的藝術》筆記 - 組合模式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

介紹

? ? ? ? 組合模式組合多個對象形成樹形結構以表示具有“部分-整體”關系的層次結構。組合模式對單個對象(即葉子對象)和組合對象(即容器對象)的使用具有一致性,又可以稱為“部分—整體”(Part-Whole)模式,它是一種對象結構型模式。

實現(xiàn)

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>
#include <vector>

class Component {    // 抽象基類
public:
    virtual void operation();
    virtual void add(Component *component);
    virtual void remove(Component *component);
    virtual Component *getChild(int i);

protected:
    std::string m_name;
};

class LeafComponent : public Component {    // 葉子節(jié)點具體類
public:
    LeafComponent(const std::string &name);
    void operation() override;
};

class CompositeComponent : public Component {    // 容器節(jié)點具體類
public:
    CompositeComponent(const std::string &name);

    void operation() override;

    void add(Component *component) override;

    void remove(Component *component) override;

    Component *getChild(int i) override;

private:
    std::vector<Component *> m_components;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

void Component::operation() {
    throw std::runtime_error("operation exception");
}

void Component::add(Component *component) {
    throw std::runtime_error("add exception");
}

void Component::remove(Component *component) {
    throw std::runtime_error("remove exception");
}

Component *Component::getChild(int i) {
    throw std::runtime_error("getChild exception");
}

LeafComponent::LeafComponent(const std::string &name) {
    m_name = name;
}

void LeafComponent::operation() {
    std::cout << m_name << ": LeafComponent::operation()" << std::endl;
}

CompositeComponent::CompositeComponent(const std::string &name) {
    m_name = name;
}

void CompositeComponent::operation() {
    std::cout << m_name << ": CompositeComponent::operation()" << std::endl;
    for (auto it = m_components.begin(); it != m_components.end(); ++it) {
        (*it)->operation();
    }
}

void CompositeComponent::add(Component *component) {
    m_components.push_back(component);
}

void CompositeComponent::remove(Component *component) {
    for (auto it = m_components.begin(); it != m_components.end(); ++it) {
        if (*it == component) {
            m_components.erase(it);
            break;
        }
    }
}

Component *CompositeComponent::getChild(int i) {
    if (i < m_components.size()) {
        return m_components[i];
    } else {
        return nullptr;
    }
}

main.cpp

#include <iostream>
#include <mutex>
#include "myclass.h"

int main() {
    Component *leaf = new LeafComponent("leaf1");
    Component *composite = new CompositeComponent("composite1");
    Component *composite2 = new CompositeComponent("composite2");

    composite->add(new LeafComponent("leaf2"));
    composite->add(new LeafComponent("leaf3"));
    composite2->add(new LeafComponent("leaf4"));
    composite->add(composite2);
    leaf->operation();
    composite->operation();
    try {
        leaf->getChild(0);
    } catch (std::exception &e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

組合模式形式

透明組合模式

? ? ? ? 抽象構建Component中聲明了所有用于管理成員對象的方法,這樣能確保所有的構建類都有相同的接口。缺點是不夠安全,因為葉子對象和容器對象在本質上是有區(qū)別的。

組合安全模式

????????安全組合模式中,在抽象構件Component中沒有聲明任何用于管理成員對象的方法,而是在Composite類中聲明并實現(xiàn)這些方法。這種做法是安全的,因為根本不向葉子對象提供這些管理成員對象的方法,對于葉子對象,客戶端不可能調用到這些方法。缺點是不夠透明,因為葉子構件和容器構件具有不同的方法,且容器構件中那些用于管理成員對象的方法沒有在抽象構件類中定義,因此客戶端不能完全針對抽象編程,必須有區(qū)別地對待葉子構件和容器構件。

總結

優(yōu)點

? ? ? ? 1.?組合模式可以清楚地定義分層次的復雜對象,表示對象的全部或部分層次。它讓客戶端忽略了層次的差異,方便對整個層次結構進行控制。

? ? ? ? 2.?客戶端可以一致地使用一個組合結構或其中單個對象,不必關心處理的是單個對象還是整個組合結構,簡化了客戶端代碼。

? ? ? ? 3.?在組合模式中增加新的容器構件和葉子構件都很方便,無須對現(xiàn)有類庫進行任何修改,符合開閉原則。

? ? ? ? 4.?組合模式為樹形結構的面向對象實現(xiàn)提供了一種靈活的解決方案。通過葉子對象和容器對象的遞歸組合,可以形成復雜的樹形結構,但對樹形結構的控制卻非常簡單。

缺點

? ? ? ? 1.?在增加新構件時很難對容器中的構件類型進行限制。

適用場景

? ? ? ? 1.?在具有整體和部分的層次結構中,希望通過一種方式忽略整體與部分的差異,客戶端可以一致性地對待它們。

? ? ? ? 2.?在一個使用面向對象語言開發(fā)的系統(tǒng)中需要處理一個樹形結構。

? ? ? ? 3.?在一個系統(tǒng)中能夠分離出葉子對象和容器對象,而且它們的類型不固定,將來需要增加一些新的類型。

練習

myclass.h

//
// Created by yuwp on 2024/1/12.
//

#ifndef DESIGNPATTERNS_MYCLASS_H
#define DESIGNPATTERNS_MYCLASS_H

#include <iostream>
#include <vector>

class Component {
public:
    virtual void display();
    virtual void add(Component *component);
    virtual void remove(Component *component);
    virtual Component *getChild(int i);

protected:
    std::string m_name;
};

class Button : public Component {
public:
    Button(const std::string &name);

    void display() override;
};

class Text : public Component {
public:
    Text(const std::string &name);

    void display() override;
};

class Window : public Component {
public:
    Window(const std::string &name);

    void display() override;

    void add(Component *component) override;

    void remove(Component *component) override;

    Component *getChild(int i) override;

private:
    std::vector<Component *> m_childs;
};

class Panel : public Component {
public:
    Panel(const std::string &name);

    void display() override;

    void add(Component *component) override;

    void remove(Component *component) override;

    Component *getChild(int i) override;

private:
    std::vector<Component *> m_childs;
};

#endif //DESIGNPATTERNS_MYCLASS_H

myclass.cpp文章來源地址http://www.zghlxwxcb.cn/news/detail-806134.html

//
// Created by yuwp on 2024/1/12.
//

#include "myclass.h"

void Component::display() {
    throw std::runtime_error("operation exception");
}

void Component::add(Component *component) {
    throw std::runtime_error("add exception");
}

void Component::remove(Component *component) {
    throw std::runtime_error("remove exception");
}

Component *Component::getChild(int i) {
    throw std::runtime_error("getChild exception");
}


Button::Button(const std::string &name) {
    m_name = name;
}

void Button::display() {
    std::cout << "Button: " << m_name << std::endl;
}

Text::Text(const std::string &name) {
    m_name = name;
}

void Text::display() {
    std::cout << "Text: " << m_name << std::endl;
}

Window::Window(const std::string &name) {
    m_name = name;
}

void Window::display() {
    std::cout << "Window: " << m_name << "包含: " << std::endl;
    for (auto it = m_childs.begin(); it != m_childs.end(); ++it) {
        (*it)->display();
    }
}

void Window::add(Component *component) {
    m_childs.push_back(component);
}

void Window::remove(Component *component) {
    for (auto it = m_childs.begin(); it != m_childs.end(); ++it) {
        if (*it == component) {
            m_childs.erase(it);
            break;
        }
    }
}

Component *Window::getChild(int i) {
    if (i < m_childs.size()) {
        return m_childs[i];
    } else {
        return nullptr;
    }
}

Panel::Panel(const std::string &name) {
    m_name = name;
}

void Panel::display() {
    std::cout << "Panel: " << m_name << "包含: " << std::endl;
    for (auto it = m_childs.begin(); it != m_childs.end(); ++it) {
        (*it)->display();
    }
}

void Panel::add(Component *component) {
    m_childs.push_back(component);
}

void Panel::remove(Component *component) {
    for (auto it = m_childs.begin(); it != m_childs.end(); ++it) {
        if (*it == component) {
            m_childs.erase(it);
            break;
        }
    }
}

Component *Panel::getChild(int i) {
    if (i < m_childs.size()) {
        return m_childs[i];
    } else {
        return nullptr;
    }
}

到了這里,關于《設計模式的藝術》筆記 - 組合模式的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 《設計模式的藝術》筆記 - 代理模式

    ? ? ? ? 代理模式是給某一個對象提供一個代理,并由代理對象控制對原對象的引用。代理模式是一種對象結構型模式。 myclass.h myclass.cpp main.cpp ? ? ? ? 1.?代理模式能夠協(xié)調調用者和被調用者,在一定程度上降低了系統(tǒng)的耦合度,滿足迪米特法則。 ? ? ? ? 2.?客戶端可以

    2024年01月19日
    瀏覽(15)
  • 《設計模式的藝術》筆記 - 橋接模式

    ? ? ? ? 橋接模式將抽象部分與其實現(xiàn)部分分離,使它們都可以獨立地變化。它是一種對象結構型模式,又稱為柄體模式或接口模式 myclass.h myclass.cpp main.cpp ? ? ? ? 1.?分離抽象接口及其實現(xiàn)部分。橋接模式使用“對象間的關聯(lián)關系”解耦了抽象和實現(xiàn)之間固有的綁定關系,

    2024年01月18日
    瀏覽(23)
  • 《設計模式的藝術》筆記 - 策略模式

    ? ? ? ? 策略模式定義一系列算法類,將每一個算法封裝起來,并讓它們可以相互替換。策略模式讓算法獨立于使用它的客戶而變化,也稱為政策模式。策略模式是一種對象行為模式。 myclass.h myclass.cpp main.cpp ? ? ? ? 1.?策略模式提供了對開閉原則的完美支持。用戶可以在不

    2024年01月25日
    瀏覽(23)
  • 《設計模式的藝術》筆記 - 原型模式

    ? ? ? ? 使用原型實例指定創(chuàng)建對象的種類,并且通過克隆這些原型創(chuàng)建新的對象。原型模式是一種對象創(chuàng)建型模式。 myclass.h myclass.cpp main.cpp ? ? ? ? 優(yōu)點: ? ? ? ? 1.?當創(chuàng)建新的對象實例較為復雜時,使用原型模式可以簡化對象的創(chuàng)建過程,通過復制一個已有實例可以提

    2024年01月19日
    瀏覽(23)
  • 《設計模式的藝術》筆記 - 迭代器模式

    ? ? ? ? 迭代器模式提供一種方法來訪問聚合對象,而不用暴露這個對象的內部表示,其別名為游標(Cursor)。迭代器模式是一種對象行為型模式。 myclass.h myclass.cpp main.cpp ? ? ? ? 1. 支持以不同的方式遍歷一個聚合對象,在同一個聚合對象上可以定義多種遍歷方式。在迭代

    2024年01月24日
    瀏覽(21)
  • 《設計模式的藝術》筆記 - 簡單工廠模式

    《設計模式的藝術》筆記 - 簡單工廠模式

    ? ? ? ? 定義一個工廠類,它可以根據(jù)參數(shù)的不同返回不同類的實例,被創(chuàng)建的實例通常都具有相同的父類。因為在簡單工廠模式中用于創(chuàng)建實例的方法是靜態(tài)方法,因此簡單工廠模式又被稱為靜態(tài)工廠方法模式,屬于類創(chuàng)建型模式 ? ? ? ? 將Factory合并到父類Product中,此時

    2024年01月16日
    瀏覽(21)
  • 《設計模式的藝術》筆記 - 單例模式

    ????????單例模式優(yōu)點是可以確保系統(tǒng)中只存在單個對象實例,缺點是不便擴展,一定程度上違背單一原則,既提供業(yè)務方法,又提供創(chuàng)建對象方法 ? ? ? ? 在類加載的時候就創(chuàng)建好對象,獲取對象時直接返回即可 ? ? ? ? 在類加載的時候沒有創(chuàng)建對象,第一次獲取對象

    2024年02月02日
    瀏覽(18)
  • 《設計模式的藝術》筆記 - 抽象工廠模式

    ? ? ? ? 提供了一個創(chuàng)建一系列相關或相互依賴的對象的接口,而無須指定它們具體的類。抽象工廠模式又稱為Kit模式,它是一種對象創(chuàng)建型模式。 ? ? ? ? 在抽象工廠模式中,每個具體工廠都提供了多個工廠方法用于產(chǎn)生多種不同類型的產(chǎn)品,這些產(chǎn)品構成了一個產(chǎn)品族。

    2024年01月16日
    瀏覽(27)
  • 《設計模式的藝術》筆記 - 建造者模式

    ? ? ? ? 建造者模式將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創(chuàng)建不同的表示。建造這模式是一種對象創(chuàng)建型模式。 myclass.h myclass.cpp main.cpp ? ? ? ? 優(yōu)點: ? ? ? ? 1.?在建造者模式中,客戶端不必知道產(chǎn)品內部組成的細節(jié),將產(chǎn)品本身與產(chǎn)品的創(chuàng)建

    2024年01月16日
    瀏覽(24)
  • 《設計模式的藝術》筆記 - 享元模式

    ? ? ? ? 享元模式運用共享技術有效地支持大量細粒度對象的復用。系統(tǒng)只使用少量的對象,而這些對象都很相似,狀態(tài)變化很小,可以實現(xiàn)對象的多次復用。由于享元模式要求能夠共享的對象必須是細粒度對象,因此它又稱為輕量級模式,是一種對象結構型模式。 myclass.

    2024年01月19日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包