zoukankan      html  css  js  c++  java
  • 软件工程-Template模式、类适配器、对象适配器的C++实现

    Template模式、类适配器、对象适配器的C++实现

    1、Template:

             

    C++实现代码:

    #include <bits/stdc++.h>
    using namespace std;
    class AbstractClass
    {
    public:
        void TemplateMethod()
        {
            cout<<"PrimitiveOper1() Start"<<endl;
            PrimitiveOper1();
            cout<<"PrimitiveOper1() End"<<endl;
    
            cout<<"PrimitiveOper2() Start"<<endl;
            PrimitiveOper2();
            cout<<"PrimitiveOper2() End"<<endl;
        }
    protected:
        virtual void PrimitiveOper1()
        {
            cout<<"AbstractClass oper1"<<endl;
        }
        virtual void PrimitiveOper2()
        {
            cout<<"AbstractClass oper2"<<endl;
        }
    };
    class ConcreteClass:public AbstractClass
    {
    protected:
        virtual void PrimitiveOper1()
        {
            cout<<"ConcreteClass Oper1"<<endl;
        }
        virtual void PrimitiveOper2()
        {
            cout<<"ConcreteClass Oper2"<<endl;
        }
    };
    int main() {
        AbstractClass * pa=new ConcreteClass();
        pa->TemplateMethod();
        if(pa)
            delete pa;
        return 0;
    }
    View Code

    2、类适配器

            

     C++实现代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Target
    {
    public:
        virtual void Request()
        {
            cout<<"Target::Request"<<endl;
        }
    };
    class Adaptee
    {
    public:
        void SpecificRequest()
        {
            cout<<"Adaptee::SpecificRequest"<<endl;
        }
    };
    class Adapter:public Target,Adaptee
    {
    public:
        void Request()
        {
            Adaptee::SpecificRequest();
        }
    };
    int main() {
        Target *pt=new Adapter();
        pt->Request();
        if(pt)
            delete pt;
        return 0;
    }
    View Code

    3、对象适配器

            

    C++实现代码:

    #include <bits/stdc++.h>
    using namespace std;
    class Target
    {
    public:
        Target(){}
        virtual void Request()
        {
            cout<<"Target::Request"<<endl;
        }
    };
    class Adaptee
    {
    public:
        void SpecificRequest()
        {
            cout<<"Adaptee::SpecificRequest"<<endl;
        }
    };
    class Adapter:public Target,Adaptee
    {
    public:
        Adapter():m_adaptee(new Adaptee){}
        ~Adapter()
        {
           if(m_adaptee!=NULL)
           {
               delete m_adaptee;
           }
        }
        void Request()
        {
            m_adaptee->SpecificRequest();
        }
    private:
        Adaptee *m_adaptee;
    };
    int main() {
        Target *pt=new Adapter();
        pt->Request();
        if(pt)
            delete pt;
        return 0;
    }
    View Code
  • 相关阅读:
    css3动画事件 animationend animationiteration animationstart
    dom对象---增加class属性,去除class属性
    数组的indexOf() 方法
    line-height中的五种取值方式和继承
    html 中line-height相关的四种box模型
    真正的能理解CSS中的line-height,height与line-height
    background-size:contain与cover的区别
    激活win10系统
    table-layout:fixed 应用
    js数组fill()方法
  • 原文地址:https://www.cnblogs.com/acmer-hmin/p/12903983.html
Copyright © 2011-2022 走看看