Development, starting from requirements · starting from vision

Source: Internet
Author: User

Starting from the demand is not an empty talk, as is the case in the development process.

Starting from the demand, it is essentially a combination of extreme programming and test-driven development ideas.

In view of the popularity of website development, I plan to explain my understanding of "demand-driven development" from a website and extend it to a wider area.


First, let's assume a requirement:

We need to implement a website similar to Google. Users can access it through a Web browser and query it in the homepage input box to return the search results.

Shows the effect:


Step 0: create a web project: sitefromscratch through eclipse. The file structure is as follows:

Add a JSP file under webroot:

<%@ page pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Access/sitefromscratch/search1.jsp through a browser to achieve the same effect as the target.


Step 2: Through the content displayed on the interface, we can roughly estimate the required data and its format, so that we can add a small amount of code to achieve the same effect:


<%@ page pageEncoding="UTF-8"%><%@page import="java.util.List"%><%@page import="java.util.ArrayList"%><%!class Result {String title;String content;public Result(String title, String content) {this.title = title;this.content = content; }} %><%String keywords = "site from scratch";List results = new ArrayList();results.add(new Result("result 1", "something.................."));results.add(new Result("result 2", "something.................."));results.add(new Result("result 3", "something.................."));results.add(new Result("result 4", "something.................."));%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Here, we have constructed a batch of pseudo data (and created a class at the same time) and achieved a completely consistent display through the corresponding execution logic.


Step 3. Now, we need to pass the process of submitting, querying, and Displaying results:

<%@ page pageEncoding="UTF-8"%><%@page import="java.util.List"%><%@page import="java.util.ArrayList"%><%!class Result {public String title;public String content;public Result(String title, String content) {this.title = title;this.content = content; }} public List search(String keywords) {List results = new ArrayList();results.add(new Result("result 1", "something.................."));results.add(new Result("result 2", "something.................."));results.add(new Result("result 3", "something.................."));results.add(new Result("result 4", "something.................."));return results;}%><%String keywords = request.getParameter("keywords");if(keywords == null) keywords = "";List results = search(keywords);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Here, we construct a list search (string keywords) method to separate the business logic from the page display and place them in the separate blocks in the JSP file.

Step 4: To keep the page concise, we extract the defined classes and methods and organize them with packages:


package cn.com.sitefromscrath.entity;public class Result {public String title;public String content;public Result(String title, String content) {this.title = title;this.content = content;}}

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchService {public List search(String keywords) {List results = new ArrayList();results.add(new Result("result 1", "something.................."));results.add(new Result("result 2", "something.................."));results.add(new Result("result 3", "something.................."));results.add(new Result("result 4", "something.................."));return results;}}

Unexpectedly, the code of searchservice. Java and result. Java is copied directly from search. jsp.

It is worth mentioning that we have added a beanfactory class, as an implementation of the factory mode, which simply returns the instances generated by the class corresponding to the specified ID.

As follows:


package cn.com.sitefromscrath;import cn.com.sitefromscrath.service.SearchService;public class BeanFactory {public static Object getBean(String id) {if("searchService".equals(id)) {return new SearchService();}throw new RuntimeException("cannot find the bean with id :" + id);}}

Although it seems to be superfluous, it will play a very important role in my subsequent discussions.


Now, the content of the JSP file looks much more concise:

<%@ page pageEncoding="UTF-8"%><%@page import="java.util.List"%><%@page import="cn.com.sitefromscrath.service.SearchService"%><%@page import="cn.com.sitefromscrath.BeanFactory"%><%@page import="cn.com.sitefromscrath.entity.Result"%><%String keywords = request.getParameter("keywords");if(keywords == null) keywords = "";SearchService searchService = (SearchService)BeanFactory.getBean("searchService");List results = searchService.search(keywords);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Step 5: I heard that MVC is a very high-tech thing. Let's also implement it:


Add a Servlet as the model layer.

package cn.com.sitefromscrath.web;import java.io.IOException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.com.sitefromscrath.BeanFactory;import cn.com.sitefromscrath.service.SearchService;public class SearchServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String keywords = request.getParameter("keywords");if(keywords == null) keywords = "";SearchService searchService = (SearchService)BeanFactory.getBean("searchService");List results = searchService.search(keywords);request.setAttribute("keywords", keywords);request.setAttribute("results", results);request.getRequestDispatcher("/search5.jsp").forward(request, response);}}


Add a JSP file search5.jsp as the view layer.

<%@ page pageEncoding="UTF-8"%><%@page import="java.util.List"%><%@page import="cn.com.sitefromscrath.entity.Result"%><%String keywords = (String)request.getAttribute("keywords");List results = (List)request.getAttribute("results");%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Configuration in Web. xml:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>SearchServlet</servlet-name><servlet-class>cn.com.sitefromscrath.web.SearchServlet</servlet-class></servlet><servlet-mapping><servlet-name>SearchServlet</servlet-name><url-pattern>/search</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>


Now let's run it again, http: // localhost: 8080/sitefromscratch/search

Binggo, the effect is no different from the target!


----------------------------------------------------------

Well, what have we achieved here? Data is still false. Is this necessary? Is this necessary?

Look, ye GE has been shot with blood. I hope you have not reached this level.


At this step, we have actually completed the handshake agreement with the front-end Page Maker:

You can write JavaScript, HTML5, or whatever. You can see the format and content of the returned data. In this way, the format will not change, and the content will not go wrong. How to embed data is yours.


To add this, the front-end page staff (maybe you own the job) do not need to start a large number of complex applications, such as MySQL and memcache, you can debug your own HTML or JS Code and avoid other exceptions (the database is down, the data table is destroyed, the network is disconnected, and memcache cannot be connected, etc) interference with front-end development.


Especially in the debug stage,If you cannot ensure what is correct, you cannot find the error..

When many programmers troubleshoot a problem through debug, the most common mistake is that they are lost in a lot of modules and cannot find a way out. The root cause of this situation is: in the eyes of the client, every module is suspicious and uncertain. You can either guess or query them one by one, and the energy and time are wasted.


For backend developers, there is only one task left: Let the following class methods return real business results.

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchService {public List search(String keywords) {List results = new ArrayList();results.add(new Result("result 1", "something.................."));results.add(new Result("result 2", "something.................."));results.add(new Result("result 3", "something.................."));results.add(new Result("result 4", "something.................."));return results;}}

We will discuss this issue in the next chapter.

To be continued...

Development, starting from requirements · starting from vision

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.