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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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})");
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// <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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
请选择:
1: 获取并显示图书id 1
2: 找不到书
3: 书是不存在的
4: 创建一个无序的账簿
5: 把书陈列在别的地方
6: 创建一本书
7: 列出所有书
0: 退出
 
 
书名: 一些很酷的电脑书 (1)
书名: geovindu (2)
书名: 涂聚文 (3)
按回车键返回主菜单.

  

posted @   ®Geovin Du Dream Park™  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-10-28 百度地图查询两地里程
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示