Software Requirement Analysis and Thinking Process towards a good Architecture
Think Again Before You Start Coding! Thinking Process! Mahmud Hasan Software Engineer Bangladesh
What we actually do in everyday programming?We basically do just 3 things in all the applications.1. We take input2. We process input3. And we generate output If we narrow it down more, we basically work with a single goal. That is “We want to generate output” from our application.Now, think what the word “Output” actually means?Simple – “Showing Object!”Example?1. In database applications we generate views and reports.2. In games we show different characters of the game and show change in their view based on scenario and action.3. In console apps we save, we trigger but ultimately we do something that would be viewable somewhere.
Now A requirement comes to you and let’s walk through how we can proceedin implementing the requirement.• 1st of all you need to break down the requirement in single piece of functionalities. Note that I have bold out the word “Single”. Yes, you never implement more than one functionality at a time.• After breaking down the functionality in single units the next question you will ask yourself for each and every functionality is, what are the items that you will need to deal with in that specific functionality? The answer of this question is/are your object/s.
Example:• Requirement (From our real life project): Extract keywords from articles and save those in your database. Implement search on article based on the extracted keywords.Breakdown:1. Extract keywords from article.Objects: Keyword, article.2. Save keywords to database.Objects: Keyword.3. Search article on keywords.Objects: keyword, article. (Do you see any other object here? Will come to this point later.)
Context of the functionality• Next question you will ask yourself is, what is the context of the functionality you are going to implement?• Does the object/s of the functionality closely related with the specific Domain of your application? Mark that, I have bold out the word “Domain”• Does the object/s of the functionality can be co-related with a broader domain?• So, what actually you mean by the word “Domain” in software engineering? We all know that, an application is basically composed of several functionalities where we deal with different objects. So, we have 2 things here: One is objects and another is functions around those objects. If you think from narrow sense, your application is a closed system where you implement different functions to deal with different objects. From this context, the application will have only one kind of user: Users who gets into the application and use it.
Think Broad: If you think with a little broader sense, your application might have 2 type of users: 1. Users who gets into the app and use it. You are initially develop the app for them. 2. Users who may like to use data that you are preserving in your application. This scenario may come later. So, if the second scenario comes you will need to expose your objects and functionalities to the external world. Here the concept DDD and SOA comes along with OOP. (Will come to this point later.) Example: Think of FaceBook. it has many information on its members and their friends. But is it a closed system? No. It has exposed it’s objects to the external world and so you can find lot of exciting apps on facebook by many developers. Think of the facebook app on your windows phone.
• Let’s come back to the question what actually the word “Domain” means. Domain is actually the specific area/ topic/ business that your application is going to be developed for. Remember the question you asked yourself earlier? So, The context of the functionality can be your own application domain. Or, it can be a broader domain where applications of other domains may also us this. So, it seems your object indeed has a domain and applications, including your one, of the same domain may use your object for different purposes. And that is how you can call your objects as Domain objects.
Note that, we have not thought of any database or table in analyzing the requirement.Rather, we still are finding and analyzing the objects for the requirement.For our specific requirement we found 2 objects: One is “Keyword” that will be extractedfrom articles. And the other one is “Artcicle”.It is obvious that the keyword extraction feature is a requirement of your application. But Ifyou ask yourself that if the keyword extraction feature can be useful from other applicationsof different domain? Yes it can be. If we think of this feature is just extracting keywords froma large text, it might be useful from many other apps. So, this function is not your applicationdomain specific. Thus the object “Keyword” can be considered as an object, but of a separatedomain than your application. The domain can be “Text” or something more generic!
“Keyword” is basically a value object of the extraction service. Now, the question is what is value object?A value object of any domain or application is the object that do not have its own identity. Inother words that does not get saved in any repository. When keyword is returned from theextraction service, it is a value object for that service.Now, when you have received the keywords in your application the next functionality you willneed to implement is saving the keywords. This time the keyword is a domain object for yourapplication domain.
DDDNow come to the object “article”. This is the object that is specific to your domain where you playaround different articles. So, it is a domain object for your application and the external world thatwill deal with your domain.So, up to this point, we have found out 2 domain objects and 1 value object out of ourrequirement. And still not thinking of database table or storage.In-fact we are approaching in Domain Driven Design (DDD) approach. (More detail of DDD is not inscope of this presentation. See more on this here http://en.wikipedia.org/wiki/Domain-driven_design)Gradually your domain objects will lead you to the database and you can easily have your databasefrom this approach using EF 4.0 code first approach. (This is not focus of this presentation.)More Example of Domain Object: Customer, Supplier, Book, Library, Measure, Portfolio etc.More Example of Value Object: SearchCriteria, ListItem, CustomView etc.
Let’s proceed more with keyword extraction functionality: We have the object “Keyword”. We need to extract keyword from a text. So, what actually we need to? We need to develop a service that will take text as input and will provide keywords as output. This service will be used by our application and may also be used by many other applications. Developing the keyword extractor as a service rather than a function of your application exposes other design concerns too. This is natural that when you develop for your own, you may compromise many things. But as you are developing a service, you have to focus on extensibility, scalability and manageability. When you expose a service, you must ensure any change in your service will not affect the existing users of the service. You see it is deriving you in enforcing good design principles in your development. Thus developing services can bring good practices in your code.
Now, let’s talk a little bit on Article Search on Keyword. We have already identified “Article” as a domain object of our application. Now we need to implement the search feature on it. Before jump into this imagine, someday, your article website becomes very popular. Different 3rd parties want to develop widgets to show contents from your website. They also want to provide search on keyword functionality in that widget. They are contracting with you to know how they can achieve that. Alas! If you do not follow proper design principle it is a nightmare for you. But think if you had developed the keyword search with good design principle and make the functionality as an article search service rather than a feature of your app, you already have what others are looking for.
• SOA So, we have decided, among the 3 functionality that we singlified from our requirement, we will develop 2 of those as service. Here is how Service Oriented Architecture (SOA) comes (More discussion on SOA is out of scope of this presentation). Look http://en.wikipedia.org/wiki/Service-oriented_architecture for more. So, so far for keyword extraction and article search we are approaching following 3 priciples: 1. OOP 2. DDD 3. SOA
• Let’s go more in-depth of the implementation of Keyword extraction service. When you start developing keyword extraction service what are the questions comes to your mind?
• Questions: 1. What is the algorithm/logic of extracting keyword from the text? Possible ans: Split by space, use third party keyword extrator, use other algorithm 2. How will be the format of the output of the service? Possible ans: XML, JSON, other format 3. What are the extensibility points for the service? Possible Ans: Note the possible answer of the above 2 questions. Both has an option called “Other”. So, this “other algorithm” or “other format” may not be defiend now, but it may come in future to get implemented. So, we will need to develop the service in a manner so, that it is extensible in future without affecting its clients.
So we can see in keyword extraction service, we have at-least 2 extension point.1. The process of keyword extraction.2. The process of keyword formatting.Can you derive to the interfaces that you need here?
we can have 2 interfaces here1. IKeywordExtractionService 1. Method: GetExtractedKeywords2. IKeywordFormatter 1. Method: GetFormattedTags Now how will this 2 interface work together? You want extracted tags handling the format that your specific service returns with your desired formation. Any design pattern in your mind?
Here is my answer. There could be other answers too:We may utilize “Strategy” pattern here.The definition of the GetExtractedTags method of IKeywordExtractionService will be as follows: string GetExtractedTags(string queryText,ITagFormatter tagFormatter);You will implement IKeywordExtractionService for different ways of extraction of the keywords.Example: YahooKeywordExtractionService, AlchemyKeywordExtractionService, PantheonKeywordExtractionService etc.In those implementations you will calltagFormatter. GetFormattedTags(tags) so that you get the tags in your desired format. Now, from the client app you just get the correct instance of the service according to your desire and pass the correct formatter you will get your desired output.
You see the extension points are open in the pattern of implementation.Anytime you can implement a different extraction process and also adifferent formatter.
I hope, this session will help all of us to develop the sense of finding the objects from our spec, finding domain, differentiating domain objects with value objects, service oriented development and above everything object oriented approach.• THANKS• QUESTIONS?