- Prefer coding against interfaces and not abstract classes.
Reasons:
- A way to design by contract. This way developers will know exactly what the need to implement and consume.
- A way to reuse code. An object can implement a couple interfaces and the client can call any one of those interfaces to implement that object.
Example 1:
public interface IContact
{
String name();
String phoneNumber();
int id();
}
public class Employee implements IContact
{
private String name;
private String phoneNumber;
private int id;
public String name(){ return this.name;}
public String phoneNumber() {return this.phoneNumber}
public int id() {return this.id;}
public Employee(id) {this.id = id;}
public Employee(id,name,phoneNumber)
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
public void DoSomeStuffOnEmployee() {...}
}
public class Employer implements IContact
{
private String name;
private String phoneNumber;
private int id;
public String name(){ return this.name;}
public String phoneNumber() {return this.phoneNumber}
public int id() {return this.id;}
public Employer(id) {this.id = id;}
public Employer(id,name,phoneNumber)
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
private int ReturnSomeValue() {...}
public void DoSomeStuffOnEmployer() {...}
}
public class SomeImplementationClass
{
public static void DoCrap(IContact contact)
{
String name = contact.name();
String id = contact.id();
}
}
to call stuff (SHOWS CODE REUSE and DESIGN BY CONTRACT):
public static void main(String[] args)
{
SomeImplementationClass.DoCrap(new Employee(1));
SomeImplementationClass.DoCrap(new Employer(5));
}
============================
Example 2.
public interface IContact
{
String name();
String phoneNumber();
int id();
}
public interface ICompany
{
String placeOfWork();
String address();
}
public class BasePerson implements IContact, ICompany
{
protected String name;
protected String phoneNumber;
protected int id;
protected String placeOfWork;
protected String address;
public String name(){ return this.name;}
public String phoneNumber() {return this.phoneNumber}
public String placeOfWork() {..};
public String address() {..};
public int id() {return this.id;}
private Employee( {}
//code reuse
protected void DoSomeStuffOn() {...}
}
public class Employer extends BasePerson
{
public Employer(id) {this.id = id;}
public Employer(id,name,phoneNumber)
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
private int ReturnSomeValue() {...}
private void DoSomeStuffOnEmployer()
{
this.DoSomeStuffOn();
}
}
public class Employee extends BasePerson
{
public Employee(id) {this.id = id;}
public Employee(id,name,phoneNumber)
{
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
}
private int ReturnSomeValue() {...}
private void DoSomeStuffOnEmployer()
{
this.DoSomeStuffOn();
}
}
public class SomeImplementationClass
{
public static void DoCrap(IContact contact)
{
String name = contact.name();
String id = contact.id();
}
public static void DoOtherCrap(ICompany contact)
{
String placeOfWork= contact.placeOfWork();
}
}
to call stuff (SHOWS CODE REUSE and DESIGN BY CONTRACT):
public static void main(String[] args)
{
SomeImplementationClass.DoCrap(new Employee(1));
SomeImplementationClass.DoOtherCrap(new Employer(5));
SomeImplementationClass.DoCrap(new Employer(11));
SomeImplementationClass.DoOtherCrap(new Employee(55));
}
No comments:
Post a Comment