AndyM
AndyM

Reputation: 622

What OOP concept deals with the following situation?

I'm just starting to learn Java and I'm struggling to find the correct way to implement the following.

I have a Class called State. This Class has a field called stateCaptial.

I create a State object.

Then I want to create many Town objects that are linked to the one State object, If I query the town for its state capital it should get it from the state object. I think it would be classed as one to many object relationship ?

What is the Java terminology for implementing such a solution ?

Many Thanks Andy

Upvotes: 2

Views: 407

Answers (8)

Owen
Owen

Reputation: 22897

You could create a a Town class as well. So, State has a field called capital of type Town. State also has a `Set' of towns so that all the towns can be looked up for a state. Town has a field state of type State. Something like below.

State class:

public class State {

     private Town capital;
     private Set<Town> towns;

     public State() {
       this.towns = new HashSet();
     }

     public State(Town capital) {
       this.capital = captial;
       this.towns = newHashSet();
       this.towns.add(capital)
     }


     public void setCapital(Town capital) {
       this.captial = capital;
     }

     public Town getCapital() {
       return this.capital;
     }

     public void addTown(Town town) {
       this.towns.add(town)
     }

     public Set getTowns() {
       return this.towns;
     }
}

Town class:

public class Town {

    private State state;

    public Town() {}

    public Town(State state) {
      this.state = state;
      this.state.addTown(this);
    }

    public void setState(State state) {
      this.state = state;
      this.state.addTown(this);
    }

    public State getState() {
      return this.state;
    }
}

Then if you have a Town object called myTown, to get the town's state's capital you use:

myTown.getState().getCapital();

If you have a State object called myState, you get all the town's you use:

myState.getTowns();

Upvotes: 1

Woot4Moo
Woot4Moo

Reputation: 24316

Here are my thoughts. The relationship is a one to many (State to Town) so to represent this in code it would look something like this:

public class State {
    private Set<Town> towns;

    public State() {
        // default constructor
    }

    public State(Set<Town> towns) {
        this.towns = towns;
    }

    public void createTown(Town town) {
        towns.add(town);
    }
}

Now the Town class could look something like this:

public class Town {
    private State state;
    private boolean isCapital;

    public Town() {
        // default constructor
    }

    public Town(State state) {
        this.state = state;
    }

    public void setCapital(boolean isCapital) {
        this.isCapital = isCapital;
    }
}

Upvotes: 1

Selim
Selim

Reputation: 1013

Aggregation and delegation..

Upvotes: 0

Matteo Mosca
Matteo Mosca

Reputation: 7448

From an OOP point of view, that's what should be the correct implementation. I'm going to use C# syntax since I'm familiar with it, but it shouldn't be too different from java.

public class State
{
public string Name {get;set;}
public ICollection<City> Cities {get;set;} //Access to all child cities
}

public class City
{
public string Name {get;set;}
public string State_Name {get;set;} //this is sort of a FK
public State State {get;set;} //Direct access to the State object related
}

You could omit the string State_Name from the City class, as you can reach the same property by using city.State.Name; Anyway it could be very convenient to have the "FK" directly on the child class for some purposes. Of course in this case, you should have code that ensures that the FK stays syncronized with the State object. If these classes are used, let's say, by an ORM framework, usually this is handled automatically.

Upvotes: 0

TimC
TimC

Reputation: 1061

You should look into encapsulation, abstraction and probably inheritance.

You should try to think of each object that you create as encapsulating the data that directly related to it, and providing methods of accessing and operating on that data.

For this issue, you could create two classes, State and Town.

Your State class will have a member called Capital which will be of type town and a List called Towns which contains all the towns in it. The town class will have a member called state.

Upvotes: 0

Chris Marisic
Chris Marisic

Reputation: 33098

What you are describing is that you would like your Town objects to be dependent on a State object. The act of inserting the dependent object into the Town class is commonly known as dependency injection or DI.

You would have a class structure similar to this

public class Town
{    
    private State _state;

    public Town(State state)
    {        
        _state = state;        
    }    

    public string StateCapital()
    {        
        return _state.Capital;
    }    

}

Upvotes: 1

Alan Geleynse
Alan Geleynse

Reputation: 25139

This would be a one to many relationship.

The best way to do this would be to have each Town object have a state field that refers to the State that it is part of. The getStateCapital method of Town would then get the stateCapital from its state and return that.

You would probably also want to have an array or List of Town objects on your State object so you could list all the Towns within a State.

Upvotes: 1

Michael Goldshteyn
Michael Goldshteyn

Reputation: 74370

Why not an array of towns in state, each referring back to the state they are in?

Upvotes: 0

Related Questions