CSharp: The single-responsibility principle (SRP) in donet core 6

Single Responsibility Principle SOLID Design Patterns
SOLID is an acronym for five principles of architecture.
S – Single Responsibility Principle
O – Open Close Principle
L – Liskov Substitution Principle
I –Interface Segregation Principle
D – Dependency Inversion Principle

 

namespace SRP;

/// <summary>
/// 书,geovindu
/// </summary>
public class Book
{
    /// <summary>
    /// ID
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    ///书的标题,书名
    /// </summary>
    public string? Title { get; set; }
}

/// <summary>
/// 书店
/// </summary>
public class BookStore
{

    /// <summary>
    /// 
    /// </summary>
    private static int _lastId = 0;
    /// <summary>
    /// 
    /// </summary>
    private static List<Book> _books;

    /// <summary>
    /// 
    /// </summary>
    public static int NextId => ++_lastId;
    /// <summary>
    /// 
    /// </summary>
    static BookStore()
    {
        _books = new List<Book>
                {
                    new Book
                    {
                        Id = NextId,
                        Title = "一些很酷的电脑书"
                    }
                };
    }
    /// <summary>
    /// 书列表
    /// </summary>
    public IEnumerable<Book> Books => _books;
    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="book"></param>
    public void Save(Book book)
    {
        // Create the book when it does not exist, 
        // otherwise, find its index and replace it 
        // by the specified book.
        if (_books.Any(x => x.Id == book.Id))
        {
            var index = _books.FindIndex(x => x.Id == book.Id);
            _books[index] = book;
        }
        else
        {
            _books.Add(book);
        }
    }
    /// <summary>
    /// 加载指定ID的书
    /// </summary>
    /// <param name="bookId"></param>
    /// <returns></returns>
    public Book? Load(int bookId)
    {
        return _books.FirstOrDefault(x => x.Id == bookId);
    }
}
/// <summary>
/// 出版社
/// </summary>
public class BookPresenter
{
    /// <summary>
    /// 显示书
    /// </summary>
    /// <param name="book"></param>
    public void Display(Book book)
    {
        Console.WriteLine($"书名: {book.Title} ({book.Id})");
    }
}

  

/// <summary>
/// 
/// </summary>
public class Program
{

    /// <summary>
    /// 
    /// </summary>
    private static readonly BookStore _bookStore = new();
    /// <summary>
    /// 
    /// </summary>
    private static readonly BookPresenter _bookPresenter = new();
    /// <summary>
    /// 
    /// </summary>
    /// <param name="args"></param>
    public static void Main(string[] args)
    {
        var run = true;
        do
        {
            Console.Clear();
            Console.WriteLine("请选择:");
            Console.WriteLine("1: 获取并显示图书id 1");
            Console.WriteLine("2: 找不到书");
            Console.WriteLine("3: 书是不存在的");
            Console.WriteLine("4: 创建一个无序的账簿");
            Console.WriteLine("5: 把书陈列在别的地方");
            Console.WriteLine("6: 创建一本书");
            Console.WriteLine("7: 列出所有书");
            //...
            Console.WriteLine("0: 退出");

            var input = Console.ReadLine();
            Console.Clear();
            try
            {
                switch (input)
                {
                    case "1":
                        FetchAndDisplayBook();
                        break;
                    case "2":
                        FailToFetchBook();
                        break;
                    case "3":
                        BookDoesNotExist();
                        break;
                    case "4":
                        CreateOutOfOrderBook();
                        break;
                    case "5":
                        DisplayTheBookSomewhereElse();
                        break;
                    case "6":
                        CreateBook();
                        break;
                    case "7":
                        ListAllBooks();
                        break;
                    case "0":
                        run = false;
                        break;
                    default:
                        Console.WriteLine("无效的选择!");
                        break;
                }
                Console.WriteLine("按回车键返回主菜单.");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("出现以下异常,请按回车继续:");
                Console.WriteLine(ex);
                Console.ReadLine();
            }
        } while (run);
    }
    /// <summary>
    /// 
    /// </summary>
    private static void FetchAndDisplayBook()
    {
        var book = _bookStore.Load(1);
        _bookPresenter.Display(book!);
    }
    /// <summary>
    /// 
    /// </summary>
    private static void FailToFetchBook()
    {
        // This cannot happen anymore, this has been fixed automatically.
    }
    /// <summary>
    /// 
    /// </summary>
    private static void BookDoesNotExist()
    {
        var book = _bookStore.Load(999);
        if (book == null)
        {
            // Book does not exist
        }
    }
    /// <summary>
    /// 
    /// </summary>
    private static void CreateOutOfOrderBook()
    {
        var book = new Book
        {
            Id = 4, // this value is not enforced by anything and will be overriden at some point.
            Title = "有些是坏的书"
        };
        _bookStore.Save(book);
        _bookPresenter.Display(book);
    }
    /// <summary>
    /// 
    /// </summary>
    private static void DisplayTheBookSomewhereElse()
    {
        Console.WriteLine("这现在是可能的,但我们需要一个新的出版人; 还不是100 % !");
    }

    /// <summary>
    /// 
    /// </summary>
    private static void CreateBook()
    {
        Console.Clear();
        Console.WriteLine("请输入书名: ");
        var title = Console.ReadLine();
        var book = new Book { Id = BookStore.NextId, Title = title };
        _bookStore.Save(book);
    }
    /// <summary>
    /// 
    /// </summary>
    private static void ListAllBooks()
    {
        foreach (var book in _bookStore.Books)
        {
            _bookPresenter.Display(book);
        }
    }

  

输出:

请选择:
1: 获取并显示图书id 1
2: 找不到书
3: 书是不存在的
4: 创建一个无序的账簿
5: 把书陈列在别的地方
6: 创建一本书
7: 列出所有书
0: 退出


书名: 一些很酷的电脑书 (1)
书名: geovindu (2)
书名: 涂聚文 (3)
按回车键返回主菜单.

  

posted @ 2022-10-28 22:30  ®Geovin Du Dream Park™  阅读(18)  评论(0编辑  收藏  举报