E-Gürbüz

Interface-Segregation Principle

Posted in OOP by Emre on Temmuz 23, 2010

http://www.globalnerdy.com/wordpress/wp-content/uploads/2009/07/interface_segregation_principle.jpg

  • “Many client specific interfaces are better than one general purpose interface“
  • “The dependency of one class to another one should depend on the smallest possible interface“
  • “Make fine grained interfaces that are client specific.“
  • “Clients should not be forced to depend upon interfaces that they don’t use. This principle deals with the disadvantages of fat interfaces. Fat interfaces are not cohesive. In other words the interfaces of classes should be broken into groups of member functions.“

Nesne Yönelimli Tasarım için tavsiye buyrulan prensiplerden biri de Interface Segregation Principle(ISP) ‘ dir. ISP, bir siniftan hizmet alan istemcilerin ilgi duymadiklari arayuzlerden soyutlanmasi, sadece ihtiyac duyduklari arayuzlere erisebilmesini saglamaktir. Bu sebeple her istemci için bütün metotlarin aynı arayüzde toplanması yanlış bir tasarımdır. Somut bir ornek vermek gerekirse; Insan günlük hayatında özel hayatıyla, iş hayatını beraber yürütür. Evinde uyuyup, yemek yer vb. İş yerinde ise çalışır, ücret kazanır. Kişi aynı kişidir ancak iş yerinde(mesela fabrika) bir Çalışan, bunun dışında bir Birey’dir. Bu senaryo için Fabrika istemcisi Insan varliginin Calisan arayuzlerini kullanmak isterken, Ev istemcisi onun Birey arayuzlerini kullanmak ister. Iyi bir tasarim olması açısından Fabrika istemcisini Birey arayuzunden, Ev istemcisini de Calisan arayuzunden soyutlamak gerekmektedir. Problem cozumu için Insan sınıfının gerceklestirecegi 2 tane arayuz(Calisan ve Birey) tanımlamak gerekir. Daha sonra Fabrika, Insan’a Calisan arayuzunden, Ev ise Birey arayuzunden baglanmalidir.

public interface Calisan {

public void calis();

public void maasHesapla();

}

public interface Birey {

public void uyu();

public void yemek();

}

public class Insan implements Calisan, Birey {

}

Tagged with: , , ,
Takip Et

Get every new post delivered to your Inbox.