সোমবার, ১৮ নভেম্বর, ২০১৩

Null Object Pattern


  • Special Case pattern bv‡gI cwiwPZ| Generally Null Object GKwU Subclass.
  • Particular case mg~‡n Bnv special behavior provide Ki‡e i.e. null object return Ki‡e|
  • hLb user GKwU class Gi valid instance specify Ki‡Z bv Pvq wKsev bv cv‡i Ges null reference pass Ki‡Z bv Pvq ZLb Bnv useful
  • Null object Gi role n‡jv Bnv null reference ‡K replace K‡i Ges same interface implement Ki‡e wKš‘ †Kvb behavior _vK‡e bv|
 UML Diagram:





  • Client -
    • requires a collaborator.
  • AbstractObject -
    • declares the interface for Client’s collaborator
    • implements default behavior for the interface common to all classes, as appropriate
  • <strong>RealObject</strong> -
    • defines a concrete subclass of AbstractObject whose instances provide useful behavior that Client expects
  • <strong>NullObject</strong> -
    • provides an interface identical to AbstractObject’s so that a null object can be substituted for a real object
    • implements its interface to do nothing. What exactly it means to do nothing depends on what sort of behavior Client is expecting
    • when there is more than one way to do nothing, more than one NullObject class may be required
Code Example

  • ProductService class Gi data cache Ki‡Z bv PvB‡j wKfv‡e Null Object pattern use Ki‡Z n‡e Zv wb‡¤œ †`Lv‡bv n‡jv-
  • Project G GKwU new class Add Kwi Ges Dnvi name w`B NullObjectCache Ges Dnvi definition wb¤œiƒc-
public class NullObjectCache : ICacheStorage
{
public void Remove(string key)
{
// Do nothing
}
public void Store(string key, object data)
{
// Do nothing
}
public T Retrieve<T>(string storageKey)
{
return default(T);
}
}
  • Gevi NullObjectCache ‡K ProductService G passed Kiv hv‡e|
  • hLbB data cache Ki‡Z n‡e ZLb Bnv wKQzB Ki‡e bv Ges memgq ProductService G null return Ki‡e (G‡ÿ‡Î ensure Ki‡e †Kvb data cached n‡e bv|)
 







 

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

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