0

I am still trying to understand LSP. From what I understand so far, Subclasses / Subtypes should be able to substitute Baseclass / Main type and the program should work intact.

I have the following...

  abstract class Warehouse<T> {
      private StockLoader<T> loader;
      private List<T> stock;

      protected setStockLoader(StockLoader loader) {
             this.loader = loader;
      }

      protected List<T> getStock() {
           return stock;
      }

      public void load() {
         stock = loader.load();
      }

      abstract showStock();
  }

 class WheatWH extends Warehouse<Wheat> {
     @Override
     public void showStock() {
         // Print stocck with getStock() returns Wheat;
     }
 }

 class RiceWH extends Warehouse<Rice> {
     @Override
     public void showStock() {
         // Print stocck with getStock() returns Rice;
     }
 }


  interface List<T> StockLoader<T>() {
      public List<T> load();     
  }

  class PlainStockLoader implements StockLoader<Wheat>{
      public List<Wheat> load() {
             //Code
      }
  }

  class Rice {
      .....
  }

  class Wheat {
      ....
  }

Does it violate LSP? If not, what would violate LSP in the above progream? In addition, does this violate any other principle? Can this be improved in any other way?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
  • `public void showStock() { // Print stocck with getStock() returns Wheat; }` a void method cannot "return Wheat". Also, it is important to tell us why you think this code violates the LSP. – Jim Garrison Mar 25 '16 at 19:45
  • 1
    I can't see any violations of LSP. What aspect of it do you think might be a violation? – Paul Boddington Mar 25 '16 at 19:46
  • 2
    Is there a reason why `Warehouse` is abstract? Creating a subclass for each different content type seems excessive when the class is already generic. – Mick Mnemonic Mar 25 '16 at 19:49
  • @MickMnemonic showStock should be different for each type of stock. So I need to enforce it. Is there any other way to do it? – Kevin Rave Mar 25 '16 at 21:13
  • Depends on what `showStock` should do. Could you achieve the same by implementing `toString()` for each stock type (`Wheat`, `Rice` etc.) and then calling that method from `Warehouse.showStock()`? – Mick Mnemonic Mar 25 '16 at 21:20
  • @MickMnemonic Nope. They have to be different..So in that case, abstract makes sense? – Kevin Rave Mar 25 '16 at 21:25
  • If it's just that one method that needs to be polymorphic then perhaps you could use the [visitor pattern](https://en.m.wikipedia.org/wiki/Visitor_pattern) for providing implementations for different kinds of stock. – Mick Mnemonic Mar 25 '16 at 21:37
  • @MickMnemonic That seems like a good one. But I will have a couple other separate methods. – Kevin Rave Mar 25 '16 at 21:44

1 Answers1

1

What you have looks perfectly fine. The goal here would be to have an implementation such that you could do something like:

Warehouse<Wheat> wheat = new WheatWH();
Warehouse<Rice> rice = new RiceWH();

And then be able to call methods from the Warehouse class on wheat and rice regardless of which subclass they are. The code you have so far does this perfectly, so I would say you are definitely on the right track.

NAMS
  • 983
  • 7
  • 17