Введите массив Объектов в Guice

Я думаю, что вы должны использовать (i) autolayout, чтобы установить ограничения на вид, зафиксировав его положение или высоту по ширине. для установки ориентации для портрета и пейзажа вы должны использовать классы размеров

(ii) Другой прием - вы можете использовать UIViewController, чтобы исправить ориентацию, используя делегат ориентации маски. затем добавить его добавить ребенка другого UIViewController enter image description here

13
задан Rich 19 February 2015 в 08:33
поделиться

3 ответа

Мне любопытно, почему Вы хотите несколько объектов, созданных нетерпеливо. Вы могли бы иметь успех, вводящий a Provider<InjectedObject>, и вызов Provider.get() каждый раз Вам нужен экземпляр. Если Вам действительно нужно 5, Вы могли бы создать их в цикле:

public MyClass {
  private final List<InjectedObject> injectedObjects;

  @Inject
  public MyClass(Provider<InjectedObject> injectedObjectProvider) {
    injectedObjects = new ArrayList<InjectedObject>();
    for (int i = 0; i < 5; i++) {
      injectedObjects.add(injectedObjectProvider.get());
    }
  }
}
8
ответ дан 1 December 2019 в 19:51
поделиться

One option would be to inject a Provider into your class, as Jesse mentioned:

public class MyClass {
  private final List<InjectedObject> injectedObjects;

  @Inject
  public MyClass(Provider<InjectedObject> injectedObjectProvider) {
    List<InjectedObject> objects = new ArrayList<InjectedObject>();
    for (int i = 0; i < 5; i++) {
      objects.add(injectedObjectProvider.get());
    }
    injectedObjects = Collections.unmodifiableList(objects);
  }
}

Doing this can be problematic. If InjectedObject is scoped as @Singleton or @RequestScoped, then each time you call injectedObjectProvider.get() you would get the same reference. Another problem with injecting a Provider to do this is it wouldn't be clear from the API that MyClass depends on multiple instances of InjectedObject. Finally, you've hardcoded in MyClass that it needs to be injected with five instances.

It's rare that you will need to inject a Provider into an object. Usually when I do this, it's because the scope of the current object means that it will be longer-lived than the scope of the dependent object (for instance, a @Singleton that needs access to a @RequestScoped object).

Instead of injecting a Provider, you can inject a List into the constructor, and create a provider method in a Guice module:

@Provides
MyClass prividesMyClass(Provider<InjectedObject> injectedObjectProvider) {
  List<InjectedObject> objects = new ArrayList<InjectedObject>();
  for (int i = 0; i < 5; i++) {
   objects.add(injectedObjectProvider.get());
  }
  return new MyClass(objects);
}

(you could, of course, bind using a TypeLiteral)

Why is this better? Even though you are still hard-coding five objects in this code, it isn't hard-coded in MyClass, so clients of MyClass (including the tests for MyClass itself) can choose to construct the object in different ways.

If hard-coding this knowledge in a Guice module isn't a good idea, you can instead create an interface that has a more specific contract than Provider

public interface InjectedObjectRepository {
  List<InjectedObject> getInjectedObjects();
}

Even if you decide that you want MyClass to be responsible for knowing how many instances to create, you might want to create an interface (perhaps named InjectedObjectSupplier so you could document explicitly that you expect a unique instance every time.

8
ответ дан 1 December 2019 в 19:51
поделиться

Not sure if this suits your needs, but Multibindings worked for me when I needed to inject multiple elements of the same kind (it produces a set though).

16
ответ дан 1 December 2019 в 19:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: