zoukankan      html  css  js  c++  java
  • 设计模式 外观模式

    首先写一个最基本的投资类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Face
    {
    
    
            class Stock1
            {
                public void Sell()
                {
                    Console.WriteLine("股票1卖出");
                }
                public void Buy()
                {
                    Console.WriteLine("股票1买入");
                }
            }
    
            class Stock2
            {
                public void Sell()
                {
                    Console.WriteLine("股票2卖出");
                }
                public void Buy()
                {
                    Console.WriteLine("股票2买入");
                }
            }
    
            class Stock3
            {
                public void Sell()
                {
                    Console.WriteLine("股票3卖出");
                }
                public void Buy()
                {
                    Console.WriteLine("股票3买入");
                }
            }
            class NationDebt1
            {
                public void Sell()
                {
                    Console.WriteLine("国债1卖出");
                }
                public void Buy()
                {
                    Console.WriteLine("国债1买入");
                }
            }
    
            class Realty1
            {
                public void Sell()
                {
                    Console.WriteLine("房地产1卖出");
                }
                public void Buy()
                {
                    Console.WriteLine("房地产1买入");
                }
            }
       
    }
    
    

    然后书写一个基金类,隐藏掉。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Face
    {
        class Fund
        {
            Stock1 gu1;
            Stock2 gu2;
            Stock3 gu3;
            NationDebt1 nd1;
            Realty1 rt1;
    
            public Fund()
            {
                gu1 = new Stock1();
                gu2 = new Stock2();
                gu3 = new Stock3();
                nd1 = new NationDebt1();
                rt1 = new Realty1();
            }
    
            public void BuyFund()
            {
                gu1.Buy();
                gu2.Buy();
                gu3.Buy();
                nd1.Buy();
                rt1.Buy();
            }
    
            public void SellFund()
            {
                gu1.Sell();
                gu2.Sell();
                gu3.Sell();
                nd1.Sell();
                rt1.Sell();
            }
    
        }
    }
    
    

    最后写测试类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Face
    {
        class Program
        {
            static void Main(string[] args)
            {
                Fund fund = new Fund();
                fund.BuyFund();
                fund.SellFund();
                Console.Read();
    
            }
        }
    }
    
    
  • 相关阅读:
    Iso-seq 必备基础
    html 段落
    html 标题
    html 简介
    motiMaker 软件安装测试
    ggplot2 提取stat计算出来的数据
    R包 randomForest 进行随机森林分析
    AJAX应用【股票案例、验证码校验】
    Servlet第二篇【Servlet调用图、Servlet细节、ServletConfig、ServletContext】
    Servlet第一篇【介绍Servlet、HTTP协议、WEB目录结构、编写入门Servlet程序、Servlet生命周期】
  • 原文地址:https://www.cnblogs.com/yufenghou/p/6024995.html
Copyright © 2011-2022 走看看