Skip to content Skip to sidebar Skip to footer

Es6 Class And Class Instance As Parameter In Constructor

class A needs instance of class B. Should i create B instance inside A constructor, or create instance where class A is initiated and pass instance to A constructor? Plan A: class

Solution 1:

Plan A is ok if

  • Class B should not be replaced with class C in a future

  • Class B not using some external dependency like ajax or database or something else.

OR

  • Your application is proof of concept or you are really time limited

Plan B is good in all other cases.

It will bring you flexibility to change your A class behavior on the fly, grants more universality, helps with testing by mocking this dependency.

So I would say that plan A is about simplicity, plan B is about flexibility and testing. Choose whatever you need in every current case

Hope this helps.

Solution 2:

Your Plan B is definitely the better approach to take because it makes your class A more flexible and removes dependencies on how class B works.

Solution 3:

The first strategy is appropriate where both classes have a relationship where they are meaningless without each other in their exact form and the child instances have no special traits that would otherwise make providing them manually have any benefit.

The latter strategy of injecting the dependencies of the class is far more flexible. This is more apparent in a typed language where you are able to clearly define class abstractions or interfaces that describe what an object "looks like" rather than the actual implementation, e.g. with TypeScript:

classProcess {
  privatestorage: StorageInterface;

  constructor(storage: StorageInterface) {
    this.storage = storage;
  }
}

interfaceStorageInterface {
  store(data: any): Promise<void>;
}

It's clear here that a Process expect an instance of something that provides a function store but it doesn't care about what the actual implementation looks like - that is left up to the class implementing StorageInterface:

classPostgresStorageimplementsStorageInterface {
  publicasyncstore(data: any): Promise<void> {
    // do some postgres query
  }
}

classFilesystemStorageimplementsStorageInterface {
  publicasyncstore(data: any): Promise<void> {
    // store in the filesystem
  }
}

Now we have total flexibility over what type of "storage" we want an instance of a Process to use rather than it itself managing what storage it is able to use, the ability to create infinite storage types without modifying the rest of the codebase and more clarity over what a Process is interacting with.

// A process that stores something in a Postgres instance.const processA: Process = new Process(new PostgresStorage());

// A process that stores something on the filesystem.const processB: Process = new Process(new FilesystemStorage());

Solution 4:

The OO approach is a bit different in the JS world than in the strongly typed classical languages. Maybe this change of platform is an excellent opportunity to review your mindsets about how to reuse and think more laterally with the composition in mind.

https://en.wikipedia.org/wiki/Composition_over_inheritance

Post a Comment for "Es6 Class And Class Instance As Parameter In Constructor"