简体   繁体   中英

Java design / object oriented design issue

I have a design dilemma I want to share with you guys: I have a class with various member variables that represents a person. One of the members is the home location of the person in coordinates. This class is persisted in a MySQL database.

The issue I have is that I want to have a list of people that are within a certain radius from the recorded distance from the specified person.

I successfuly created an SQL query that returns a result set with the person's details, and its distance from the pin point. But now is the problem: what's the best way to save this data in Java? Saving the distance inside the person class's members is not good, because the distance is not relevant to the person object. I thought about creating a two dimenesional array which holds the person in the first column and the data in the other. Another option I thought is to create a container object with two values, the person and the distance.

What do you think is the most efficient and "object oriented way" of doing that?

它看起来像Map<Person, Double>

I would provide a connection class that holds the distance and person information.

class PinPoint
{
    private double x; // Assuming
    private double y;

    public List<Connection> getConnections(double radius)
    {
        // Return a list of connections with the person and distance information
    }
}

class Connection
{
    private double distance;
    private Person person;
}

That way you can add more things in the connection class later on, too. If you don't need it a simple map might suffice, too.

I would suggest a Map<Person, Integer> where the Person is the key and the distance is the value.

Another option might be Guava's Table class: Table<Person, Point, Integer> . This would be if you want to hold the distances to multiple points.

您可以将搜索结果存储为包含所有Person和您的精确定位的对象,并且您将能够在获得结果集后确定距离。

你可以有一个PersonGeoInfo类,它可以保存对Person的引用以及你认为合适的其他任何地理信息。

Why not create a PinPoint class which has all the relevant variables and a data structure such as a simple array of the persons nearby populated by your query.

You could then put instances of your PinPoint object into what ever data structure you find most useful to the task at hand.

You should create a separate object called Pin or Marker. Then you need a many-to-many table/object in the middle between the Person and Marker. It could be called PersonToPin and would have attributes person, pin and distance.

You can then have a Set called pins on the Person and a set called persons on the Pin.

Doing all this stuff yourself can be a real pain. I would recommend using something like Hibernate or another persistence library which allows you to concentrate on the Java objects and the library will take care of creating the tables and updating the database.

If you make an object that holds a Person, Address, and distance from the pinpoint (assuming it's fixed), you could implement Comparable on that object's distance. Then you could easily get your list of objects under a certain distance from the pinpoint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM