标签:des com http blog class style img div code java size
索引
将对一个类的所有对象的管理封装到一个单独的管理器类中。
这使得管理职责的变化独立于类本身,并且管理器还可以为不同的类进行重用。
Encapsulates management of a class’s objects into a separate manager object.
This allows variation of management functionality independent of the class and the manager’s reuse for different classes.
Subject
Manager
Client
当以下情况成立时可以使用 Manager 模式:
1 namespace ManagerPattern.Implementation1 2 { 3 public class Book 4 { 5 public Book(string isbn, string authors, string title) 6 { 7 this.ISBN = isbn; 8 this.Authors = authors; 9 this.Title = title; 10 } 11 12 public string ISBN { get; private set; } 13 public string Authors { get; private set; } 14 public string Title { get; private set; } 15 16 public string Publisher { get; set; } 17 public Image Cover { get; set; } 18 19 public string GetTableOfContents() 20 { 21 return "something"; 22 } 23 } 24 25 public class BookManager 26 { 27 private Dictionary<string, Book> _books 28 = new Dictionary<string, Book>(); 29 30 public BookManager() 31 { 32 } 33 34 public Book AddBook(string isbn, string authors, string title) 35 { 36 Book book = new Book(isbn, authors, title); 37 _books.Add(book.ISBN, book); 38 return book; 39 } 40 41 public Book GetBookByISBN(string isbn) 42 { 43 Book book; 44 _books.TryGetValue(isbn, out book); 45 return book; 46 } 47 48 public IEnumerable<Book> FindBooksOfAuthor(string author) 49 { 50 return _books.Values.Where(b => b.Authors.Contains(author)); 51 } 52 } 53 54 public class Client 55 { 56 public void TestCase1() 57 { 58 BookManager manager = new BookManager(); 59 manager.AddBook("xxxx-xxxx-xxxx", "Dennis Gao", "Good Man"); 60 Book book = manager.GetBookByISBN("xxxx-xxxx-xxxx"); 61 book.GetTableOfContents(); 62 } 63 } 64 }
《设计模式之美》为 Dennis Gao 发布于博客园的系列文章,任何未经作者本人同意的人为或爬虫转载均为耍流氓。
设计模式之美:Manager(管理器),布布扣,bubuko.com
标签:des com http blog class style img div code java size
原文地址:http://www.cnblogs.com/gaochundong/p/design_pattern_manager.html