#Blocking creating an object, only creating via factory
1 messages ยท Page 1 of 1 (latest)
You can make CarFactory a inner class of Car and have the constructor of Car and its subclasses be protected/private depending on your structure
You don't have to make your constructors public. Making them private or package level.
I wouldn't make the factory be a car though.
Either expose factory methods, or declare the constructor with package access and declare the factory in the same package.
if i let constructor without any acces modifier i cant make an object in main, but i can create it in test class
is it ok?
Without any modifier means it's accessible in the same package. So if possible you might just want to make that private
Most of the common solutions require you to be using Java's package system. If main is in the same package as Car, then making the constructor package access won't help.
You also have the choice of 'factory methods' or 'factory types'
but when i make them private i cant use them in creating an object in carFactory where i need to add it co cache
class Foo {
private Foo() {}
public static Foo create() {
return new Foo();
}
}
No one else can call the constructor, they have to say Foo.create()
Must the factory be its own type or is a method just enough? Because if it is this becomes really simple, just make the constructor private and have a "createX" method in the same class.
beat me to it ๐
it needs to be in factory ๐
Or you use a package and factory types
package x;
public class Foo {
Foo() {}
}
...
public class FooFactory {
public Foo create() {
return new Foo();
}
}
anything not in the x package must use a FooFactory to call fooFactory.create().
Alternatively the factory can be a nested class.
class Foo {
private Foo() {}
public static class Factory {
public Foo create() {
return new Foo();
}
}
}
No one can create a Foo directly, and must use a Foo.Factory with fooFactory.create()
Create a car service. Each car has common properties: power (engine power) and engineCapacity (engine capacity). There are three types of cars: SportsCar with additional fields maxSpeed, acceleration, turbo; Bus with additional fields seatingCapacity, standingCapacity, hasWifi; and NormalCar with additional fields trunkSize, fuelType, numberOfDoors. The service should allow: 1) finding the car with the highest power (power) โ method argument: list of cars; 2) finding the car of a given type with the highest engine capacity (engineCapacity) โ method arguments: list of cars and the car type; 3) exporting a list of cars to JSON โ void, arguments: list of cars and file path; 4) importing a list of cars from JSON โ returns List<Car>, argument: file path. Example JSON: [{"type": "SportsCar", "power": 350, "engineCapacity": 3.5, "maxSpeed": 320, "acceleration": 3.5, "turbo": true}, {"type": "Bus", "power": 200, "engineCapacity": 5.0, "seatingCapacity": 50, "standingCapacity": 20, "hasWifi": false}, {"type": "NormalCar", "power": 120, "engineCapacity": 1.6, "trunkSize": 400, "fuelType": "petrol", "numberOfDoors": 5}]. Cars must be cacheable and cannot be instantiated directly; the only way to create a car is via factory methods in CarFactory, which handles the cache, e.g., SportsCar sc1 = CarFactory.createSportsCar(350, 3.5, 320, 3.5, true); โ if such a car already exists, the same instance is returned (sc1 == sc2 -> true).
It doesn't have any rules about structure so I'd use the nested classes if I were you. It's possible you might get a complaint about being able to bypass the factory if the caller is in the same package if you use the other approach.