Tải bản đầy đủ (.pdf) (5 trang)

Giáo trình hướng dẫn truy cập phương thức gán đối tượng cho một giao diện đối lập trừu tượng p3 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (444.27 KB, 5 trang )

Ngôn Ngữ Lập Trình C#
// bộ khởi tạo lớp Document lấy một tham số
public Document( string s)
{
Console.WriteLine(“Creating document with: {0}”, s);
}
// thực thi giao diện IStorable
public void Read()
{
Console.WriteLine(“Implementing the Read Method for IStorable”);
}
public void Write( object o)
{
Console.WriteLine(“Implementing the Write Method for IStorable”);
}
public int Status
{
get
{
return status;
}
set
{
status = value;
}
}
// thực thi ICompressible
public void Compress()
{
Console.WriteLine(“Implementing Compress”);
}


public void Decompress()
{
Console.WriteLine(“Implementing Decompress”);
}
// thực thi giao diện ILoggedCompressible
public void LogSavedBytes()
{
Console.WriteLine(“Implementing LogSavedBytes”);
Thực Thi Giao Diện
183
Ngôn Ngữ Lập Trình C#
}
// thực thi giao diện IStorableCompressible
public void LogOriginalSize()
{
Console.WriteLine(“Implementing LogOriginalSize”);
}
// thực thi giao diện
public void Encrypt()
{
Console.WriteLine(“Implementing Encrypt”);
}
public void Decrypt()
{
Console.WriteLine(“Implementing Decrypt”);
}
// biến thành viên lưu dữ liệu cho thuộc tính
private int status = 0;
}
public class Tester

{
public static void Main()
{
// tạo đối tượng document
Document doc = new Document(“Test Document”);
// gán đối tượng cho giao diện
IStorable isDoc = doc as IStorable;
if ( isDoc != null)
{
isDoc.Read();
}
else
{
Console.WriteLine(“IStorable not supported”);
}
ICompressible icDoc = doc as ICompressible;
if ( icDoc != null )
{
icDoc.Compress();
Thực Thi Giao Diện
184
Ngôn Ngữ Lập Trình C#
}
else
{
Console.WriteLine(“Compressible not supported”);
}
ILoggedCompressible ilcDoc = doc as ILoggedCompressible;
if ( ilcDoc != null )
{

ilcDoc.LogSavedBytes();
ilcDoc.Compress();
// ilcDoc.Read(); // không thể gọi được
}
else
{
Console.WriteLine(“LoggedCompressible not supported”);
}
IStorableCompressible isc = doc as IStorableCompressible;
if ( isc != null )
{
isc.LogOriginalSize(); // IStorableCompressible
isc.LogSavedBytes(); // ILoggedCompressible
isc.Compress(); // ICompress
isc.Read(); // IStorable
}
else
{
Console.WriteLine(“StorableCompressible not supported”);
}
IEncryptable ie = doc as IEncryptable;
if ( ie != null )
{
ie.Encrypt();
}
else
{
Console.WriteLine(“Encryptable not supported”);
}
}

Thực Thi Giao Diện
185
Ngôn Ngữ Lập Trình C#
}

 Kết quả:
Creating document with: Test Document
Implementing the Read Method for IStorable
Implementing Compress
Implementing LogSavedBytes
Implementing Compress
Implementing LogOriginalSize
Implementing LogSaveBytes
Implementing Compress
Implementing the Read Method for IStorable
Implementing Encrypt

Ví dụ 8.2 bắt đầu bằng việc thực thi giao diện IStorable và giao diện ICompressible. Sau đó
là phần mở rộng đến giao diện ILoggedCompressible rồi sau đó kết hợp cả hai vào giao diện
IStorableCompressible. Và giao diện cuối cùng trong ví dụ là IEncrypt.
Chương trình Tester tạo đối tượng Document mới và sau đó gán lần lượt vào các giao diện
khác nhau. Khi một đối tượng được gán cho giao diện ILoggedCompressible, chúng ta có thể
dùng giao diện này để gọi các phương thức của giao diện ICompressible bởi vì ILogged-
Compressible mở rộng và thừa kế các phương thức từ giao diện cơ sở:
ILoggedCompressible ilcDoc = doc as ILoggedCompressible;
if ( ilcDoc != null )
{
ilcDoc.LogSavedBytes();
ilcDoc.Compress();
// ilcDoc.Read(); // không thể gọi được

}
Tuy nhiên, ở đây chúng ta không thể gọi phương thức Read() bởi vì phương thức này của
giao diện IStorable, không liên quan đến giao diện này. Nếu chúng ta thêm lệnh này vào thì
chương trình sẽ biên dịch lỗi.
Nếu chúng ta gán vào giao diện IStorableCompressible, do giao diện này kết hợp hai giao
diện IStorable và giao diện ICompressible, chúng ta có thể gọi tất cả những phương thức của
IStorableCompressible, ICompressible, và IStorable:
IStorableCompressible isc = doc as IStorableCompressible;
if ( isc != null )
{
isc.LogOriginalSize(); // IStorableCompressible
Thực Thi Giao Diện
186
Ngôn Ngữ Lập Trình C#
isc.LogSaveBytes(); // ILoggedCompressible
isc.Compress(); // ICompress
isc.Read(); // IStorable
}
Truy cập phương thức giao diện
Chúng ta có thể truy cập những thành viên của giao diện IStorable như thể là các thành viên
của lớp Document:
Document doc = new Document(“Test Document”);
doc.status = -1;
doc.Read();
hay là ta có thể tạo thể hiện của giao diện bằng cách gán đối tượng Document cho một kiểu
dữ liệu giao diện, và sau đó sử dụng giao diện này để truy cập các phương thức:
IStorable isDoc = (IStorable) doc;
isDoc.status = 0;
isDoc.Read();
Ghi chú: Cũng như đã nói trước đây, chúng ta không thể tạo thể hiện của giao diện một

cách trực tiếp.Do đó chúng ta không thể thực hiện như sau:
IStorable isDoc = new IStorable();
Tuy nhiên chúng ta có thể tạo thể hiện của lớp thực thi như sau:
Document doc = new Document(“Test Document”);
Sau đó chúng ta có thể tạo thể hiện của giao diện bằng cách gán đối tượng thực thi đến kiểu
dữ liệu giao diện, trong trường hợp này là IStorable:
IStorable isDoc = (IStorable) doc;
Chúng ta có thể kết hợp những bước trên như sau:
IStorable isDoc = (IStorable) new Document(“Test Document”);
Nói chung, cách thiết kế tốt nhất là quyết định truy cập những phương thức của giao diện
thông qua tham chiếu của giao diện. Do vậy cách tốt nhất là sử dụng isDoc.Read(), hơn là sử
dụng doc.Read() trong ví dụ trước. Truy cập thông qua giao diện cho phép chúng ta đối xử
giao diện một cách đa hình. Nói cách khác, chúng ta tạo hai hay nhiều hơn những lớp thực thi
giao diện, và sau đó bằng cách truy cập lớp này chỉ thông qua giao diện.
Gán đối tượng cho một giao diện
Trong nhiều trường hợp, chúng ta không biết trước một đối tượng có hỗ trợ một giao
diện đưa ra. Ví dụ, giả sử chúng ta có một tập hợp những đối tượng Document, một vài đối
tượng đã được lưu trữ và số còn lại thì chưa. Và giả sử chúng ta đã thêm giao diện giao diện
thứ hai, ICompressible cho những đối tượng để nén dữ liệu và truyền qua mail nhanh chóng:
interface ICompressible
{
Thực Thi Giao Diện
187

×