The beauty of the design model: Manager)

Source: Internet
Author: User

The beauty of the design pattern: Manager (Manager) index intent structure participant suitability effect implementation method (1): Manager pattern example implementation. The intent is to encapsulate the management of all objects in a class into a separate manager class. This makes the change of management responsibilities independent of the class itself, and the manager can also be reused for different classes. 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. Structure participant Subject domain object. Provides the domain services required by the Client. The Manager class is the only class responsible for creating and destroying Subject objects. It tracks and manages Subject objects. Typical management responsibilities include searching for Subject Objects Based on the specified Key. Because Subject has no reference to the Manager, the Manager can be modified or subclass as needed. The Client obtains the Subject object from the Manager object. Use the Subject domain service. Applicability when the following conditions are met, you can use the Manager mode: When you need to operate on all objects of the same class. When you need to create and destroy objects as needed. When you need to control the object lifecycle. Results can be used to collect statistics on all objects. Management responsibilities can be independent of changes. Management responsibilities can be replaced as needed. Management responsibilities can be reused for implementation (1): Example Implementation of the Manager mode. Copy code 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 BookManager26 {27 private Dictionary <string, Book >_books28 = 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 Client55 {56 public void TestCase1 () 57 {58 BookManager manager = new BookManager (); 59 manager. addBook ("xxxx-xxxx", "Dennis Gao", "Good Man"); 60 Book book = manager. getBookByISBN ("xxxx-xxxx"); 61 book. getTableOfContents (); 62} 63} 64}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.