Development, starting from requirement · Second: Aircraft Manufacturing Plant

Source: Internet
Author: User

CD town building ~~!


Now, let's switch to the back-end developer's perspective. What we need to do is implement this class and let it return real business data.

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;}}

Because this method needs to be rewritten, it is a pity that the simulation data that had been manually input had to be deleted. Therefore, I decided to redefine a class,

Package cn.com. sitefromscrath. service; import Java. util. arraylist; import Java. util. list; import cn.com. sitefromscrath. entity. result; public class searchserviceinrealbiz extends searchservice {public list search (string keywords) {list Results = new arraylist (); // do something. here is the real business logic. In order not to make the problem complex at once, we will leave the central idea of this chapter on hold. Return results ;}}

Note:

SearchServiceInRealBiz extends SearchService

To cater to the bad taste of Java, otherwise, the code in our searchservlet:

SearchService searchService = (SearchService)BeanFactory.getBean("searchService");

A cast exception is thrown.

To put it in one sentence, the main reason for "bad taste" is personal preferences"Duck typing", Such as Python.


Now I can explain my title, because we have built a factory in chapter 1, so we only need to adjust a little code, you can switch the entire system from the simulated status to the actual business status.

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

We commented out

return new SearchService();

Return the newly defined class:

return new SearchServiceInRealBiz();

Now, I'm sure that the front-end page will display real data-this belief does not need to be established in coordination with front-end developers, or even do not need to run tomcat to operate the web again.

"Code that has not been tested will certainly have problems."Principle,

public class BeanFactory {        public static Object getBean(String id) {if("searchService".equals(id)) {//return new SearchService();return new SearchServiceInRealBiz();}throw new RuntimeException("cannot find the bean with id :" + id);}public static void main(String ... arg) {String keywords = "test";SearchService searchService = (SearchService)BeanFactory.getBean("searchService");List results = searchService.search(keywords);for(int i = 0; i < results.size(); i++) {Result result = (Result) results.get(i);System.out.print("[" + result.title + "]");System.out.println(result.content);}}}

Run the command to obtain the output result: 

[result 1]something..................[result 2]something..................[result 3]something..................[result 4]something..................
Bingo! Test passed. -- In order not to complicate the problem, JUnit is not used here. :)

Out of the "pattern" of Java programmers, I decided to design an interface, which looks a little more java style.

package cn.com.sitefromscrath.service;import java.util.List;public interface SearchService {public List search(String keywords);}
Note: In order not to modify JSP, we name the interface
SearchService 

Then there is a simulated data Implementation class and a business data Implementation class.

Authentic:

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchServiceInRealBiz implements SearchService {public List search(String keywords) {List results = new ArrayList();//do sth.return results;}}

Simulation:

package cn.com.sitefromscrath.service;import java.util.ArrayList;import java.util.List;import cn.com.sitefromscrath.entity.Result;public class SearchServiceMock implements 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;}}

Now, let's look at the factory class. Nothing is better than the factory to satisfy the programmer's control desires :)


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

Here we have a switch:

public static boolean MOCK = false; 

In this way, we can achieve a convenient switch by controlling this switch.


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

This chapter ends, and I also have a bad taste. I will repeat the end of the first chapter:


Well, what have we achieved here? Is this necessary? Is this necessary?


In addition to a switch that ensures constant (analog) Front-end data output, it ensures that your development work does not interfere with the development and testing of front-end developers, it seems that there is no need.

But... please note that Tomcat is not started during implementation and test of the Code, but it also guarantees "code/module reliability"-I believe, one of the most common tasks for many web developers during development is to restart Tomcat and manually execute Form operations to view the page output results with the naked eye-too slow and too slow, dear user!

(In addition, the above mentioned JUnit, our simulation service:

SearchServiceMock 

You can also use the easymock tool to try it out .)


Of course, if you still don't think it is necessary, it is because our business logic is not complex enough.


In the next chapter, we will try to simulate a business process to make simple things more complex.


To be continued ....

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.