Factory model-practice details-practice Details

Source: Internet
Author: User

Factory model-practice details-practice Details

Factory model:
There are a lot of explanations. Here I will explain what I understand in combination to everyone: the company has such a requirement that LBS positioning should be used in the App to implement certain functions. There are a lot of product Technologies Beginning with requirements and technical validation sessions. When we discuss whether to use Baidu API or AMAP. There are endless debates. Some people say that Baidu is inaccurate in positioning and some say that autonavi is inaccurate in positioning. There are different opinions. What should I do? Finally, B's total shot board is used together. Which one is useful? The lead shot the board, but did not say it again? What should I do? The factory model is coming soon. I have designed both of them for you, and set a switch and parameter for the Code. If you say it is not good to use AMAP, I will change the parameter to Baidu, the Code was generated until the leader was happy.

Public class test {
Public static void main (String [] args ){
Location position = new LocationFactory (). getInstance ("xiaomi ");
Position. getPosition ();
Position. getCityName (10, 20 );
}
}
 
Class LocationFactory {
Public static Location getInstance (String type ){
If ("baidu". equals (type )){
Return new BaiduLocation ();
} Else {
Return new XiaoMiLocation ();
}
}
}
Class BaiduLocation implements Location {
@ Override
Public void getPosition (){
// TODO Auto-generated method stub
System. out. println ("using Baidu to locate the current longitude and latitude is XXXXX ");
}
@ Override
Public void getCityName (long lat, long lng ){
// TODO Auto-generated method stub
System. out. println ("using Baidu to locate the current city is XXXXX ");
}
}
Class XiaoMiLocation implements Location {
@ Override
Public void getPosition (){
// TODO Auto-generated method stub
System. out. println ("using Xiaomi to locate and obtain the current latitude and longitude is XXXXX ");
}
@ Override
Public void getCityName (long lat, long lng ){
// TODO Auto-generated method stub
System. out. println ("using Xiaomi to locate the current city is XXXXX ");
}
}
Interface Location {
Public void getPosition ();
Public void getCityName (long lat, long lng );
}

The above example demonstrates the concept of the factory model. LocationFactory is a factory class. The parameter of the static function getInstance determines Whether Baidu or AMAP is used. In this way, the caller only needs to care about Whether Baidu or AMAP is used. Location is an interface that abstracts common function calls by AMAP and Baidu. For positioning, it is usually used to query the address based on the latitude and longitude, or locate the current location to obtain the latitude and longitude. Of course there may be more useful functions, which I will not list here. With such a common interface, XiaoMiLocation and BaiduLocation can meet the caller's needs by implementing its interfaces. The caller can change the parameters to meet the needs of different positioning APIs. Of course, if Baidu and autonavi are uncomfortable, you can use Google APIs. You only need to construct a GoogleLocation class and implement the Location interface.

The factory mode is widely used. For example, the BitmapFactory class commonly used in bitmap of android is used to create Bitmap objects. The static factory method is usually used.

Related Article

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.