The most important thing is my core Object-Oriented Analysis ideas:
1() Identify the most critical business scenarios. Generally, you can use verbs to search for them. For example, in the recruitment system, a candidate is a one-time candidate to deliver a position, and a candidate is a business scenario; if a student takes an exam in a certain course, the exam is a business scenario. If a student goes to the library to borrow a book, it is a business scenario;
2Analyze the scenario participants, participants in the form of objects, and participants in the form of services for each business scenario; why do we need to differentiate between objects and services? Sometimes we don't care about which participant is, but what the participant is. Generally, in the system, we only care about what service it is, and there is usually only one instance of the Service in the system. The objects are different and we care about who the objects are;
3 ) analyzes the basic state features of the participant objects in each scenario; the so-called basic State feature refers to the inherent state feature of an object that is created from the very beginning. The most vivid example is the height and weight of a person, when a person is born, he or she has two states: height and weight. For another blog post, he or she has the state feature of content since he or she was written, however, we can modify the content of a blog. However, some status features cannot be modified. For example, the creation time of a blog post cannot be modified once it is created; it should be noted that we should not regard the association information associated with the object as the basic State feature of the object. For example, if a person has a certificate for cet6, the relationship between the person and the certificate is related. The certificate is not an inherent basic State feature of the person, people participate in this test. I will discuss how to think and understand this Association later!
4 ) analyze the roles played by the participants in each scenario, what is the complete interaction process of the entire scenario and what interaction behaviors the object performs when it participates in the scenario; I believe everyone understands this is very important because it involves how objects interact and how to analyze which objects have the same interaction responsibilities; this ultimately determines the question of which method the class has at the Code level. I will introduce the example of thinking in this regard below. Let's talk about the theory first. I think the key point is to understand each interaction behavior in the entire business scenario through a four-color prototype analysis method. The four-color prototype in one sentence is: What kind of person, organization, or item participates in an activity with a role at a certain time or within a certain period of time. Another trick is that we can often ask ourselves, "who notifies who to do what? "Who are the drivers of behavior? Who is the performer of the behavior? Generally, the behavior is driven by the notification party, the behavior executor is the notified party, and the notified party has the "Action requested by the notification party" action. In addition, I think it should be noted that objects in real life do not mean that they assume a role before they have the behavior defined by the role, but they do, this behavior is only shown after the role is assumed. For objects in real life, the behavior defined by the role is executed and the behavior assumed by the role occurs simultaneously; but the objects in software are different, because the objects in software are only some aspects of objects in real life that we care about, so their capabilities are limited. In addition, from the perspective of a single responsibility for design implementation, we will not design the objects in the software very complex, including many responsibilities, because this will make the objects difficult to maintain, this does not violate the analytical principles, but the design principles. Objects in software are often designed to dynamically inject interaction behaviors defined by roles to objects when they assume a role, so as to give objects the ability to participate in scenario interaction. Therefore, simply put, an object in the software usually only has basic state characteristics and basic non-interactive behaviors. When it assumes a role, it dynamically has interactive behavior;
5) Analyze the changes to the basic state features of the participants in each scenario after the interaction process ends. This is a good understanding. When an object participates in an interaction activity, some basic state features will change, for example, one person takes part in one100After the rice race, the heartbeat speed will speed up. The heartbeat speed is the basic sign of a person. It should be easy to understand.
6) analyzes how to record and track this interaction and analyzes the additional information generated by this interaction; it is estimated that we seldom think about this point, and this is also the most distinctive part of my Object-Oriented Analysis ideas! You must know that an object interaction activity will generate some interaction information related to the interaction activity. For example, a candidate activity will generate information related to the activity, such as admission, written test scores, and interview scores. For example, an exam will generate the exam score information; A single borrow will produce a borrow information (including: the borrower, the borrowed book, And the borrow time. We may also design a return time). In many cases, the interaction information will be updated in other subsequent interaction scenarios. For example, the initial status of a job application may be "new delivery", indicating that the candidate has just submitted his resume and selected a position. Later, she went to the test or interview, the application status changes to "tested" or "interviewed". For example, if a student takes an exam, he or she has no score at the beginning, however, after the instructor approved the paper, he got the test score. For another example, if the book has not been returned, the time for returning the book is blank. Once the book has been returned, the time for returning the book is available. Therefore, from these rules, we can find that interaction is actually a process, and once this process starts, it will generate some relevant information, such as the application status, test scores, the return time of borrowing information, and so on. We usually consider all the information involved in the interaction process and all the additional information generated in the interaction process as a whole. Therefore, I think it is necessary to design an object to indicate the result of an interaction. The result contains all the information involved in the interaction process and all the additional information generated during the interaction process; think about the word "Apply". Sometimes you think of it as a verb and think of it as a noun after its advantage. When we think it is a verb, we focus on interaction, activity, and behavior. When we think it is a noun, we focus on all information generated by the application. Therefore, here, I think there should be a number in your mind, that is, after the interaction ends, we often need to design an object to indicate 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 this), and on the other hand, it reflects the additional information generated by the interactive activity, such as the score, the application status, the time for Returning books, and so on. Finally, it should be emphasized that this information is designed as an object because it is neither historical nor unchangeable. On the contrary, this information will be updated in other subsequent interactions; so here, I think we don't need to worry about how to store the information associated with an object after it participates in an interactive activity.
Well, the above is my understanding of object-oriented analysis. The following uses the library borrowing system (Click here to download the source code) as an example to analyze it based on the above analysis method.
First, let's briefly describe the main business scenarios of the book lending system:
Library Information Warehouse receiving scenario:
The library administrator scans books with different names. Everyone knows that each specific book has an ISBN, that is, the International Standard Book Number, and the International Standard Book Number. The Library manages books based on ISBN. There are many different copies of the same ISBN book, and each copy is a real book. Therefore, strictly speaking, the library database stores "inventory information of books ". In the case of library receiving, as long as the ISBN is the same, that is, the book with the same name will only be scanned once, and then the Administrator will enter the total number of such books, where should I store the inventory information. Of course, this is just a business scenario that I understand myself. Let's take this understanding right or wrong first.
Book Borrowing scenario:
- The borrower took the library card and found several books to borrow. Then he ran to the library to borrow books;
- The librarian scans the debit card of the borrower and the barcode of the book. The barcode is ISBN;
- The borrower leaves the library with the book;
Book return scenario:
- The borrower eats a library card and goes to the library to return the book;
- The librarian scans the borrower's debit card and the book's barcode. If a penalty is required, it will be calculated at this time;
- The author leaves the library;
Next, we will analyze each scenario based on the above analysis ideas.
Analysis of book warehouse receiving scenarios:
1) scenario participants:
Libraries and books. The Library only needs one instance in the whole library borrowing system, and we don't care whether it is the library or the library. Therefore, I think the Library can be designed as a service.
2) Basic status features of participants:
The basic state features of books: such as the title, author, and publisher;
Basic status features of the Library: No. In general, services are stateless and services only provide service behavior;
3) scenario interaction process analysis:
It is easy to understand that the library itself has the behavior of warehouse receiving. When a book is stored in the database, the library service generates and stores the inventory information of a book, which contains the number of books with a name and the location of the bookshelves. The count in the inventory information indicates the number of books with a name. This count changes when you borrow or return a book. The code example is as follows:
Implementation of library warehouse receiving scenarios:
Public Class Storebookcontext
{
Private Ilibraryservice Library = Null ;
Private Book = Null ;
Public Storebookcontext (ilibraryservice library, book)
{
This . Library = Library;
This . Book = Book;
}
Public Void InterAction ( Int Count, String Location)
{
Library. storebook (book, Count, location );
}
}
Library library warehouse receiving method:
Public Void Storebook (book, Int Count, String Location)
{
// Inventory information of books generated when books are stored into the database
VaR bookstoreinfo = New Bookstoreinfo (book, count );
Bookstoreinfo. Location = Location;
Bookstoreinforepository. Add (bookstoreinfo );
}
4) after interaction, does the basic State feature of the scenario participant change? None of them have changed.
Analysis of borrowing scenarios:
1) scenario participants:
A library registers accounts, libraries, and books. A library is a service and a book. It is also a common book. It has no behavior and is borrowed. It is important to analyze the participant in the library registration account. Let's talk about the four concepts: software users (software users), registered accounts, library cards, and borrowers. Understanding these concepts is very important! First, the software user is the software user, which is what we usually call the user. This is no problem. What is the relationship between the library card and the registered account? We all know that in real life, people use a library card to borrow books. In software, people log on through their registered accounts and then do things with the "persons" represented by the registered account. Therefore, A library card is a tool used by users to borrow books. Similarly, registering an account is a tool used by software users to borrow books. A user has a library card and a user has a registered account. So how do readers understand it? What we usually call a book borrower is actually a role, that is, when a user participates in the book borrowing process through a debit card, we call the person who is performing this action or has performed this behavior as a borrower. A more general expression is that we usually give a title to the participants who are doing something or have already done something, such as the murderer, borrower, author, and candidate, and so on. With these understandings, we know that the borrower is only a role, and a registered account plays the role of the borrower and can drive the borrower;
2) Basic status features of participants:
The basic state features of books: such as the title, author, and publisher;
Basic status features of the Library: No. In general, services are stateless and services only provide service behavior;
The basic status of the registered account: the registered account of the library has basic status features, such as the card number, owner name, and whether the account is locked;
3) scenario interaction process analysis:
A software user, that is, a software user, logs on to the software system through a registered account, selects a few books he wants to borrow, and then clicks the "Borrow book" button, then, this button triggers a Book Borrowing scenario. When this scenario is created, it contains some scenario participants. In this example, the account and book are used, then the Book Borrowing scenario knows that the account should assume the role of the borrower. Therefore, when an account assumes the role of a borrower, it interacts with each book. The registered account that assumes the role of the borrower automatically has the behavior of borrowing books. In the internal implementation of this behavior, the Library is notified to lend the books. After receiving the notice, the Library first obtains the inventory information of the book based on the current book. If the current inventory information is 0, it indicates that the book is not in stock, an exception is thrown to notify the software user that the book has been borrowed. If there is inventory, the inventory information will be updated first, which is 1 less than the number of books. Then, an object for borrowing information will be generated based on the current borrower, book, and current time, this object also contains additional information about the book return time. Finally, save the object. The sample code is as follows:
Implementation of the borrowing scenario:
Public Class Borrowbookscontext
{
Private Libraryaccount account = Null ;
Private Ienumerable < Book > Books = Null ;
Public borrowbookscontext (libraryaccount account, ienumerable book > books)
{< br> This . account = account;
This . books = books;
}
Public Void InterAction ()
{
VaR borrower = Account. actas < Iborrower > ();
Foreach (VAR book In Books)
{
Borrower. borrowbook (book );
}
}
}
Method of borrowing from a reader:
Public Void Borrowbook (Book)
{
// Notify the library to lend me the book
Library. lendbook (book, This );
}
Library lending methods:
Public Void Lendbook (book, iborrower borrower)
{
// Update the inventory information of books in the library, such as the quantity information and the location of the bookshelves.
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 (); // Quantity minus 1
Bookstoreinfo. Location = Null ; // Clear location
//Generate borrow information and save it to Repository
Borrowinforepository. Add (NewBorrowinfo (book, borrower, datetime. Now ));
}
4) after interaction, does the basic State feature of the scenario participant change? None of them have changed.
Analysis of book return scenarios:
With the analysis of the borrow scenario, the book return scenario is also easy to analyze. The main process is: the registered account plays the role of the borrower to execute the book return behavior, during the execution process, the Library is notified to receive a book to be returned. After receiving the notification, the Library first calls up the information of the borrow book corresponding to the book, and then updates its return time, finally, update the inventory information of the book. In addition, it is basically similar to borrowing books. I believe everyone should understand the code at a Glance. Go to the Code directly!
Book return scenarios:
Public Class Returnbookscontext
{
Private Libraryaccount account = Null ;
Private Ienumerable < Book > Books = Null ;
Public returnbookscontext (libraryaccount account, ienumerable book > books)
{< br> This . account = account;
This . books = books;
}
Public Void InterAction ()
{
VaR returnner = Account. actas < Iborrower > ();
Foreach (VAR book In Books)
{
Returnner. returnbook (book );
}
}
}
The method for Returning books by the borrower is as follows:
Public Void Returnbook (Book)
{
// Notify the library to receive the books I want to return
Library. receivereturnedbook (book, This );
}
How the Library receives the returned book:
Public Void Receivereturnedbook (book, iborrower borrower)
{
// Set the time for returning the borrow information
VaR borrowedinfo = Borrowinforepository. findnotreturnedborrowinfo (borrower. ID, Book. ID );
Borrowedinfo. returntime = Datetime. now;
//Here, the real system will also calculate whether the return time is out of date and the logic such as the penalty calculation, because I am a demo, so I will not do this.
//Only the number of books is updated here, because the books are not immediately put back on the shelf when you return the books, so the bookshelves are left empty at this time.
//This book will be updated only when we place it somewhere on the shelf.
VaR bookstoreinfo=Bookstoreinforepository. getbookstoreinfo (book. ID );
Bookstoreinfo. increasecount ();//Quantity Plus 1
}
well, that's almost the case. It took me a lot of time to finish writing articles , I hope everyone can understand it! In other words, when you look at my Source Code , do not overemphasize the implementation of my framework. Instead, you should pay attention to this object-oriented analysis approach, the framework is designed to support code implementation generated by this analysis idea more conveniently. I think you understand!