Object-Oriented Analysis Methods (transfer)

This article is transferred from: http://www.cnblogs.com/netfocus/archive/2011/09/18/2180656.html

 

First share my object-oriented analysis method

  1. Find out some of the most critical business scenarios; generally look for through verbs. For example, in the recruitment system, if a candidate submits a position, it is an application, and the application is a business scenario; if a student takes an exam for a certain course, then the exam is a business. Scenario; a student goes to the library to borrow books, then borrowing books is a business scenario;
  2. For each business scenario, analyze which scenario participants are involved, which participants participate in the form of objects, and which participants participate in the form of services; the reason for distinguishing between objects and services is because sometimes we don't care which participant is, but Only care what the participants are. Generally, in the system, we only care what service it is, and there is generally only one instance of the service in the system; while the object is different, we will care who the object is, that is, which one;
  3. Analyze the basic state characteristics of each scene participant object; the so-called basic state characteristics refer to the state characteristics that the object is born with and has after the object is created from the beginning; the most vivid example is the height and weight of a person, When a person is born, he has the two status characteristics of height and weight; another example is a blog post, which has the status characteristic of content since it was written, but we can modify the content of the blog post at will; but some states Features cannot be modified. For example, the creation time of a blog post cannot be modified once the blog post is created; it should be noted that we should not regard some associated information associated with an object as the basic state feature of the object. For example, someone has a certificate of CET-6, then the relationship between the person and the certificate is a relationship of ownership. The certificate is not an inherent basic state characteristic of a person, but a person who participates in a certain test scene. Come; later I will mention how to think and understand this relationship!
  4. Analyze what role each scene participant object plays to participate in the scene, what is the complete interaction process of the entire scene, and what interactive behaviors are performed by the objects in the process of participating in the scene; I believe everyone understands this is very important, because it involves How objects interact with each other involves how to analyze which object should have which interaction responsibility; thus ultimately deciding which class should have which methods at the code level. I will introduce some examples of thinking in this regard below, but let’s talk about the theory first. I think the point is to understand every interaction in the entire business scenario through the analysis method of the four-color prototype. To sum up the four-color archetype in one sentence is: what kind of person or organization or object participates in a certain activity at a certain time or a certain period of time in a certain role. Another trick is that we can often ask ourselves, this interaction is "who tells who to do what?", who is the driver of the behavior? Who is the performer of the action? The driver of the general behavior is the notifying party, the executor of the behavior is the notified party, and the notified party has the execution behavior of "what the notifying party requires to do"; in addition, I think it should be noted that the object in real life is not It is said that it has the behavior defined by the role after it has played a role, but it already has it, but it shows the behavior after playing the role; so for objects in real life, the implementation of the role defined Behavior and role-playing happen at the same time, and there is no such thing as who comes first; but objects in software are different, because objects in software are only a certain aspect of objects in real life that we care about, so their capabilities are also different. limited. In addition, from the perspective of design and implementation, from the perspective of single responsibility, we will not design the objects in the software to be very complicated and include many responsibilities, because this will make the objects difficult to maintain. Although this does not violate the analysis principle, it violates the design. in principle. Objects in software are often designed so that when it plays a role, it dynamically injects the interaction behavior defined by the role into the object, thus giving the object the ability to participate in scene interaction. Therefore, to put it simply, the object in the software usually only has basic state characteristics and basic non-interactive behavior, but when it plays a role, it has dynamic interactive behavior;
  5. Analyze which basic state characteristics of each scene participant object will change after the interaction process; this is well understood. When an object participates in an interactive activity, some basic state characteristics will change, such as a person participating After a 100-meter race, the heart rate will increase; the heart rate is the basic physical sign of a person; this should be well understood, so I won’t give many examples.
  6. Analyze how to record and track this interaction, and analyze what additional information this interaction will generate; it is estimated that people rarely think about this, and I think this is the most distinctive part of my object-oriented analysis ideas. Bar! Everyone must know that an interaction of an object will generate some interaction information related to the interaction. For example, a job application activity will generate some information related to the activity, such as admission, written test results, interview results, etc.; for example, an exam will generate the information of the test score; a borrowing book will generate a borrowing information (including: borrower, Books borrowed, borrowing time, we may also design a return time); and, in many cases, these interaction information will be updated in other subsequent interaction scenarios. For example, the status of an application may be "newly submitted" at the beginning, which means that the applicant has just submitted a resume and selected a certain position, and then she goes to take a written test or an interview, then the status of this application will become "written test" or "Interviewed"; for example, when a student takes an exam, he has no grades at the beginning, but after the teacher approves the papers, he will have the exam results. For another example, after a book is borrowed, if the book has not been returned, the time for returning the book is empty, and once the book is returned, there is a time for returning the book; therefore, we can find from these laws that the interaction is actually A process, and once the process starts, it will generate some relevant information, such as the status of the application, the score of the test, the return time of the loan information, and so on. Usually we will consider all the information involved in the interaction process itself and all the additional information generated by the interaction process as a whole. Therefore, I think it is necessary for us to design an object to represent the result of a certain interaction. This result contains all the information involved in the interaction process itself and all the additional information generated by the interaction process; think about it, see "Apply" This word, which you sometimes think of as a verb, is a noun after an advantage. When we think of it as a verb, we focus on the interaction itself, the activity itself, and the emphasis on behavior; when we think of it as a noun, we pay attention to all the information generated by the application behavior; so, seeing this, I think everyone should have in mind The number is counted, that is, after the interactive behavior is over, we often need to design an object to represent the relevant information of an interactive activity; on the one hand, this information reflects the participants of the interactive activity, (interaction time, interaction location, if we care about these On the other hand, it reflects the additional information generated by interactive activities, such as grades, application status, borrowing and returning time, and so on. Finally, it is important to stress that the

Well, the above is the object-oriented analysis idea that I have mastered. The following takes the book lending system ( click here to download the source code ) as an example, and analyzes it according to the above analysis method.

Case study: description of use case scenarios for library management system requirements

Book Information Storage Scenario

Librarians scan books with different names. Everyone knows that each specific book has an ISBN, that is, International Standard Book Number. Libraries manage books according to ISBN, and there will be many different copies of a book with the same ISBN, and each copy is a real book. Therefore, strictly speaking, what is stored in the library database is "book inventory information". In the scenario of book storage, as long as the ISBN is the same, that is, the book with the same title will only be scanned once, and then the administrator will input the inventory information such as how many books there are in total and where they should be placed. Of course, this is just my own understanding of the business scenario. Regardless of right or wrong, let's assume this understanding is correct.

book borrowing scene 

  1. The borrower goes to the library with a library card, first finds a few books to borrow, and then goes to the borrowing office to borrow books;
  2. The librarian scans the borrower's library card and the barcode of the book, and the barcode is the ISBN;
  3. The borrower leaves the library with the book;

book return scene

  1. The borrower eats the book card to go to the library, and returns the book at the book return office;
  2. The librarian scans the borrower's library card and the barcode of the book, and if there is a fine, it will be calculated at this time;
  3. Returners leave the library;

Combine object-oriented analysis ideas and demand use case scenarios for analysis

Analysis of Book Storage Scenarios

scene participants

Libraries, books; the library only needs one instance in the whole book lending system, and we don't care whether it is this library or that library, so I think the library can be designed as a service.

Participants' basic state characteristics

The basic state characteristics of the book: such as the title, author, publisher, etc.;

The basic state characteristics of the library: no, in general, the service is stateless, and the service only provides service behavior;

Scene interaction process analysis

It is easy to understand that the library itself has the behavior of book storage. When a book is put into storage, the library service will generate and save a book's "inventory information", which includes the number of books with a certain name and the information on the shelf position. The Count in the inventory information indicates how many books of a certain name exist. When the scene of borrowing or returning a book occurs, this Count will change; the code example is as follows:

The implementation of the book storage scene class:

copy code
    public class StoreBookContext
    {
        private ILibraryService library = null;
        private Book book = null;

        public StoreBookContext(ILibraryService library, Book book)
        {
            this.library = library;
            this.book = book;
        }
        public void Interaction(int count, string location)
        {
            library.StoreBook(book, count, location);
        }
    }
copy code

How to store library books: 

copy code
public void StoreBook(Book book, int count, string location)
{
    //The inventory information of the book is generated when the book is put into storage
    var bookStoreInfo = new BookStoreInfo(book, count);
    bookStoreInfo.Location = location;
    bookStoreInfoRepository.Add(bookStoreInfo);
}
copy code

Finally, after the interaction, do the basic state characteristics of the scene participants change? Nothing has changed.

Analysis of borrowing scenarios

scene participants

Library registered account, library, book; library is a service, book is just an ordinary book, it has no behavior, it is borrowed. What needs to be analyzed is the participant of the library registration account. Let me talk about my understanding of the four concepts: software user (ie, software user), registered account, book card, and book borrower. Understanding these concepts is very important! First of all, the software user is the user of the software, which is what we usually call the user. There is no problem with this. What is the relationship between the library card and the registered account? We all know that in real life, people use library cards to borrow books; in software, people log in through their registered accounts, and then do things with the "people" represented by the registered account; therefore, library cards are used by users to borrow books The same registered account is also a tool for software users to borrow books through the software; the user has a book card, and the user has a registered account. So how do borrowers understand it? What we usually call a borrower is actually a role, that is, when a user participates in the process of borrowing books through a library card, we call the person who is performing the behavior or who has performed the behavior as the borrower. In a more general expression, we usually give a title to a participant who is doing something or has done something, such as murderer, borrower, book returner, applicant, etc.; so, there is After these understandings, we know that the borrower is only a role, and a registered account can act as a borrower after playing the role of the borrower;

Participants' basic state characteristics

The basic state characteristics of the book: such as the title, author, publisher, etc.;

The basic state characteristics of the library: no, in general, the service is stateless, and the service only provides service behavior;

Basic status of registered account: The library registered account has basic status characteristics, such as: card number, owner name, whether it is locked, etc.;

Scene interaction process analysis

The user of a certain software, that is, the software user logs in to the software system through a registered account, selects several books that he wants to borrow, and clicks the "borrow book" button, and then the button triggers a book borrowing scene, which creates a It includes some scene participants, in the example of borrowing books, it is the account number and the book, and then the book borrowing scene knows that the account should play the role of the book borrower. Therefore, after the account plays the role of a borrower, the interactive behavior of borrowing books is performed on each book. The registered account that plays the role of the borrower automatically has the behavior of borrowing books. In the internal implementation of this behavior, the library is notified to borrow the book. After receiving the notification, the library first obtains the inventory information of the book according to the current book. If the current inventory information is 0, it means that the book is out of stock, and an exception is thrown to notify the software user that the book has been borrowed. If there is still inventory, first update the inventory information, such as the number of books minus 1, and then generate a borrowing information object based on the current borrower, the book, and the current time, and the object will also contain an additional information about the return time of the book. Finally, the borrowing information object is saved. The code example is as follows:

Implementation of the borrowing scene class:

copy code
    public class BorrowBooksContext
    {
        private LibraryAccount account = null;
        private IEnumerable<Book> books = null;

        public BorrowBooksContext(LibraryAccount account, IEnumerable<Book> books)
        {
            this.account = account;
            this.books = books;
        }

        public void Interaction()
        {
            var borrower = account.ActAs<IBorrower>();
            foreach (var book in books)
            {
                borrower.BorrowBook(book);
            }
        }
    }
copy code

How to borrow books for borrowers:

public void BorrowBook(Book book)
{
    //Notify the library to lend me the book
    library.LendBook(book, this);

How to borrow books from the library: 

copy code
public void LendBook(Book book, IBorrower borrower)
{
    //Update the inventory information of the book in the library, such as: quantity information, location information of the bookshelf
    var bookStoreInfo = bookStoreInfoRepository.GetBookStoreInfo(book.Id);
    if (bookStoreInfo.Count == 0)
    {
        throw new Exception(string.Format("The count of book '{0}' in library is zero, so you cannot borrow it.", book.BookName));
    }
    bookStoreInfo.DecreaseCount(); //Number Subtract 1
    bookStoreInfo.Location = null; //Clear the location

    //Generate borrowing information and save it to Repository
    borrowInfoRepository.Add(new BorrowInfo(book, borrower, DateTime.Now));
}
copy code

Finally, after the interaction, do the basic state characteristics of the scene participants change? Nothing has changed.

Book return scene analysis

With the analysis of the borrowing scene, the book returning scene is also easy to analyze. The main process is: the registered account plays the role of the borrower to perform the book return behavior, and during the execution process, the library is notified to receive a certain book to be returned. After receiving the notification, the library will first call up the borrowing information corresponding to the book, then update its return time, and finally update the inventory information of the book. The other basics are similar to borrowing books. I believe everyone should understand the code when they see the code. Let’s go to the code directly!

Book return scene:

copy code
    public class ReturnBooksContext
    {
        private LibraryAccount account = null;
        private IEnumerable<Book> books = null;

        public ReturnBooksContext(LibraryAccount account, IEnumerable<Book> books)
        {
            this.account = account;
            this.books = books;
        }

        public void Interaction()
        {
            var returnner = account.ActAs<IBorrower>();
            foreach (var book in books)
            {
                returnner.ReturnBook(book);
            }
        }
    }
copy code

How to return books to borrowers:

public void ReturnBook(Book book)
{
    //Notify the library to receive the book I want to return
    library.ReceiveReturnedBook(book, this);
}

How the library accepts returned books:

copy code
public void ReceiveReturnedBook(Book book, IBorrower borrower)
{
    //Set the book return time of borrowing information
    var borrowedInfo = borrowInfoRepository.FindNotReturnedBorrowInfo(borrower.Id, book.Id);
    borrowedInfo.ReturnTime = DateTime.Now;

    //Here, true The system will also calculate whether the return time is overdue, calculate the logic of fines, etc., because this is a demonstration example, so I will not do this processing.//Only

    update the number of books here, because the book is not returned immediately. The book is put back on the bookshelf, so the bookshelf position information of the book is still empty at this time.//The
    position information will not be updated until we place the book somewhere on the bookshelf
    var bookStoreInfo = bookStoreInfoRepository.GetBookStoreInfo(book. Id);
    bookStoreInfo.IncreaseCount(); //Add 1 to the count
}
copy code

Okay, that's about it. It took me a lot of thought and time from sorting and thinking to writing the article. I hope everyone can understand it! Another digression, when you look at my source code, you should not pay too much attention to the implementation of my framework, but should pay attention to this object-oriented analysis idea. Code implementation generated from analysis of ideas. I think you understand, hehe!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027771&siteId=291194637