Development docs
  • Introduction
  • General information
    • Development Ecosystem
    • Documentation
    • Testing
    • Continuous Integration
    • Code Style
    • Paradigms
    • Design Patterns
    • Architecture
    • Refactoring
    • Date Formats
  • Various
    • Technologies and services
      • Travis
      • GitHub
    • Databases
      • Database Kinds
      • Style Guide
      • Joins
    • Web
      • Template View
      • Interactive View
      • Open Graph
      • Twitter Card
    • Parsers
    • Regular Expression
    • File Formats
      • Properties
    • Logging
  • git
    • Configuration files
      • gitignore
      • gitattributes
    • Gitflow
    • Tools
  • Web
    • REST
    • SOAP
  • Architecture
    • SOA
  • Object Oriented Programming
    • Returns
  • Jenkins
    • Pipeline
      • Steps
      • Environment
      • Notifications
      • Scripts
  • Java
    • Environment
      • Development Ecosystem
      • IDE
    • General
    • Interfaces and Generics for a Service
      • Dependencies
      • Type Errors
      • Nested Type Errors
    • Creating New Instances Dynamically
      • Using Classes
      • Using Providers
Powered by GitBook
On this page

Was this helpful?

  1. Java
  2. Creating New Instances Dynamically

Using Providers

Java 8 added the provider interface, which eases creating new instances:

public class ModelObjectBuilderWithProvider<V extends ModelObject> implements ModelObjectBuilder<V> {

   private final Provider<V> provider;

   public ModelObjectBuilderWithProvider(final Provider<V> prov) {
      super();

      provider = prov;
   }

   public V getModelObject(final String name) {
      final V obj;

      obj = provider.get();
      obj.setName(name);

      return obj;
   }

}
new ModelObjectBuilderWithProvider(ModelObjectImpl::new);
PreviousUsing Classes

Last updated 6 years ago

Was this helpful?