শুক্রবার, ১৫ নভেম্বর, ২০১৩

Interface Segregation Principle (ISP)


"Clients should not be forced to imWplement interfaces they don’t use."
·         Client ‡K Zv‡`i AcÖ‡qvRbxq interface mg~n implement Ki‡Z force Kiv hv‡e bv
·         Ab¨fv‡e ejv hvq general purpose big single interface ‡_‡K client specific small many interface better.
·         A_©vr hw` †Kvb interface A‡bK eo nq Z‡e Bnv‡K multiple interface G break Ki‡Z n‡e|

Real World Comparison





·         g‡bKwi GKwU byZb desktop PC purchase Kiv n‡q‡Q|
·         PC Gi CPU ‡K couple of USB ports, some serial ports GKwU VGA port Av‡Q|
·         hw` CPU Gi Cache Open Kiv nq Z‡e †`Lv hv‡e motherboard G cÖPzi slot i‡q‡Q| GB mKj   slot Øviv wewfbœ parts mg~n‡K motherboard Gi mv‡_ connect Kiv nq| GB mKj slot mg~‡ni AwaKvskB hardware engineer iv  System assembly Gi mgq use K‡i|
·         GB mKj slot mg~n CPU Open bv Kwi‡j invisible _v‡K|
·         ïaygvÎ cÖ‡qvRbxq slot mg~nB me©`v mevi Kv‡Q available/visible _v‡K  Ges User slot mg~‡ni mv‡_ external device mg~n e.g.- USB port G Mouse, Keyboard, Pen drive etc, VGA port Gi mv‡_ Monitor etc. connect K‡i easily system use Ki‡Z cvi‡e|
·         hw` mKj slot mg~nB me©`v mevi Kv‡Q available/visible _v‡K Z‡e
o   User GK device slot G Ab¨ device Connect Kivi Chance _vK‡Zv e.g.- RAM slot G external VGA/AGP, SATA Slot G USB Connect etc Kvib Maximum User Gi Computer Mother board Gi internal mechanism m¤ú‡K© ch©vß aviYv _v‡K bv| d‡j hardware failure Gi chance A‡bK †e‡o hv‡e|

Identify Problem in Programming

  • Avgiv ASP.NET 2.0 custom Membership Provider wb‡q KvR Ki‡ev|
  • Membership Provider use K‡i Avgiv ASP.NET Gi built in user management Gi KwZcq functionality †K Avgv‡`i System Gi User management G integrate Ki‡ev|
  • GRb¨ Avgv‡`i‡K ASP.NET Gi built in abstract class MembershipProvider ‡K Avgv‡`i CustomMembershipProvider class G implement Ki‡Z n‡e|
  • MembershipProvider GKwU  fat interface/abstract class.
  • Avgiv hw` mivmwi D³ MembershipProvider class ‡K Avgv‡`i CustomMembershipProvider class G implement Kwi Z‡e Avgv‡`i‡K D³ MembershipProvider G defined mKj member mg~n‡K implement Ki‡Z n‡e hvnvi d‡j Avgv‡`i‡K CustomMembershipProvider class G cÖPzi cwigv‡b code wjL‡Z n‡e| ïay ZvB bq Avgv‡`i‡K Avgv‡`i Proposed system Gi Rb¨ AcÖ‡qvRbxq method mg~nI implement Ki‡Z n‡e| wb‡¤œi code G Zvi wek` weeiY †`qv n‡jv-
public class CustomMembershipProvider : MembershipProvider
{
    public override string ApplicationName
    {
        get
        {
            throw new Exception("The method or operation is not implemented.");
        }
        set
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }

    public override bool ChangePassword(string username, string oldPassword, string newPassword)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override bool ChangePasswordQuestionAndAnswer(string username, string password,
        string newPasswordQuestion, string newPasswordAnswer)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override MembershipUser CreateUser(string username, string password, string email,
        string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey,
        out MembershipCreateStatus status)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override bool DeleteUser(string username, bool deleteAllRelatedData)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override bool EnablePasswordReset
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override bool EnablePasswordRetrieval
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex,
        int pageSize, out int totalRecords)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex,
        int pageSize, out int totalRecords)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override int GetNumberOfUsersOnline()
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override string GetPassword(string username, string answer)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override MembershipUser GetUser(string username, bool userIsOnline)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override string GetUserNameByEmail(string email)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override int MaxInvalidPasswordAttempts
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override int MinRequiredNonAlphanumericCharacters
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override int MinRequiredPasswordLength
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override int PasswordAttemptWindow
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override MembershipPasswordFormat PasswordFormat
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override string PasswordStrengthRegularExpression
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override bool RequiresQuestionAndAnswer
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override bool RequiresUniqueEmail
    {
        get { throw new Exception("The method or operation is not implemented."); }
    }

    public override string ResetPassword(string username, string answer)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override bool UnlockUser(string userName)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override void UpdateUser(MembershipUser user)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public override bool ValidateUser(string username, string password)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
  • Avgviv nq‡Zv Membership provider class Gi 2/3 wU method Avgv‡`i User management G e¨envi Ki‡ev| Zvi Rb¨ Avgv‡`i GZ¸‡jv Line Gi code wjL‡Z n‡q‡Q hvnv KL‡bvB MÖnb‡hvM¨ bq|

Solution which will not violate LSP

Code Example
·         To demonstrate the principle of Interface Segregation, you will work through a small exercise that revolve around the domain of a product catalog.
·         Interface Segregation principle demonstrate Kivi Rb¨ Avgiv †h exercisewU Ki‡ev Dnv n‡jv product catalog domain
·         eZ©gvb product catalog G DVD I Blu-Ray disc Gi  movie products Av‡Q





·         Dfq class B IProduct interface †K implement K‡i‡Q|
·         class Øq Ges interface Gi definition wb¤œiƒc-

using System;
public interface IProduct
{
    decimal Price { get; set; }
    decimal WeightInKg { get; set; }
    int Stock { get; set; }
    int Certification { get; set; }
    int RunningTime { get; set; }
}

using System;
public class DVD : IProduct
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}

using System;
public class BluRayDisc : IProduct
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}

·         GLb ch©šÍ mKj wKQzB mwVKfv‡e Pj‡Q|
·         GLb Avgiv byZb GKwU product type introduce Kwi hvnv movie bq
·          TShirt bv‡g byZb GKwU class add Kwi| BnvI IProduct interface implement Ki‡e|
using System;
public class TShirt : IProduct
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}
·         GLb Avgiv †h mKj issue mg~n movie product Ges TShirt class Gi g‡a¨ cv_©K¨ M‡o Zv‡`i‡K split K‡i ‡dj‡ev Ges movie-specific GKwU interface G add Ki‡ev|
using System;
public interface IMovie
{
    int Certification { get; set; }
    int RunningTime { get; set; }
}
·         GLb Avgiv Certification I RunningTime properties †K IProduct interface †_‡K remove K‡i †`e Ges TShirt class update Ki‡ev-
using System;
public class TShirt : IProduct
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
}
·         GLb Avgv‡`i ensure Ki‡Z n‡e DVD I BluRayDisc class IMovie interface implement K‡i-
using System;
public class DVD : IProduct, IMovie
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}

using System;
public class BluRayDisc : IProduct, IMovie
{
    public decimal Price { get; set; }
    public decimal WeightInKg { get; set; }
    public int Stock { get; set; }
    public int Certification { get; set; }
    public int RunningTime { get; set; }
}

  • bx‡Pi Figure Øviv refactor cieZ©x class mg~n †`Lv‡bv n‡jv-
 






  • Interface splitting Gi gva¨‡g Avgiv reuse capability increase Ki‡Z cvwi Ges code mnR‡eva¨ Ki‡Z cvwi|



কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন