This commit is contained in:
aietcn 2018-05-02 05:50:19 +08:00 committed by Grzegorz Piwowarek
parent 13d226c97b
commit 54d37ea0dc
12 changed files with 254 additions and 0 deletions

View File

@ -18,10 +18,29 @@
<artifactId>auto-value</artifactId>
<version>${auto-value.version}</version>
</dependency>
<dependency>
<groupId>com.google.auto.factory</groupId>
<artifactId>auto-factory</artifactId>
<version>${auto-factory.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
</dependencies>
<properties>
<auto-value.version>1.3</auto-value.version>
<auto-factory.version>1.0-beta5</auto-factory.version>
<guice.version>4.2.0</guice.version>
</properties>
</project>

View File

@ -0,0 +1,21 @@
package com.baeldung.autofactory;
import com.baeldung.autofactory.model.Camera;
import com.baeldung.autofactory.model.Phone;
import com.baeldung.autofactory.model.PhoneFactory;
import com.baeldung.autofactory.modules.SonyCameraModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class App {
public static void main(String[] args) {
PhoneFactory phoneFactory = new PhoneFactory(() -> new Camera("Unknown", "XXX"));
Phone simplePhone = phoneFactory.create("other parts");
Injector injector = Guice.createInjector(new SonyCameraModule());
PhoneFactory injectedFactory = injector.getInstance(PhoneFactory.class);
Phone xperia = injectedFactory.create("Xperia");
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.autofactory;
import com.baeldung.autofactory.custom.SmartPhone;
/**
* @author aiet
*/
public interface CustomStorage {
SmartPhone customROMInGB(int romSize);
}

View File

@ -0,0 +1,10 @@
package com.baeldung.autofactory.custom;
/**
* @author aiet
*/
public abstract class AbstractFactory {
abstract CustomPhone newInstance(String brand);
}

View File

@ -0,0 +1,16 @@
package com.baeldung.autofactory.custom;
import com.google.auto.factory.AutoFactory;
/**
* @author aiet
*/
@AutoFactory(extending = AbstractFactory.class)
public class CustomPhone {
private final String brand;
public CustomPhone(String brand) {
this.brand = brand;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.autofactory.custom;
import com.baeldung.autofactory.CustomStorage;
import com.google.auto.factory.AutoFactory;
/**
* @author aiet
*/
@AutoFactory(className = "SamsungFactory", allowSubclasses = true, implementing = CustomStorage.class)
public class SmartPhone {
private int romSize;
public SmartPhone(int romSize) {
this.romSize = romSize;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.autofactory.model;
/**
* @author aiet
*/
public class Camera {
private final String manufacturer;
private final String serial;
public Camera(String manufacturer, String serial) {
this.manufacturer = manufacturer;
this.serial = serial;
}
public String getManufacturer() {
return manufacturer;
}
public String getSerial() {
return serial;
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.autofactory.model;
import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
/**
* @author aiet
*/
public class ClassicPhone {
private final String dialpad;
private final String ringer;
private String otherParts;
@AutoFactory
public ClassicPhone(@Provided String dialpad, @Provided String ringer) {
this.dialpad = dialpad;
this.ringer = ringer;
}
@AutoFactory
public ClassicPhone(String otherParts) {
this("defaultDialPad", "defaultRinger");
this.otherParts = otherParts;
}
public String getDialpad() {
return dialpad;
}
public String getRinger() {
return ringer;
}
public String getOtherParts() {
return otherParts;
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.autofactory.model;
import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
import javax.inject.Named;
/**
* @author aiet
*/
@AutoFactory
public class Phone {
private Camera camera;
private String otherParts;
public Phone(@Provided @Named("Sony") Camera camera, String otherParts) {
this.camera = camera;
this.otherParts = otherParts;
}
/* required when used as a base class for AutoFactory */
public Phone() {
}
public Camera getCamera() {
return camera;
}
public String getOtherParts() {
return otherParts;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.autofactory.modules;
import com.baeldung.autofactory.model.Camera;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import javax.inject.Named;
/**
* @author aiet
*/
public class SonyCameraModule extends AbstractModule {
private static int SONY_CAMERA_SERIAL = 1;
@Named("Sony")
@Provides
Camera cameraProvider() {
return new Camera("Sony", String.format("%03d", SONY_CAMERA_SERIAL++));
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.autofactory.provided;
import com.google.auto.factory.AutoFactory;
import com.google.auto.factory.Provided;
import javafx.scene.Camera;
import javax.inject.Provider;
/**
* @author aiet
*/
@AutoFactory
public class IntermediateAssembler {
private final Provider<Camera> camera;
private final String otherParts;
public IntermediateAssembler(@Provided Provider<Camera> camera, String otherParts) {
this.camera = camera;
this.otherParts = otherParts;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.autofactory.provider;
import com.baeldung.autofactory.model.Camera;
import com.google.inject.Provider;
/**
* @author aiet
*/
public class SonyCameraProvider implements Provider<Camera> {
private static int sonyCameraSerial = 1;
@Override
public Camera get() {
return new Camera("Sony", String.format("%03d", sonyCameraSerial++));
}
}