如何构造代码以仅返回某些成员

问题描述

我想问一下如何构造代码,以便当用户(主程序)没有苹果(hasApple = false)时,用户将无法访问fruitMenu.Apple或{{1} }。

我想让程序在没有苹果的情况下运行,这样就不会有苹果和苹果的价格供用户选择。

fruitMenu.ApplePrice

解决方法

下面的代码可以代表对某些问题的评论。

我们应该使Fruit与其实现OrangeApple尽可能独立。由于它们没有其他逻辑,因此我添加了字段Type

Menu也不应该真正在意其中的内容。通过从其中删除HasAppleHasOrange,代码变得更易于维护-当我们要引入新型水果时,我们唯一需要更改的就是在水果名称中添加新类型

如果未将水果添加到菜单,则水果为空会限制对价格字段的访问

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    public class FruitNames
    {
        public const string Apple = "Apple";
        public const string Orange = "Orange";
    }

    public class Fruit
    {
        public string Type { get; set; }
        public double Price { get; set; }

        public Fruit(string type,double price)
        {
            Type = type;
            Price = price;
        }
    }
    public class Menu
    {
        private List<Fruit> items = new List<Fruit>();

        public IEnumerable<Fruit> GetMenu()
        {
            return items;
        }

        public void Add(Fruit fruit)
        {
            items.Add(fruit);
        }
    }

    public static class MenuExtensions
    {
        public static Menu WithApple(this Menu menu)
        {
            menu.Add(new Fruit(FruitNames.Apple,5));

            return menu;
        }

        public static Menu WithOrange(this Menu menu)
        {
            menu.Add(new Fruit(FruitNames.Orange,10));

            return menu;
        }
        public static Fruit GetFruitOrDefault(this Menu menu,string type)
        {
            return menu.GetMenu().FirstOrDefault(x => x.Type == type);
        }

        public static Fruit GetAppleOrDefault(this Menu menu)
        {
            return menu.GetFruitOrDefault(FruitNames.Apple);
        }

        public static Fruit GetOrangeOrDefault(this Menu menu)
        {
            return menu.GetFruitOrDefault(FruitNames.Orange);
        }
    }

    public class Program
    {
        static void PrintFruit(Fruit fruit)
        {
            Console.Write(fruit.Type);
            Console.Write("\t");
            Console.WriteLine(fruit.Price);
        }

        static void Main(string[] args)
        {
            Menu menu = new Menu();
            Console.WriteLine("Menu without fruits:");

            Fruit apple = menu.GetAppleOrDefault();
            if (apple != null)
            {
                PrintFruit(apple); // won't be printed
            }

            Fruit orange = menu.GetOrangeOrDefault();
            if (orange != null)
            {
                PrintFruit(orange); // won't be printed
            }

            Console.WriteLine("Menu with fruits:");
            menu = menu.WithApple().WithOrange(); // fill menu with items

            apple = menu.GetAppleOrDefault();
            if (apple != null)
            {
                PrintFruit(apple); // will be printed
            }

            orange = menu.GetOrangeOrDefault();
            if (orange != null)
            {
                PrintFruit(orange); // will be printed
            }

            Console.ReadKey();
        }
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...