zoukankan      html  css  js  c++  java
  • 【设计模式】牛市股票还会亏钱 外观模式

    一,概述

           外观模式是软件工程中常用的一种软件设计模式。它为子系统中的一组接口提供一个统一的高层接口。使用子系统更容易使用。


    二,例如

            股民买股票,大部分散户自己买股票、国债、房地产,然后到时机抛售。这样就要求每个股民对每支股票都和了解才能保证赚钱,就是说每个股民都要跟一大堆股票打交道。

            而更加优化的方法是,股民将钱交给专业的理财机构,定期获利达到双赢。这样股民只需要跟理财机构打交道即可。就是所谓的把一组股票通过理财机构这一个统一的接口调用。

     

    1)单个股民投资基金代码

    #include <iostream>
    using namespace std;
    
        //股票1
    class Stock1
    {
    public:
            //卖股票
            void Sell()
            {
                cout<<" 股票1卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票1买入"<<endl;
            }
    };
    
        //股票2
    class Stock2
    {
    	public :
            //卖股票
            void Sell()
            {
                cout<<" 股票2卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票2买入"<<endl;
            }
    };
    
        //股票3
    class Stock3
    {
    public:
            //卖股票
            void Sell()
            {
                cout<<" 股票3卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票3买入"<<endl;
            }
    };
    
        //国债1
    class NationalDebt1
    {
    public:
            //卖国债
            void Sell()
            {
                cout<<" 国债1卖出"<<endl;
            }
    
            //买国债
            void Buy()
            {
                cout<<" 国债1买入"<<endl;
            }
    };
    
        //房地产1
    class Realty1
    {
       	public:
            //卖房地产
             void Sell()
            {
                cout<<" 房产1卖出"<<endl;
            }
    
            //买房地产
            void Buy()
            {
                cout<<" 房产1买入"<<endl;
            }
        };
        
    int main()
    {
    
                Stock1 *gu1 = new Stock1();
                Stock2 *gu2 = new Stock2();
                Stock3 *gu3 = new Stock3();
                NationalDebt1 *nd1 = new NationalDebt1();
                Realty1 *rt1 = new Realty1();
    
                gu1->Buy();
                gu2->Buy();
                gu3->Buy();
                nd1->Buy();
                rt1->Buy();
    
                gu1->Sell();
                gu2->Sell();
                gu3->Sell();
                nd1->Sell();
                rt1->Sell();
    
                
    
    }
        

    2)有了理财机构投资(外观模式)

           Fund资金管理类,直接管理股票的买卖,股民只需要调用资金管理类的买进、卖出就可以了。大大简化了股民理财负担。

    #include <iostream>
    using namespace std;
    
    
    
        //股票1
        class Stock1
        {
        	public:
            //卖股票
            void Sell()
            {
                cout<<" 股票1卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票1买入"<<endl;
            }
        };
    
        //股票2
        class Stock2
        {
        	public:
            //卖股票
            void Sell()
            {
                cout<<" 股票2卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票2买入"<<endl;
            }
        };
    
        //股票3
        class Stock3
        {
        	public:
            //卖股票
            void Sell()
            {
                cout<<" 股票3卖出"<<endl;
            }
    
            //买股票
            void Buy()
            {
                cout<<" 股票3买入"<<endl;
            }
        };
    
        //国债1
        class NationalDebt1
        {
        	public:
            //卖国债
            void Sell()
            {
                cout<<" 国债1卖出"<<endl;
            }
    
            //买国债
            void Buy()
            {
                cout<<" 国债1买入"<<endl;
            }
        };
    
        //房地产1
        class Realty1
        {
        	public:
            //卖房地产
            void Sell()
            {
                cout<<" 房产1卖出"<<endl;
            }
    
            //买房地产
            void Buy()
            {
                cout<<" 房产1买入"<<endl;
            }
        };
    
        class Fund
        {
       	 private:
            Stock1 *gu1;
            Stock2 *gu2;
            Stock3 *gu3;
            NationalDebt1 *nd1;
            Realty1 *rt1;
    
          public:
            Fund()
            {
                gu1 = new Stock1();
                gu2 = new Stock2();
                gu3 = new Stock3();
                nd1 = new NationalDebt1();
                rt1 = new Realty1();
            }
    
           void BuyFund()
            {
                gu1->Buy();
                gu2->Buy();
                gu3->Buy();
                nd1->Buy();
                rt1->Buy();
            }
    
            void SellFund()
            {
                gu1->Sell();
                gu2->Sell();
                gu3->Sell();
                nd1->Sell();
                rt1->Sell();
            }
    
        };
    
        
    int  main()
    {
                Fund *jijin = new Fund();
    
                jijin->BuyFund();
                jijin->SellFund();
    }
    
        


    三,外观模式总结(C#)

        class SubSystemOne
        {
            public void MethodOne()
            {
                Console.WriteLine(" 子系统方法一");
            }
        }
    
        class SubSystemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine(" 子系统方法二");
            }
        }
    
        class SubSystemThree
        {
            public void MethodThree()
            {
                Console.WriteLine(" 子系统方法三");
            }
        }
    
        class SubSystemFour
        {
            public void MethodFour()
            {
                Console.WriteLine(" 子系统方法四");
            }
        }
    
        class Facade
        {
            SubSystemOne one;
            SubSystemTwo two;
            SubSystemThree three;
            SubSystemFour four;
    
            public Facade()
            {
                one = new SubSystemOne();
                two = new SubSystemTwo();
                three = new SubSystemThree();
                four = new SubSystemFour();
            }
    
            public void MethodA()
            {
                Console.WriteLine("\n方法组A() ---- ");
                one.MethodOne();
                two.MethodTwo();
                four.MethodFour();
            }
    
            public void MethodB()
            {
                Console.WriteLine("\n方法组B() ---- ");
                two.MethodTwo();
                three.MethodThree();
            }
        }
    
    int main()
    {
                Facade facade = new Facade();
    
                facade.MethodA();
                facade.MethodB();
    
    }
        

    四,何时使用外观模式

     

          1.在设计阶段初期,要有意识的将不同的两个层分离,比如经典的三层架构,就需要考虑在数据访问层和业务逻辑层、业务逻辑层和表示层的层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合大大降低。

          2.在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂。增加外观Facade可以提供一个简单的接口,减少它们之间的依赖

          3.在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。


     

  • 相关阅读:
    初识你Swift【上篇】
    初识你Swift【下篇】
    单元测试基础
    时间都去哪了?
    iOS App上线的秘密
    mysql系列——DQL常见操作汇总(四)
    Get和Post请求有什么区别?
    SpringBoot2+WebSocket之聊天应用实战
    OCR识别
    mysql系列——子查询(非常重要)(八)
  • 原文地址:https://www.cnblogs.com/secbook/p/2654981.html
Copyright © 2011-2022 走看看