设计模式
设计模式
西笑生
西笑生
展开
-
switch语句的优化
可以使用 状态模式 或者 策略模式.如果用多态 有些多余 优化如下。switch语句的优化。原创 2017-07-10 19:40:10 · 1097 阅读 · 0 评论 -
C++ 单例模式
flyfish 2014-12-16编写一个Singleton类class Singleton(){}; 该类是空类经过编译器处理后,C++编译器默认编写的函数代码如下class Singleton() {public: Singleton() {} // 1 构造函数 Singleton(const原创 2014-12-16 21:36:30 · 3885 阅读 · 0 评论 -
pimpl idiom
pimpl idiomflyfish 2014-9-30pimpl是Pointer to implementation的缩写为什么要使用pimpl1最小化编译依赖2接口与实现分离3可移植pimpl idiom也被称作Cheshire Cat , Compiler Firewall idiom.,d-pointer这个技术在设计模式中作为桥接模式(Brid原创 2014-09-30 17:02:05 · 1151 阅读 · 0 评论 -
工厂方法模式(Factory method pattern)
工厂方法模式(Factory method pattern)flyfish 2015-7-4工厂方法模式正式定义 - 引用《HeadFirst 设计模式》 Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a原创 2015-07-04 19:19:26 · 1349 阅读 · 0 评论 -
if else语句的优化
if else语句的优化flyfish假设假设类A的成员函数Process处理的type比较多,if else语句就比较多void A::Process(int type, std::string s){ if (type == 1) { Function1(s); } else if (type == 2) { Functio原创 2017-07-13 17:43:48 · 1285 阅读 · 1 评论 -
Yoda Notation
Yoda Notationflyfish 2016-2-22《星球大战》里有个 Yoda 大师 他说话是这样子的: Yoda: Bring him here. Question him we will. Yoda: Around the survivors a perimeter create. Yoda: If Dooku escapes, rally more sy原创 2016-02-22 16:13:07 · 769 阅读 · 0 评论 -
建造者模式(Builder Pattern)
建造者模式(Builder Pattern)flyfish 2016-1-22 C++实现class Product{public: void DoSomething(){}};class Builder{public: virtual void SetPart1(){};//处理Product的第一部分 virtual void SetPart2(){};//处原创 2016-01-22 16:13:31 · 552 阅读 · 0 评论 -
里氏替换原则(Liskov's Substitution Principle)
里氏替换原则(Liskov’s Substitution Principle)flyfish解释 All the time we design a program module and we create some class hierarchies.Then we extend some classes creating some derived classes.We must m原创 2017-07-20 11:53:28 · 2231 阅读 · 0 评论 -
抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式(Abstract Factory Pattern)flyfish 2016-1-21 本文编程语言采用C+++实现,参考《设计模式之禅》中的Java实现,#pragma once//抽象产品类Aclass AbstractProductA{public: void ShareMethod(); virtual void DoSomething()=0;};原创 2016-01-21 12:00:05 · 529 阅读 · 0 评论 -
状态模式(State Pattern)
状态模式(State Pattern)flyfish#include "stdafx.h"#include <iostream>class role;class employee{public: virtual void output(role *role_ptr) = 0;};class engineer : public employee{public: virtua原创 2016-08-30 16:18:01 · 419 阅读 · 0 评论 -
开闭原则(Open - Closed Principle)
开闭原则(Open - Closed Principle)C++ 描述 开闭原则坏的示例,,增加图形的时候GraphicEditor需要变化class Shape {public: int m_type;};class Rectangle :public Shape{ Rectangle() { Shape::m_type = 1; }}原创 2017-07-18 18:05:48 · 932 阅读 · 0 评论 -
外观模式(Facade Pattern)
外观模式(Facade Pattern)flyfish Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher - level interface that makes the subsystem easier to use. Wrap a complicate原创 2017-07-25 18:56:43 · 318 阅读 · 0 评论 -
接口隔离原则(Interface segregation principle)
接口隔离原则(Interface segregation principle)flyfish一些对接口隔离原则的描述 Clients should not be forced to depend upon interfaces that they don’t use.Client不应该被迫依赖他们不使用的接口 Many client specific interfaces are bett原创 2017-07-19 19:35:17 · 1408 阅读 · 0 评论 -
策略模式(Strategy Pattern)
策略模式flyfish#include "stdafx.h"#include <iostream>#include <map>using namespace std;typedef enum StrategyType{ StrategyA, StrategyB, StrategyC}STRATEGYTYPE;class Strategy{public:原创 2017-07-19 14:55:01 · 489 阅读 · 0 评论 -
职责链模式(Chain of Responsibility)
职责链模式(Chain of Responsibility)flyfish#include "stdafx.h"#include #define SAFE_DELETE(p) if (p) { delete p; p = nullptr; }class AuditingRequest{public: AuditingRequest(int nMoney) : m_n原创 2018-01-09 11:55:18 · 218 阅读 · 0 评论 -
单一职责原则(Single Responsibility Principle)
单一职责原则(Single Responsibility Principle)flyfish原文地址动机 在此背景中,一个职责是考虑因一个原因而改变,如果一个类,改变它有两个原因。我们应该根据功能性分成两个类。每个类处理一个职责 在未来当我们需要做出某种改变时,只需要修改该类就可以了。当我们去改变一个具有多个职责的类时,这个变动可能会影响该类的相关职责的其他功能单一职责是简单直观的原则,但是在实原创 2017-07-17 12:44:30 · 502 阅读 · 0 评论 -
原型模式(Prototype Pattern)
原型模式(Prototype Pattern)flyfishC++实现class Prototype{ Prototype(){} virtual ~Prototype(){} virtual Prototype * Clone() = 0; //实现由基类的派生类完成};class ConcretePrototype : public Prototype{publ原创 2016-01-27 17:12:31 · 555 阅读 · 0 评论 -
迪米特法则(Law of Demeter)
迪米特法则(Law of Demeter)flyfish第一个例子 Dont Confuse Your Dog “If you want your dog to run, do you talk to your dog or to each leg? The moral : Change the state of a contained object only through the原创 2017-07-25 10:23:42 · 919 阅读 · 0 评论 -
依赖倒置原则 (Dependency Inversion Principle)
依赖倒置原则 (Dependency Inversion Principle)flyfishA. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details sh原创 2017-07-19 20:45:50 · 1078 阅读 · 0 评论 -
装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern)flyfish 动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式比生成子类更为灵活。 attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alt原创 2016-02-02 14:00:09 · 487 阅读 · 0 评论 -
模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern)flyfish 2016-1-21本文编程语言采用C+++实现,参考《设计模式之禅》中的Java实现 define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclas原创 2016-01-21 17:34:15 · 539 阅读 · 0 评论 -
软件设计原则概述
软件设计原则概述flyfishDesign Principles(SOLID) Single Responsibility Principle(单一职责原则) Open Close Principle(开闭原则) Liskov’s Substitution Principle(Liskov替换原则) Interface Segregation Principle(接口隔离原则) Depen原创 2017-07-20 17:46:01 · 563 阅读 · 0 评论 -
适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern) flyfish Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible原创 2016-01-29 11:10:59 · 431 阅读 · 0 评论