///
<summary>
///
借阅者角色定义
///
</summary>
public
interface
IBorrower : IRole
<
UniqueId
>
{
IEnumerable
<
IBorrowedBook
>
BorrowedBooks {
get
; }
//
借了哪些书
void
BorrowBook(Book book);
//
借书行为
Book ReturnBook(UniqueId bookId);
//
还书行为
}
///
<summary>
///
图书馆角色定义
///
</summary>
public
interface
ILibrary : IRole
<
UniqueId
>
{
IEnumerable
<
Book
>
Books {
get
; }
//
总共有哪些书
Book TakeBook(UniqueId bookId);
//
书的出库
void
PutBook(Book book);
//
书的入库
}
///
<summary>
///
被借的书角色定义
///
</summary>
public
interface
IBorrowedBook : IRole
<
UniqueId
>
{
Book Book {
get
; }
//
书
DateTime BorrowedTime {
get
; }
//
被借时间
}
///
<summary>
///
借书场景
///
</summary>
public
class
BorrowBooksContext
{
private
ILibrary library;
//
场景参与者角色1:图书馆角色
private
IBorrower borrower;
//
借书参与者角色2:借阅者角色
public
BorrowBooksContext(ILibrary library, IBorrower borrower)
{
this
.library
=
library;
this
.borrower
=
borrower;
}
///
<summary>
///
启动借书场景,各个场景参与者开始进行交互
///
</summary>
public
void
Interaction(IEnumerable
<
UniqueId
>
bookIds)
{
foreach
(var bookId
in
bookIds)
{
borrower.BorrowBook(library.TakeBook(bookId));
//
}
}
}
///
<summary>
///
还书场景
///
</summary>
public
class
ReturnBooksContext
{
private
ILibrary library;
private
IBorrower borrower;
public
ReturnBooksContext(ILibrary library, IBorrower borrower)
{
this
.library
=
library;
this
.borrower
=
borrower;
}
public
void
Interaction(IEnumerable
<
UniqueId
>
bookIds)
{
foreach
(var bookId
in
bookIds)
{
library.PutBook(borrower.ReturnBook(bookId));
}
}
}