Articles → .NET → Interface Segregation Principal C#

Interface Segregation Principal C#







  1. Children
  2. Serviceman
  3. Businessman
  4. Old age people


public interface IInsurance {
	void CarInsurance();
	void MedicalInsurance();
	void TermInsurance();
}




public class Children: IInsurance {
	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}
public class ServiceMan: IInsurance {
	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}

public class BusinessMan: IInsurance {
	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}

public class OldAge: IInsurance {
	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}




  1. There is no relevance of car insurance for children
  2. There is no relevance of providing medical insurance to a serviceman because their employer has already opted the insurance for their employees (because of the government policy).
  3. No term insurance for old age people because according to the insurance company, maximum age should be 40.


  1. CarInsurance method is not relevant for Children class.
  2. MedicalInsurance method is not relevant for ServiceMan class.
  3. TermInsurance method is not relevant for OldAge class.



Interface Segregation Principle For Rescue




public interface ICarInsurance {
	void CarInsurance();
}

public interface IMedicalInsurance {
	void MedicalInsurance();
}
public interface ITermInsurance {
	void TermInsurance();
}




public class Children: IMedicalInsurance {

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}
}
public class ServiceMan: ICarInsurance,
ITermInsurance {

	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}

public class BusinessMan: ICarInsurance,
IMedicalInsurance,
ITermInsurance {

	public void CarInsurance() {
		throw new NotImplementedException();
	}

	public void MedicalInsurance() {
		throw new NotImplementedException();
	}

	public void TermInsurance() {
		throw new NotImplementedException();
	}
}

public class OldAge: ICarInsurance {

	public void CarInsurance() {
		throw new NotImplementedException();
	}
}





Posted By  -  Karan Gupta
 
Posted On  -  Thursday, June 4, 2015

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250