#Blocking creating an object, only creating via factory

1 messages ยท Page 1 of 1 (latest)

zinc hazelBOT
#

<@&987246399047479336> please have a look, thanks.

gaunt briar
#

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

pseudo wyvern
#

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.

echo epoch
#

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?

gaunt briar
#

Without any modifier means it's accessible in the same package. So if possible you might just want to make that private

pseudo wyvern
#

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'

echo epoch
#

but when i make them private i cant use them in creating an object in carFactory where i need to add it co cache

pseudo wyvern
#
class Foo {
    private Foo() {}

    public static Foo create() {
        return new Foo();
    }
}

No one else can call the constructor, they have to say Foo.create()

gaunt briar
#

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.

gaunt briar
echo epoch
#

it needs to be in factory ๐Ÿ™

pseudo wyvern
#

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().

gaunt briar
#

You can use inner classes if package access is an issue for you, like so

pseudo wyvern
#

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()

echo epoch
#

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).

gaunt briar
#

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.

echo epoch
#

thank you guys for your help, at the end i made constructor private and made static method in class wich is making a specific car and then used it in carfactory

#

๐Ÿ˜†