找柳市做网站,转转假网站怎么做,泰安人才市场招聘网,代理加速器C++访问者模式
一、模式痛点:当if-else成为维护噩梦
开发动物园管理系统,最初的需求很简单:
class Animal {};
class Cat : public Animal {};
class Dog : public Animal {};// 处理动物叫声
void makeSound(Animal* a) {if (auto c = dynamic_castCat*(a)) {st…
C++访问者模式
一、模式痛点:当if-else成为维护噩梦
开发动物园管理系统,最初的需求很简单:
class Animal {};
class Cat : public Animal {};
class Dog : public Animal {};// 处理动物叫声
void makeSound(Animal* a) {if (auto c = dynamic_castCat*(a)) {std::cout "Meow!\n";} else if (auto d = dynamic_castDog*(a)) {std::cout "Woof!\n";}
}当新增喂养功能时,代码迅速腐化:
void feedAnimal(Animal* a) {if (auto c = dynamic_castCat*(a)) {std::cout "Give fish\n";} else if (auto d = dynamic_castDog*(a)) {std::cout "Give bone\n";}
}系统痛点:
每新增功能就要修改所有类型判断逻辑类型检查与业务逻辑高度耦合违反开放封闭原则(OCP)二、模式实现:双分派的艺术
2.1 类图精髓 #mermaid-svg-dbwVpnIRKAByxCBL {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-dbwVpnIRKAByxCBL .error-icon{fill:#552222;}#mermaid-svg-dbwVpnIRKAByxCBL .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dbwVpnIRKAByxCBL .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-dbwVpnIRKAByxCBL .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dbwVpnIRKAByxCBL .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dbwVpnIRKAByxCBL .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dbwVpnIRKAByxCBL .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dbwVpnIRKAByxCBL .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dbwVpnIRKAByxCBL .marker.cross{stroke:#333333;}#mermaid-svg-dbwVpnIRKAByxCBL svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dbwVpnIRKAByxCBL g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-dbwVpnIRKAByxCBL g.classGroup text .title{font-weight:bolder;}#mermaid-svg-dbwVpnIRKAByxCBL .nodeLabel,#mermaid-svg-dbwVpnIRKAByxCBL .edgeLabel{color:#131300;}#mermaid-svg-dbwVpnIRKAByxCBL .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-dbwVpnIRKAByxCBL .label text{fill:#131300;}#mermaid-svg-dbwVpnIRKAByxCBL .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-dbwVpnIRKAByxCBL .classTitle{font-weight:bolder;}#mermaid-svg-dbwVpnIRKAByxCBL .node rect,#mermaid-svg-dbwVpnIRKAByxCBL .node circle,#mermaid-svg-dbwVpnIRKAByxCBL .node ellipse,#mermaid-svg-dbwVpnIRKAByxCBL .node polygon,#mermaid-svg-dbwVpnIRKAByxCBL .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dbwVpnIRKAByxCBL .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-dbwVpnIRKAByxCBL g.clickable{cursor:pointer;}#mermaid-svg-dbwVpnIRKAByxCBL g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-dbwVpnIRKAByxCBL g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-dbwVpnIRKAByxCBL .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-dbwVpnIRKAByxCBL .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-dbwVpnIRKAByxCBL .dashed-line{stroke-dasharray:3;}#mermaid-svg-dbwVpnIRKAByxCBL #compositionStart,#mermaid-svg-dbwVpnIRKAByxCBL .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #compositionEnd,#mermaid-svg-dbwVpnIRKAByxCBL .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #dependencyStart,#mermaid-svg-dbwVpnIRKAByxCBL .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #dependencyStart,#mermaid-svg-dbwVpnIRKAByxCBL .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #extensionStart,#mermaid-svg-dbwVpnIRKAByxCBL .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #extensionEnd,#mermaid-svg-dbwVpnIRKAByxCBL .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #aggregationStart,#mermaid-svg-dbwVpnIRKAByxCBL .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL #aggregationEnd,#mermaid-svg-dbwVpnIRKAByxCBL .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-dbwVpnIRKAByxCBL .edgeTerminals{font-size:11px;}#mermaid-svg-dbwVpnIRKAByxCBL :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}