zoukankan      html  css  js  c++  java
  • 设计模式(简述)

    • 简单工厂模式(Simple Factory)-- 创建模式

    简单工厂模式就是由一个工厂类可以根据传入的参数决定创建出不同产品类的实例。

    • 工厂模式(Factory Method)-- 创建模式

    定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。

    一个查询器示例的结构图:

    这里QueryRunner对应于抽象工厂,SybaseQueryRunner和OracleQueryRunner对应于具体工厂。而ResultSet对应于抽象产品,client可以根据需要选择特定的工厂生成对应的ResultSet。

    • 抽象工厂(Abstract Factory)-- 创建模式

    AbstractFactory模式是为创建一组(有多类)相关或依赖的对象提供创建接口。

    比 如游戏中为了创建不同等级的怪物,创建几个不同level的怪物生成器,如LevelOneMonsterFactory, LevelTwoMonsterFactory,它们都是继承自AbstractMonsterFactory的,而游戏中对应有Wolf,Tiger, (这两个类都是抽象类),那么就可能有LevelOneWolf,LevelTwoWolf,LevelOneTiger,LevelTwoTiger。

    那么对应的工厂方法就有factoryWolf(),factoryTiger(),具体如下图:

    • 单件模式(Singleton)-- 创建模式

    单件模式的三个特点:

    1 单例类只可有一个实例。
    2 单例类必须自己创建自己这惟一的实例。
    3 单例类必须给所有其他对象提供这一实例

    • 外观模式(Facade)-- 结构模式

    为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

    上图中Compiler对应于Facade类,为client提供了一个简单的接口。代码如下:

    void Compiler::compile(istream& input, ByteCodeStream& output)
    {
        Scanner scanner(input);
        ProgramNodeBuilder builder;
        Parser parser;

        parser.parse(scanner, builder);

        CodeGenerator generator(output);
        ProgramNode* parseTree = builder.getRootNode();
        parseTree->traverse(generator);
    }

    在Compiler::compile里面使用了子系统的功能,但是给client提供的接口就很简单。

    • 适配器模式(Adapter)-- 结构模式

    将一个类的接口转换成客户希望的另一个接口。Adapter模式可以使原本因为接口不兼容而不能一起工作的类可以一起工作。

    适配器模式分为两种,一,对象适配模式,如下图,二,类适配模式(使用多重继承来重用现有的类,我觉得这种方式不好)

    Table. Comparing the Facade Pattern with the Adapter Pattern
     Facade Adapter
    Are there preexisting classes? Yes Yes
    Is there an interface we must design to? No Yes
    Does an object need to behave polymorphically? No Probably
    Is a simpler interface needed? Yes No

    • 桥接模式(Bridge Pattern)-- 行为模式

    将抽象部分和它的实现部分分离,使它们度能独立的变化。

    The Bridge Pattern: Key Features

    Intent Decouple a set of implementations from the set of objects using them.
    Problem The derivations of an abstract class must use multiple implementations without causing an explosion in the number of classes.
    Solution Define an interface for all implementations to use and have the derivations of the abstract class use that.
    Participants and Collaborators The Abstraction defines the interface for the objects being implemented. The Implementor defines the interface for the specific implementation classes. Classes derived from the Abstraction use classes derived from the Implementor without knowing which particular ConcreteImplementor is in use.
    Consequences The decoupling of the implementations from the objects that use them increases extensibility. Client objects are not aware of implementation issues.
    Implementation
    • Encapsulate the implementations in an abstract class.

    • Contain a handle to it in the base class of the abstraction being implemented.

      Note: In Java, you can use interfaces instead of an abstract class for the implementation.

    GoF Reference Pages 151?62.

    Standard, simplified view of the Bridge pattern.



    • 模板方法(Template Method)-- 行为模式

    定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。

    • 状态模式(State)-- 行为模式

    允许一个对象在器内部状态改变时改变它的行为。对象看起来似乎修改了它的类。


  • 相关阅读:
    揭示短线操作宝贵心得
    MFC常用类、成员函数、数组类、Cstring类、CTime类、CPoint类
    A股和B股的区别
    大盘指数的定义及其计算方法
    追涨杀跌法
    成交量变化八规律(旧文有韵)
    蓝筹股、红筹股的含义
    对上市公司进行综合分析
    socket异步笔记
    从WEB SERVICE 上返回大数据量的DATASET
  • 原文地址:https://www.cnblogs.com/kevinwan/p/371689.html
Copyright © 2011-2022 走看看