Jumat, 17 Februari 2012

Property

Sebuah  field dalam class memiliki access modifier  tertentu, bisa private, public, atau protected.
Kita  harus  bijaksana  dalam menggunakan  access modifier  field  ini,  jika  tidak  berhati-hati  bisa
menyebakan desain class kita rentan terhadap perubahan dikemudian hari.   Salah satu point yang
hendak dicapai dalam Object Oriented Desain adalah meminimalkan terjadinya ripple effect. Efek
ini bisa terjadi jika perubahan class yang satu mengakibatkan perubahan di class  lainnya,  istilah
lainnya  adalah  "tighty-coupled".  Demi  Encapsulation,  variabel/data  tidak  boleh  diekspose
langsung,  karena  itu  access modifier  nya  harus  private.  Lalu  bagaimana  cara  saya mengakses
variabel  tersebut  dari  class  lain.  Bukankah  kalau  access  modifier  nya  private  hanya  dikenal
diclass  tersebut? Variabel hanya boleh diakses  lewat method  accessor dan mutator nya. What
the #$%@#  istilah apa  lagi itu accessor dan mutator? makin memusingkan saja OOP ini!

Kita lihat contoh berikut :
public class Customer
{
    //Field
    private string customerId;

    //Accessor

    public string GetCustomerId()
    {
        return customerId;
    }

    //Mutator

    public void SetCustomerId(string customerId)
    {
        this.customerId = customerId;
    }
}

Kita  punya  sebuah  field  bernama  customerId  dengan  tipe  string  dan  access  modifier  private.
Untuk mengakses  nilai  variabel  customerId  ini  harus  lewat method  GetCustomerId()  terlebuh
dahulu.  Jika  ingin mengubah  nilai  customerId  harus melalui method  SetCustomerId()  dengan
parameter  customerId.  Method  yang  pertama  disebut  Accessor  karena  fungsinya    untuk
mengakses nilai sebuah field, sedangkan yang kedua disebut mutator karena fungsinya mengubah
nilai.

Contoh Penggunaanya :

Customer cust = new Customer();
cust.SetCustomerId("XERIS"); //mengubah variabel customerId
Console.WriteLine(cust.GetCustomerId());//mengakses variabel customerId 

Di  .NET  ada  cara  efesien  untuk  memadukan  Accessor  dan  mutator  ini  dalam  satu  kesatuan
dengan menggunakan sebuah method tunggal yang disebut Property.

Contoh Poperty :

public class Customer
{
    //Field

    private string customerId;
    private string companyName;

    //Property

    public string CustomerId
    {
        get { return customerId; }
        set { customerId = value; }
    }

    public string CompanyName
    {
        get { return companyName; }
        set { companyName = value; }
    }
}

pada property CustomerId, method Accessor diwakili oleh get dan mutator oleh set. Lebih simple
bukan :-)
 
Cara Penggunaannya :

Customer cust = new Customer();

//memodifikasi nilai variabel customerId

cust.CustomerId = "XERIS"; 

//menampilkan nilai variabel customerId

Console.WriteLine(cust.CustomerId); 

Code Lengkap :
public class Customer
{
    private string customerId;
    private string companyName;
    private string contactName;
    private string address;
    private string phone;

    //Constructor Default

    public Customer()
    {

    }

    //Constructor Overloading

    public Customer(string customerId, string companyName,
        string contactName, string address, string phone)
    {
        this.customerId = customerId;
        this.companyName = companyName;
        this.contactName = contactName;
        this.address = address;
        this.phone = phone;

    }

    public string CustomerId
    {
        get { return customerId; }
        set { customerId = value; }
    }

    public string CompanyName
    {
        get { return companyName; }
        set { companyName = value; }
    }

    public string ContactName
    {
        get { return contactName; }
        set { contactName = value; }
    }

    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    public string phone
    { 
        get { return phone; }
        set { phone = value; }
    }
}

Tidak ada komentar:

Posting Komentar