Where to create AbstractFactory

In an abstract factory, you declare a type that is responsible for creating objects.

This will prevent the need to use such a switch:

 if( type == ONE ) {
     doOne();
  } else if( type == TWO ) { 
     doTwo();
  } etc. 

Or the same thing:

 switch( type ) {
     case ONE: doOne(); break;
     case TWO: doTwo(); break;
     etc....
  }

In it:

   MyAbstractFactory factoryInstance = ... ? 

   SomeObject object = factoryInstance.createObject();

   object.doX();

As I understand it, AbstractFactory will create the correct object, which in turn will perform the polymorphically correct behavior.

Then, if you use this object 10-20 or 100 times in your program, you do not need to repeat this switch every time. You simply execute the appropriate method and leave the polymorphism to do this work.

   object.doY();

   object.doZ();

Adding a new type is as simple as creating a new concrete factory.

All this is clear to me. But...

Where and how is a specific factory created (in general terms)?

( main() Configuration.init()), , , if/else | switch, , .

"" ( ), , , .

:)

+3
5

An Factory factory . , . , "Abstract Factory" " ", factory .

, , /, , CarSeats, CarStereo .., TruckSeats, TruckStereo, , : IVehicleItem. 2 TruckFactory CarFactory, factory, VehicleFactory. - .

VehicleFactory carFactory = new CarFactory();
IVehicle car = new Car(carFactory);

VehicleFactory truckFactory = new TruckFactory();
IVehicle truck = new Truck(truckFactory);

, factory, . , , , switch factory . Enum , .

: , , , factory. , "downvote". factory Factory .

Wiki - factory , factory .

, Wiki GoF..etc , . , factory,

public static ImageReader getImageReader(InputStream is) {    

 int imageType = figureOutImageType(is);

   switch(imageType) {
            case ImageReaderFactory.GIF:
                return new GifReader(is);
            case ImageReaderFactory.JPEG:
                return new JpegReader(is);
            // etc.
        }
}

Abstract factory Pattern, .

public static GUIFactory createOsSpecificFactory() {
        int sys = readFromConfigFile("OS_TYPE");
        if (sys == 0) {
            return new WinFactory();
        } else {
            return new OSXFactory();
        }
    }

, "" . , , . , "" "" . .

+1

, factory , (, , factory ) . - "FactoryService", factory .

:

FactoryService service = FactoryService.instance();
MyAbstractFactory factory = service.getFactory();
SomeObject obj = factory.createObject();

, factory , , , .

, (Spring, , ). Spring, Spring, factory factory.

: factory . - -factory, .

+1

, if, if. ifs, , , ifs - , . , (IMHO).

, factory. , , , , factory, , . , factory , , , , .

if ( #, ), -, ( ), , .

- Enum, , .

0

, , AbstractFactory.

-, . : SQL Server, Oracle. . Provider: ProviderBase, ProviderCollection ..

- . factory , . switch, , . , -, , . .

0

Using some IoC frameworks in this case is the most flexible solution - Spring was recommended in earlier answers, Google Guice is another great choice to get started with.

However, if a clean Java solution is required, the enumerations work really great. Pls will consider the following example:

enum FactoryConfig {
  CONFIG_1 {
     IFactory instantiate() {
        return ...
     }
  },
  CONFIG_2 {
     IFactory instantiate() {
        return ...
     }
  },
  CONFIG_3 {
     IFactory instantiate() {
        return ...
     }
  };

  abstract IFactory instantiate();
}
...
// and its usage..

IFactory factory = FactoryConfig.CONFIG_3.instantiate();

More information on the similar use of enumerations can be found, for example, here and.

0
source

Source: https://habr.com/ru/post/1713247/


All Articles