CSC/ECE 517 Fall 2009/wiki3 13 ncs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
Line 18: Line 18:
== Code example ==
== Code example ==


Let us consider the automobiles car and truck. We can either define all the shared methods and behaviors implicitly as methods within the class  
Let us consider the Automobile class. We can either define all the shared methods and behaviors implicitly as methods within the class  


<code>class Automobile {
<source lang="java">
public String getMake() {...}
 
public String getModel() {...}
class Automobile {
public String getYear() {...}
        public Automobile () {}
public String getLicensePlate() {...}
public String getMake() {}
public String getLicenseExpiration(){...}
public String getModel() {}
public String getYear() {}
public String getLicensePlate() {}
public String getLicenseExpiration() {}
 
public void turn() {}
public void accelerate() {}
public void stop() {}


public void turn(){...}
public String getSpeed() {}
public void accelerate(){...}
public String getRPM() {}
public void stop(){...}
public String getFuelLevel() {}
}


public String getSpeed(){...}
</source>
public String getRPM(){...}
public String getFuelLevel(){...}
}</code>


Alternatively define behaviors explicitly and have the car and truck class implement them.  
Alternatively define behaviors explicitly and have the car and truck class implement them.  


<code>
<source lang="java">
public interface Autospec{
interface Autospec{
public String getMake();
public String getMake();
public String getModel();
public String getModel();
Line 48: Line 53:
}
}


public interface OperateAuto {
interface OperateAuto {
public void turn();
public void turn();
public void accelerate();
public void accelerate();
Line 54: Line 59:
}
}


public interface AutoDashboard{
interface AutoDashboard{
public String getSpeed();
public String getSpeed();
public String getRPM();
public String getRPM();
Line 79: Line 84:
public String getFuelLevel(){...}
public String getFuelLevel(){...}
}
}
</code>
</source>


= Advantages of using explicit interface =
= Advantages of using explicit interface =

Revision as of 01:25, 17 November 2009

Introduction

Bertrand Meyer's principle of explicit interface states that when 2 modules A and B communicate, this must be obvious from the text of A or B or both.

In this article we will consider the Pro's and cons of this approach. When it does and does not make sense to follow this principle.

Interface (Implicit vs Explicit)

A generic definition of an interface in programming context can be defined as a set of methods exposed that can be invoked by other objects.

For most object oriented languages all protected and public methods of the Object will constitute to be the Object's interface.

Most object oriented languages provide a means to specify a specific behavior that the objects can implement. For example If we wish to implement the behavior of automobile we could let

each class that is an automobile have all the methods implemented without using an explicit interface or have a formal explicit interface defined that defines the automobile behavior

and have each automobile object implement the well defined Automobile interface.

Code example

Let us consider the Automobile class. We can either define all the shared methods and behaviors implicitly as methods within the class

class Automobile {
        public Automobile () {}
	public String getMake() {}
	public String getModel() {}
	public String getYear() {}
	public String getLicensePlate() {}
	public String getLicenseExpiration() {}

	public void turn() {}
	public void accelerate() {}
	public void stop() {}

	public String getSpeed() {}
	public String getRPM() {}
	public String getFuelLevel() {}
}

Alternatively define behaviors explicitly and have the car and truck class implement them.

interface Autospec{
	public String getMake();
	public String getModel();
	public String getYear();
	public String getLicensePlate();
	public String getLicenseExpiration();

}

interface OperateAuto {
	public void turn();
	public void accelerate();
	public void stop();
}

interface AutoDashboard{
	public String getSpeed();
	public String getRPM();
	public String getFuelLevel();
}

class Automobile implements Autospec, OperateAuto, AutoDashboard{
	// AutoSpec
	public String getMake(){...}
	public String getModel(){...}
	public String getYear(){...}
	public String getOwner() {...}
	public String getLicensePlate(){...}
	public String getLicenseExpiration(){...}

	// OperateAuto
	public void turn(){...}
	public void accelerate(){...}
	public void stop(){...}

	// AutoDash
	public String getSpeed(){...}
	public String getRPM(){...}
	public String getFuelLevel(){...}
}

Advantages of using explicit interface

Let us consider the advantages of having explicit interfaces

Understandability

The behaviors are well understood and helps us understand the functionality as a set of smaller related operations. By documenting the behaviors it is easier to understand each set and

the behavior individually and it is clear by looking at the Auto class that it implements these behavior. The interface exposed by the auto class can be easily understood.

Continuity

It is very easy to find the elements that a potential change can effect. In our example above if we have explicit interfaces to determine the impact of change in any of the interface

methods or modifying an interface itself can be easily determined by the determining the sets of elements that either implement the interface or invoke it.

Composability and Decomposability

It is easy to determine the impact if we have the need to decompose a module further. For example if we feel the need to split the specification interface into two one that deals with

the specs and the other that deals with DMV then any and all outside dependencies are clearly visible. We simply need to look at elements that use the Autospec interface only.


Disadvantages of using explicit interfaces

Not really any

The only case it might not make sense is if the class itself has only one


Conclusion

Bertrand Meyer's principle does help us understand the code better and makes it easier to identify the impacted elements much more easily if there are any changes in the interface or we intend to compose or decompose the objects in smaller pieces.