- Patterns for Object Data
To make an object data or a class data we need some patterns to make the class tidy up and running as desired, without make some variables or methods that not needed and long. We also need a pattern to make easier for programmers who use the class. There are 6 patterns:
- Keeping a Total
Keep a track of a quantity that can go up or down, increasing or decreasing. Usually it related to mathematics calculation. For example, a car fuel will be increased when add some fuels and decreasing when drives.
- The first thing we should do is make a variable that represent the current total.
public class Car {
private double totalFuel;
…
}
- Then make a method that affecting the total
Increased:
The total fuel from the first refuel.
public void addFuel (double fuel)
{
totalFuel = totalFuel + fuel;
…
}
Decreased:
The calculation for the fuel is equal to distance divided by 20 because we decide the decrease in fuel is 5 L per 100 Km or 1 L per 20 Km.
public void decrFuel (double dist)
{
double fuel;
fuel = dist/20;
totalFuel = totalFuel – fuel;
}
Reset:
public void clear()
{
totalFuel = 0;
…
}
- The last is, make a method that store the current total.
public double getTotalFuel() {
return totalFuel;
}
- Counting Events
Count how many times certain events occur in the life of an object. For example, how many times this car get refuel in its life time?
- First is, make a variable to keep the counter.
public class Car {
…
private int fuelCount;
…
}
- Implement the counter to a method that make this counter running.
public void addFuel (double fuel)
{
…
fuelCount++;
…
}
- Collecting Values
Some objects may need to store some numbers, strings, or other objects. If the value is many and need to be stored in sequence, we can use ArrayList to do so. For example, we need to store the price for every refuel.
- Make the variable to store the ArrayList. If we use ArrayList don’t forget to add import before the class row.
import java.util.ArrayList;
public class Car {
…
private ArrayList<Integer> spendLists;
…
}
- Use a constructor to automatically make a new ArrayList.
public Car()
{
spendLists = new ArrayList<>();
…
}
- Make a method add to add values to the list
public void addSpend(int spend)
{
spendLists.add(spend);
}
- Managing Properties of an Object
Provide an instance variable to store the property’s value and methods to get it and set it, or we can call these methods as getter and setter. For example we need to store the car’s name and ID.
- First thing is, make variable to store car’s name and ID.
public class Car(){
…
private String carName;
private String carId;
…
}
- Car’s name isn’t fixed, the owner can change it whenever he wants it. So we should make a set method. After the set method, make a get method to store the current car’s name.
public void setCarName(String carName) {
this.carName = carName;
}
public String getCarName() {
return carName;
}
- For the car’s ID, first we make the inputted ID from the constructor. Then, different with name, ID can’t be changed by the owner, so it is a fixed value. We don’t need any setter to a fixed value, so just make a get method.
public Car(String id)
{
…
carId = id;
}
public String getCarId() {
return carId;
}
- Modelling Objects with Distinct States
Some objects have behavior that varies depending on the past. For example, the owner need to buy a car if the current car has refuel 10 times.
- First, make a variable that represent the new car. Then make a final variable for the need of the new car, contain the number of refuel.
public class Car {
…
private int newCar;
public static final int need = 10;
public static final int no_Need = 0;
…
}
- Make a method to add the number of refuel after every refuel.
public void addFuel(double fuel)
{
…
newCar++;
}
- Make a new variable and method to store the string for every number of refuel.
private String iNeedCar;
public void buyCar()
{
If (newCar == need)
{
iNeedCar = “URGENT!! NEED NEW CAR”;
}
else if (newCar == no_Need)
{
iNeedCar = “No need new car”;
}
else if (newCar > no_Need && newCar < need)
{
iNeedCar = “Somewhat I Need A New Car!”;
}
else
{
iNeedCar = “Is There Any Ready Stock Car?”;
}
}
- Describe The Position of an Object
Save the current object’s position. For example, we need to save the current car position with the owner’s house.
- Like before, first make a variable to store the position of the car
public class Car {
…
private double position;
}
- Make a method that contain calculation for the car’s position or distance
public void distFromHome (double dist)
{
position = position + dist;
}