JEE MVC, controller calls the interface instead of the interface implementation



I'm following this tutorial: http://wiki4.caucho.com/Building_a_simple_listing_in_JSP It creates a basic web application in Eclipse using the MVC pattern with Resin as the web container. Here's the part I don't understand.

For the model, it creates a POJO, Interface and Interface Implementation. The data represents a Book. When we get to the controller, it creates a simple servlet. It calls the interface instead of the interface implementation. How does the interface to execute the implementation class? Also what if there were two impl classes, how would the interface know which to call?


Related to : JEE MVC, controller calls the interface instead of the interface implementation
Explicit Interface Implementation demanded, but only one interface in use?
Programming Languages

Why does the following code have to have 'CalcUsable' defined using Explicit Interface Implementation syntax? (see last line of code) If I use non-explicit syntax, (ie. public decimal CalcUsable) I get an "inconsistent accessibility" error, specifically:

"Parameter type ...List (IVoucher) is less accessible than method CalcUsable(...)"

interface IVoucher
{
    string Serial { get; }
    decimal FaceValue { get; }
    string DiscountType { get; }
    string ApplyMsg { get; }
    string RejectMsg { get; }
    decimal CalcUsable(List<Product> products,
List<IVoucher> vouchers);
}

// 'GiftVoucher' models the simple voucher which has no use
restrictions.
//
public class GiftVoucher : IVoucher
{
    public string Serial { get; private set; }
    public decimal FaceValue { get; private set; }
    public string DiscountType { get; private set; }
    public string ApplyMsg { get; private set; }
    public string RejectMsg { get; private set; }

    public GiftVoucher(string serial, decimal faceValue)
    {
        Serial = serial;
        FaceValue = faceValue;
        ApplyMsg = string.Empty;
        RejectMsg = string.Empty;
        DiscountType = "Gift";
    }

    // 'CalcUsable' provides the voucher applicability logic.  
    //
    decimal IVoucher.CalcUsable(List<Product> products,
List<IVoucher> vouchers)
    {
         blar, blar, blar...

Is it possible for several classes to share an interface implementation?
Programming Languages

I have a number of java classes that are already implemented. I would like to add some behavior to these classes; this behavior in some cases could be identical for all classes. It is also assumed that I can not create a super class since all the classes are already implemented.

The question I have is what options do I have in this regard.

Interface: Is it possible that for some interfaces I implement them once and have classes share the single implementation (since in some cases the implementation required would be identical for all classes). This would avoid manual duplication of code in all classes.

I thought about defining an abstract class and having the classes extend the abstract class; the problem is that the sublcasses would in this case would not be a meaningful extension of the abstract class since they do not form a meaningful hierarchy.

Thanks.


issues with interface implementation in c#
Programming Languages

I have the following interface in my code

public interface IDevTestable
{
    bool RunTest(out List<Result> selfTestResults);
}

I have 4 different classes implementing the interface:

class A: IDevTestable
{
    bool RunTest(out List<Result> selfTestResults)
    {
     //get value1 
    }
}

class B: IDevTestable
{
    bool RunTest(out List<Result> selfTestResults)
    {
      //get value2
    }
}

class C: IDevTestable
{
    bool RunTest(out List<Result> selfTestResults)
    {
       //get value3 
    }
}

class D: IDevTestable
{
    bool RunTest(out List<Result> selfTestResults)
    {
      //get value4 
    }
}

When I loop over all the Assemblies in my code, and find the ones that implement the interface to try to call the RunTest method, it always runs the first implementation regardless.

How do I select a specific implementation that I would like to run at a given time from the code?

This is how I'm running the code to get all the implementations of an interface:

//dev is the driver name that has the implementation. I
checked that I got all the driver names correctly. 
foreach (object dev in Devices)
{
    IDevTestable obj = dev as IDevTestable;

    if (obj != null)
    {
       retVal = obj.RunTest(out indvTestResults);
    }
}

Every time it is only running the implementation by Class A. Any suggestions on what I might be missing, or could possibly add to make it run the specific implementation that I want it to run?


Including interface implementation inside interface right approach?
Programming Languages

While browsing the code of a project, I encountered an implementation of BinaryHeap where two implementations (using Array and Tree) were packed inside the interface itself. I found it somewhat complicated. The code is:

public interface BinaryHeap<T extends
Comparable<T>> extends IHeap<T>  {
   //some variables and other declarations.

   public static class BinaryHeapArray<T extends
Comparable<T>> implements BinaryHeap<T> { 
     //Implementation based on Array
   }

   public static class BinaryHeapTree<T extends
Comparable<T>> implements BinaryHeap<T> {
     //Implementation based on Tree
   }
}

Is there anything wrong with this approach? How can it be improved in terms of readability?


list interface implementation - JAva
Programming Languages

I have a basic question I think I am missing some fundamentals here. I read when implementing interface in class we have it must adhere all methods that are in the interface. Ex:

Public interface Vehicles
{
 public void hasWheels();
 public void hasEngine();
}

I implement this interface in my class car and adhere both these methods hasWheels() and hasEngine() in my class.

   public class Car implements Vehicles
    {
     public void hasWheels()
    {
     System.out.println("Car has wheels");
    }
    public void hasEngine()
    {
     System.out.println("Car has Engine");
    } 
    }

My question is: I read list is an interface and when I am implementing this in class why is the class not using all the methods from the interface like the above example.

Is it because the way we implement the list interface is java.util.ArrayList


JEE MVC, controller calls the interface instead of the interface implementation
Programming Languages

I'm following this tutorial: http://wiki4.caucho.com/Building_a_simple_listing_in_JSP It creates a basic web application in Eclipse using the MVC pattern with Resin as the web container. Here's the part I don't understand.

For the model, it creates a POJO, Interface and Interface Implementation. The data represents a Book. When we get to the controller, it creates a simple servlet. It calls the interface instead of the interface implementation. How does the interface to execute the implementation class? Also what if there were two impl classes, how would the interface know which to call?



Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .