Thứ Tư, 3 tháng 12, 2008

C# 3.0 (C# 2008)

Những cái mới trong C# .NET 3.0

Một số bạn hỏi tôi rằng có phải C# 3.0 nằm trong .NET 3.0 Framework hay không? Xin thưa là không? Mà C# 3.0 được giới thiệu trong .NET 3.5. [Xem thêm ở đây]

Trong C# 3.0 có một số tính năng mới đã được giới thiệu, dưới đây là liệt kê tóm tắt các tính năng đó:

* Khai báo biến kiểu không tường minh (Implicitly typed local variables)
* Tạo kiểu (lớp) tạm thời (Anonymous types)
* Các phương thức mở rộng (Extension methods)
* Khởi tạo các Object và Collection (Object and collection initializers)
* Biểu thức Lambda (Lambda expressions)
* Biểu thức truy vấn LINQ (Query expressions)
* Biểu thức cây (Expression Trees)
* Tự động xây dựng Properties (Auto-Implemented Properties)

Nội dung dưới đây sẽ đi sâu chi tiết vào một số các tính năng mới này.
Implicitly Typed Local Variables

C# 3.0 đưa thêm 1 từ khoá mới gọi là "var". Từ khoá var cho phép định nghĩa 1 biến mà không cần khai báo kiểu tường minh, như ví dụ dưới đây là hợp lệ trong C# 3.0

var i = 1;

Một điểm lưu ý khi sử dụng khai báo này là giá trị gán vào phải được thực hiện ngay sau lệnh khai báo. Khi thực hiện lệnh gán thì biến đã được ngầm định khai báo kiểu tương ứng. Trong ví dụ:

var i = 1; // Correct
var i; //Incorrect

thì dòng lệnh thứ nhất khai báo là hợp lệ, và i được hiểu là kiểu int. Nhưng đối với dòng lệnh thứ 2, thì không được chấp nhận. Ngoài ra, kiểu dữ liệu được gán vào cũng không được là kiểu null.

Nếu ai đã từng sử dụng VB6 thì cũng lưu ý, đây không phải là kiểu Object cũng không phải là kiểu variant.

Đối với khởi tạo các kiểu dữ liệu mảng, thì có thể sử dụng syntax hơi khác một chút như ví dụ:
var intArr = new[] { 1, 2, 3, 4 };// Declare an array

Dòng code trên có thể được hiểu là intArr as int[];

Ngoài ra, var còn dùng để khai báo các biến có kiểu dùng tạm (Anonymous Types) sẽ được làm rõ trong phần kế tiếp.
Anonymous Types

C# 3.0 cho phép tạo ra các class dùng tạm thời mà không cần phải định nghĩa class đó trước. Ví dụ, mình cần 1 class tạm thời dùng để khai báo 1 mẫu xe có các thuộc tính là năm SX (manf), màu sắc (color) và số chỗ ngồi (seats) thì dưới đây là 1 ví dụ cho phép khai báo như vậy:

var mycar = new { manf = 2007, color = System.Drawing.Color.Black, seats = 4 };
mycar.manf = 2006;
mycar.color = System.Drawing.Color.White;

Trong dòng code trên, từ khoá new cho phép định nghĩa ra 3 thuộc tính là manf, color và seats. Khi compile, C# Compiler sẽ tạo ra 1 class tương tự vậy

class __Anonymous1 {

private int _manf = 2007;
private int _seats = 4;
private System.Drawing.Color _color = System.Drawing.Color.Black;
public string manf
{
get { return _manf; }
set { _manf = value; }
}

public string seats
{
get { return _seats; }
set { _seats = value; }
}

public int color
{
get { return _color; }
set { _color = value; }
}

}
Ngoài ra, nếu như có 1 khai báo khác tương tự để tạo 1 instances mới cho 1 class kiêủ như vậy, thì C# Compiler cũng đủ thông minh để tạo ra 1 class cho >2 khai báo như vậy, và thậm chí, trong code, có thể gán giá trị cho nhau (vì chung 1 type)
Extension Methods

Extension Method là 1 tính năng rất hay trong C# 3.0 này.

Giả sử bạn có được 1 class từ đâu đó, tuy nhiên, bạn không có code mà chỉ có file dll mà thôi. Bạn muốn mở rộng và thêm 1 số hàm của class MyPoint chẳng hạn, thì thông thường bạn sẽ làm cách nào? Inherit , viết Wrapper ...?

Với Extension methods, thì C# cho phép lập trình viên viết các method cho các lớp khác một cách khá dễ dàng. Hãy xem ví dụ sau, class MyPoint là 1 class không có sẵn source, và mình muốn thêm 1 hàm để tính Distance giữa 2 điểm (2 object MyPoint) là từ chính MyPoint đang xét tới 1 MyPoint khác.

Rất đơn giản, khai báo 1 class khác và khai báo thêm hàm cần thêm vào, lưu ý từ khoá this trong Parameter đầu tiên

public static class MyExtend{

public static int DistanceTo(this MyPoint mp, MyPoint another){

//Process the method here
return (mp.x + mp.y) - (another.x + another.y);

}

}

Và trong lớp cần sử dụng... (trong ví dụ sau có dùng 1 tính năng mới của C# 3.0 là Object, Collection Initilizers sẽ được bàn sau)

List allPoints = new List{

new MyPoint{x = 1, y=2},
new MyPoint{x = 3, y=4},
new MyPoint{x = 4, y=2},
new MyPoint{x = 2, y=5}
};

int result = allPoints[1].DistanceTo(allPoints[2]);

Từ ví dụ trên, có thể thấy hàm DistanceTo được xem như 1 hàm của lớp MyPoint.

Khi sử dụng tính năng này, cần lưu ý:

1. Hàm cần thêm vào phải là hàm static
2. Hàm cần thêm vào phải đặt vào trong 1 class static
3. Kiểu dữ liệu của tham số đầu tiên sẽ tương ứng với kiểu dữ liệu cần thêm vào.
4. Từ khoá this phải đặt ở tham số đầu tiên
5. Để sử dụng, thì class dùng để khai báo phải visible trong context cần sử dụng (dùng using...)

Object and Collection Initializers

C# 3.0 cũng cho phép khởi tạo các đối tượng và khai báo chỉ trong cùng 1 câu lệnh, và class cũng không cần phải có các constructor, hãy xem ví dụ khai báo Class MyPoint dưới đây:

public class MyPoint{

int _x = 0;
public int x {

get { return _x; }
set { _x = value; }

}

int _y = 0;
public int y{

get { return _y; }
set { _y = value; }

}

}

Lưu ý là ở đây, Minh không khai báo bất kỳ Constructor nào. Hãy xem ví dụ ở trên về Extension Method, C# 3.0 cho phép viết khởi tạo các class như đã được nêu ở bên trên

Đơn giản hơn:

MyPoint p = new MyPoint() { x = 1, y = 4 };

Cũng đã có câu hỏi rằng tại sao không gọi vậy cho lẹ:

MyPoint p = new MyPoint(1,4);

Do ở đây không sử dụng Constructor, nên cách gọi này là gọi 1 constructor với 2 tham số 1 và 2...
Auto-Implemented Properties

Trong lúc lập trình, việc khai báo các Properties cho 1 class là chuyện làm rất thường xuyên, như ví dụ ở trên, class MyPoint được khai báo các properties như sau

public class MyPoint{

int _x = 0;
public int x {

get { return _x; }
set { _x = value; }

}

int _y = 0;
public int y{

get { return _y; }
set { _y = value; }

}

}

Đa số các Properties đều thực hiện các thao tác như là ghi và đọc từ 1 biến private nào đó. Trong C# 3.0, nếu như properties của bạn chỉ thực hiện các thao tác như ở trên (đọc và ghi vào biến private) thì không cần phải khai báo như vậy, mà thay vào đó chỉ cần khai báo:

public class MyPoint{

public int x {get ; set;}

public int y {get ; set; }

}
Khi biên dịch, thì C# Compiler sẽ tự động tạo ra các biến private tương tự y như khai báo tường minh ở khai báo trên. Kết quả là 2 khai báo hoàn toàn tương tự nhau.


3 tính năng còn lại bữa sau viết tiếp vô đây, hết giờ ... làm rồi

Tags: dotnet, techniques, csharp, what_new
Wednesday September 19, 2007 - 04:25pm (ICT) Permanent Link | 2 Comments
Chạy theo .NET???? (Phần 2)
Chạy theo .NET???? (Phần 2) magnify

Trong Entry trước đã có nói về các Foundation của .NET 3.0 trở lên là WPF - Windows Presentation Foundation, WCF - Windows Communication Foundation, WF - Windows Workflow Foundation , Windows CardSpace.

Entry này sẽ đi sâu hơn những khác biệt trong các .NET framework, từ 1.x, 2.0, 3.0 và 3.5 (beta)

ADO.NET vNext= LINQ
.NET Compact Framework 1.0 .NET Compact Framework 2.0

* .NET Compact Framework 2.0
* .NET Micro Framework 2.0



* .NET Compact Framework 3.5
* Silverlight 1.1
* .NET Micro Framework 2.0

Từ bảng trên, có thể thấy một số điểm sau đây:

* Từ 1.x lên 2.0 đã có sự thay đổi cơ bản về cấu trúc bên dưới của .NET framework (CLR 1.0 --> CLR 2.0)
* Từ 2.0 lên 3.0 về cơ bản thì không có gì thay đổi nhiều trừ các Foundation được đưa vào. Như vậy, những ứng dụng đã được viết để chạy trên .NET 2.0 hoàn toàn có thể chạy được trên nền 3.0. Từ đó, nếu bạn đang sử dụng .NET 1.x, và bạn muốn nâng cấp lên, tốt nhất là nâng cấp lên 3.0 luôn mà ko cần phải cài .NET 2.0. Ngay cả trong lập trình C#, thì C# vẫn là 2.0. Do đó, nếu đang cài .NET 2.0, và bạn chưa cần phải sử dụng các Foundation mới, thì việc cài đặt 3.0 là không cần thiết.
Như vậy, nếu để đưa ra một lời khuyên cho bạn nên cài .NET framework nào, thì .NET 3.0 vẫn được prefer hơn cả vì nó bao gồm cả 2.0 và .NET 3.5 thì chưa được ổn định (beta)
* .NET 3.0 có chứa 1 compiler mới để xử lý các new features và để xử lý LINQ

Các tính năng mới trong .NET 3.5
Thực thi bên dưới Framework nhanh hơn

1. Bộ thu dọn "Rác" trong bộ nhớ làm việc nhanh hơn
2. Quá trình NGen nhanh hơn, "thông minh" hơn đồng thời cũng yêu cầu ít bộ nhớ hơn.
3. Cải thiện các tính năng hỗ trợ CPU 64 bit
4. Nâng cao performance cuả ThreadPool
5. Security check caching trong quá trình thực thi NGen.

Base Class Library – Các class mới

1. Thêm các kiểu dữ liệu mới: BigInteger, HashSet và DateTime2
2. NSA Suite ”B” and FIPs compliant cryptography
3. Lightweight Reader/Writer Lock Classes
4. Anonymous and Named Pipes IO Classes
5. Tích hợp với Event Tracing for Windows
6. New Addin hosting model for extensibility

Language Integrated Query (LINQ)

1. Đưa LINQ vào ngôn ngữ lập trình (C# và VB .NET) và cả trong framework

Workflow Enabled Services – Process and Messaging together

1. Sử dụng Workflow để tạo ra các Services có khả năng chạy trong thời gian lâu dài và ổn định. Công cụ mới là WF activities and new programming model classes cũng đã được thêm vào để đơn giản hoá quá trình tạo ra các Service có Workflow được ứng dụng sử dụng WF và WCF. Tính năng này cho phép các lập trình viên có thể phát triển các business logic cho các Service sử dụng WF đồng thời gởi message từ Service đó thông qua WCF, điều này cho phép đưa ra những chiến lược tốt hơn và giảm thời gian viết code so với trước đó.

Web 2.0 Friendly and AJAX Enabled WCF Services
Visual Studio Developer Tools for WF, WCF and in Visual Studio “Orcas”
More WS-* Standards Support

1. Implementation in WCF of the latest OASIS specifications Web Services Atomic Transaction (WS-AtomicTransaction) 1.1, WS-ReliableMessaging 1.1, WS-SecureCOnversation and Web Services Coordination (WS-Coordination) 1.1.

RSS and ATOM Syndication API

1. Trong .NET 3.5 hỗ trợ các tính năng có thể sinh ra các RSS và ATOM Syndication một cách dễ dàng hơn.

Partial Trust Support for WCF Hosting

1. Partial trust on the client is provided for ASMX parity focussing mainly on partially trusted WCF applications deployed through click-once. Support is provided for basic HTTP binding provided that the application runs in the Internet zone permissions and have granted the apropriate WebPermission. Secure communication is possible through transport security only. All other features are not available to partially trusted applications including hosting services, duplex communications, non-HTTP transports, WS-* protocols and any WF use.

Rules Data Improvements

1. The rules engine in WF is improved to add support for C# 3.0 extension methods, and for operator overloading . Also the ”new” operator is added to compete the base set of expression types.

Built-in WPF tools for Visual Studio “Orcas”

1. The Visual Studio designer for WPF was previously released as a CTP. It is not integrated into the development environment and is significantly improved.

Additional WPF Features and Improved Performance

1. WPF has smoother animations, faster startup and better overall performance. There are also new data types available for data binding with LINQ. Better integration support is now provided for with codename “WPF/E”.

Trên đây là 1 số tính năng mới bên trong .NET framework 3.5 và Visual Studio .NET Orcas (beta).

Chú thích NGen:

NGen là từ viết tắt của Native Image Generator. Đây là 1 công cụ được đưa vào từ .NET 1.x dùng để tạo 1 "native image" cho 1 assembly. Trong .NET, CLR thực thi các mã IL là mã trung gian của .NET (không phải mã nhị phân). Để máy có thể hiểu được, thì nó cần phải được dịch sang mã máy (mã nhị phân). CLR Excution Engine có chứa 1 bộ biên dịch gọi là JIT (Just In Time) để mà chuyển mã IL này sang mã dạng nhị phân (native code) trong lúc chạy. Tuy nhiên, mã nhị phân này sẽ không được lưu lại trên các thiết bị lưu trữ permanant mà nó chỉ được lưu trên bộ nhớ trong Process lifetime. Khi application được restart thì bộ biên dịch JIT phải compile lại các mã IL sang mã native lại.

Bộ NGen này cho phép tạo 1 cái "Native Image" (như là chụp lại cái đoạn mã máy đã được biên dịch) và lưu trên thiết bị lưu trữ trong 1 vùng đặc biệt gọi là NGen Cache - Native Image Cache.

Qua các .NET framework khác nhau, bộ NGen này được tối ưu hóa nhiều hơn với những giải thuật tốt hơn. Nếu gõ vào Start > Run... %windir%\assembly thì có thể thấy thư mục NGen Cache này

Phần tiếp theo: Những cái mới trong C# 3.0
Tags: dotnet, techniques
Wednesday September 19, 2007 - 11:25am (ICT) Permanent Link | 1 Comment
Chạy theo .NET????
Chạy theo .NET???? magnify

Hic, mấy tuần này đang rảnh, nên ngồi đọc tài liệu, cập nhật kiến thức. Mình đã quyết định đi theo hướng công nghệ của MS, chạy theo nó đúng là hụt hơi luôn. .NET 3.0 vừa mới ra, chưa kịp xài, đã ra .NET 3.5, VSS Orcas và SilverLight… hic hic hic

Tranh thủ viết blog, chia sẻ với bạn bè chút kiến thức mới sưu tầm được.

Bài viết này trước hết giải thích một số nền tảng mới trong .NET 3.0.

Trước hết, thì theo đọc qua taì liệu 1 cách sơ bộ thì có thể đưa ra công thức cho .NET 3.0 là

.NET 3.0 = .NET 2.0 + WPF + WCS + WF + WPF

Riêng đối với .NET 3.5 ngoài WPF + WCS + WF + WPF, phần core sử dụng CLR 3.5 còn có thêm 1 cái khá hay đó là LINQ và ASP .NET AJAX được tích hợp các tính năng MS ASP .NET AJAX 1.0 (trước đó code name là Atlas) từ thời .NET 2.0 và khá nhiều tính năng mới được thêm vào. Có thể nói từ .NET 3.0 sang .NET 3.5 là cả 1 break change.

Okie okie, đi chi tiết cho .NET 3.0 trước, thì dưới đây là 1 số tính năng mới của nó

* WPF - Windows Presentation Foundation
* WCF - Windows Communication Foundation
* WF - Windows Workflow Foundation
* Windows CardSpace
* CNG - Cryptography API: Next Generation
* UAC - User Application Control

Chúng ta sẽ đi sâu hơn chi tiết từng đề mục:
WPF: Windows Presentation Foundation

WPF là một bộ các controls trong đó có khá nhiều cái là trùng lặp với các control đơn giản trong Windows Form. Tuy nhiên, các control mới hỗ trợ các tính năng về đồ họa và các tương tác một cách có ấn tượng hơn.

Ví dụ như các đối tượng có thể chứa các đối tượng khác như là 1 nội dung của nó. 1 cái button có thể chứa cả 1 cái Grid, trong grid có thể chứa nhiều label, textbox, hình hay bất kỳ các control nào khác. Đó là đặc điểm khá tuyệt vời của WPF, nó cho phép các control thể hiện ở rất nhiều dạng khác nhau cùng với các tương tác khác nhau, hơn là chỉ 1 đòng text.

Các control trong WPF cũng có thể được tô gradient phần background. Lập trình viên có thể định ra cacs style và template sẵn để cho toàn bộ form có 1 cái GUI thống nhất. Nếu như mà mình cần thay đổi 1 cái button từ kiểu linear gradient (đổ màu tuyến tính) từ màu đỏ sang vàng thành radial gradient (đổ màu tuyến tính theo tâm vòng tròn) từ màu trắng sang đen, mình có thể làm rất dễ dàng trong vài giây và chỉ tại 1 chỗ.

Một đặc điểm nữa của các Control trong WPF đó là nó có thể chứa các trigger, và thực hiện số số action nào đó khi nhận được “kích thích” . Ví dụ 1 cái button có thể thay đổi màu sắc, làm bự ra, thu nhỏ lại, hay quay vòng vòng hay nói chung, các hiệu ứng mà con chuột được đưa vào hoặc là đc nhấn lên đó.

Một trong những khái niệm quan trọng nhất trong WPF là sự tách biệt giữa giao diện và code của nó. Từ ý tưởng là người thiết kế giau diện có thể dùng 1 cái tool như là Expression để xây dựng giao diện, và developer sẽ xử lý code cho các đối tượng đó. Hiện tại, thì chương trình Expression ko có đc miễn phí mà nó phải đc trả tiền để có nó, và MS cũng chưa có cho biết là Expression có được có trong Visual Studio hay ko? Hiện tại, với cái giao diện design WPF trong Visual Studio thì nó ko có làm việc tốt lắm với các control của WPF. Hi vọng sẽ có những caỉ tiến hơn trong các phiên bản sau.

Trong WPF cũng có đưa them 1 khái niệm là XAML XAML (Extensible Application Markup Language — đọc là "zammel"), đây là ngôn ngữ mà sẽ định dạng câú trúc cuả WPF và cũng là ngôn ngữ chính để tạo ra SilverLight object.

Tuy nhiên, các WPF control lại ko muốn “làm việc” chung với các Windows Form control. Mình có thể dùng 1 control của Win Form để chứa WPF control và ngược lại, tuy nhiên, nó ko có dễ dàng như việc kéo thả những control thông thường trong 1 cái form.

Ví dụ, control trong Win Form sử dụng Text để define cái text của control đó như Button hay Textbox chẳng hạn. Ngược lại, các WPF control sử dụng Content để define cái mà bên trong nó chứa, có thể là 1 control khác hoặc 1 đoạn text chẳng hạn. Ngoại trừ Window object (là 1 phiên bản trong WPF của Form) thì cần có cả Content chứa các controls khác và cả Title để định nghĩa cái tiêu đề form.

Windows Control cũng sử dụng FontFamily, FontSize, FontStretch, FontStyle, và FontWeight một cách riêng biệt để hiển thị text, nhưng với Control WPF thì sử dụng thuộc tính Font duy nhất với các thuộc tính riêng. Điều này cũng đồng nghĩa là Visual Studio's WPF form designer sẽ ko có tạo ra những dialog có những font đẹp hơn theo ý mình.
WCF - Windows Communication Foundation

Windows Communication Foundation là 1 bộ các công cụ để thực hiện theo Dịch vụ hướng Kiến Trúc (SOA). Nó bao gồm các lớp và method để xây dựng các lớp (class) client và server. Những tool này giúp lập trình viên xây dựng cacs Class một cách rất dễ dàng.

WCF cũng đồng thời cho phép client và server giao tiếp với nhau bằng rất nhiều cách như TCP, HTTP, name pipes hay Message Queue. WCF cũng đồng thời bao gồm các tính năng để cung cấp các giải pháp giao tiếp an toàn giữa client và server.

Tuy nhiên, 1 điểm bất lợi là WCF cung cấp tất cả những option này thì đồng thời, nó cũng yêu cầu cấu hình, cấu hình cho WCF là 1 vấn đề. VSS cũng ko cung cấp nhiều thông tin hướng dẫn làm sao để build một file cấu hình bằng file XML để câú hình client và server ở mức độ mong muôns. Vì vậy, vấn đề cấu hình cho WCF là một khó khăn mà cần cân nhắc khi sử dụng WCF.
WF - Windows Workflow Foundation

Hì, trước tiên, chúng ta cần giải thích vì sao MS lại sử dụng WF cho cụm từ Windows Workflow Foundation mà đúng ra là fải sử dụng WFF? Thực ra, WFF là tên viết tắt của tổ chức World Wrestling Federation, nên MS cũng ko muốn bị kiện tụng gì nữa, cuối cùng đã sử dụng là WF vừa ngắn gọn hơn. Vì vậy, ở 1 số tài liệu, thuật ngữ này được sử dụng là Windows Workflow hoặc Workflow.

WF cung cấp 1 nhóm các lớp (class) mà mô hình hoá từng khía cạnh của 1 workflow của 1 ứng dụng. Những lớp khác nhau đại diện cho những action ví dụ như đợi 1 sự kiện, thực thi theo điều kiện (branching), đợi 1 trong nhiều sự việc xảy ra và thực thi các lệnh tuần tự.

Sau khi thiết kế workflow, 1 workflow engine sẽ di chuyển các đối tượng qua rất nhiều trạng thái trong workflow cho tới khi chúng đến được điều kiện kết thúc.
Windows CardSpace

WC - CardSpace (hay InfoCard) là 1 công cụ để xây dựng hệ thống nhận dạng dựa vào CardSpace đã trên máy client. Ví dụ trên Ebay xây dựng 1 hệ thống cho phép xác định tính đáng tin cậy cuả ngươì mua và người bán. Tương tự, người dùng CardSpace cũng sẽ tạo ra các identities mà người khác ko thể làm giả khi thao tác với chương trình ứng dụng.
CNG - Cryptography API: Next Generation

CNG được MS hứa hẹn là 1 mô hình mới, đơn giả trong việc mã hoá. Nó bao gồm nhiều cách để "plug and play" với các component khác nhau ví dụ như bộ sinh số ngẫu nhiên (có thể được customize). Nó cũng bao gồm các giải thuật mã hoá mới trên thế giới.

Tuy nhiên, có vẻ như không phải là ý tưởng hay khi dùng CNG với managed code. Thư viện CNG ko fải là thư viện .NET (.NET assembly), nó cũng không phải là COM, cũng không phải là 1 kiểu thư viện như *.tbl file
UAC - User Application Control

Nếu ai đã từng sử dụng Vista thì sẽ thấy 1 điều rằng, dù đang login vào quyền Administrator, thì khi cài đặt phần mềm hoặc tương tác 1 số phần hệ thống, thì 1 hộp thoại sẽ hiển thị, và đôi khi, hiển thị cả ô nhập password của admin. Cách làm này nhằm bảo vệ hệ thống được phép truy xuất bởi những người được phép.

UAC hướng tới việc điều khiển các quyền sử dụng của người dùng đối với application, vì vậy, khi lập trình, thì cần lưu ý đến mức độ ảnh hưởng và cần dùng quyền càng ít càng tốt.

Trong .NET, thì các quyền như ghi, sửa, xoá Registry được phép thực thi mà không cần quyền Admin.

Phù, mệt quá, entry sau viết tiếp: So sánh giữa .NET 1.0, 2.0, 3.0 và 3.5

Một số tài liệu tham khảo

* WPF - Windows Presentation Foundation
* WCF - Windows Communication Foundation
* WF - Windows Workflow Foundation
* Windows CardSpace
* CNG - Cryptography API: Next Generation
* UAC Described
* UAC article: "Teach Your Apps To Play Nicely With Windows Vista User Account Control"
* UAC article: Developer Best Practices and Guidelines for Applications in a Least Privileged Environment

Tags: techniques, theory, dotnet
Tuesday September 18, 2007 - 02:38pm (ICT) Permanent Link | 2 Comments
Abstract class vs Interface
Abstract class vs Interface magnify
Hum ni ngồi rảnh rảnh đọc cái Design Pattern , lâu lắm rồi ko đọc nó. Thì thấy ra mình còn đang confused giữa Abstract Class và Interface .

Sau 1 hồi tìm hiểu trên net thì túm lại là tìm ra được 1 số câu hỏi và câu trả lời sau:

1. Sự Giống và khác nhau giữa Interface và Abstract Class
2. Khi nào thì dùng Abstract Class
3. Khi nào thì dùng interface

Dươí đây là 1 đống tài liệu sau khi tổng kết, làm biếng dịch sang tiếng Việt (Và cũng không biết để sao) nên để vậy lun
What is an Abstract class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body . As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments . The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class . Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

Both together

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract . If all the methods of an abstract class are uncompleted then it is the same as an interface but with the restriction that it cannot make a class inherit from it. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

There are some similarities and differences between an interface and an abstract class that I have arranged in a table for easier comparison:

Feature


Interface


Abstract class

Multiple inheritance


A class may inherit several interfaces.


A class may inherit only one abstract class.

Default implementation


An interface cannot provide any code, just the signature.


An abstract class can provide complete, default code and/or just the details that have to be overridden.

Constants


Static final constants only, can use them without qualification in classes that implement the interface . On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional.


Both instance and static constants are possible.
Third party convenience An interface implementation may be added to any existing third party class. A third party class must be rewritten to extend only from the abstract class.
is-a vs -able or can-do Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface , which could apply to many otherwise totally unrelated objects. An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Damamation descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is.

In a Java context, users should typically implement the Runnable interface rather than extending Thread , because they're not really interested in providing some new Thread functionality, they normally just want some code to have the capability of running independently. They want to create something that can be run in a thread, not a new kind of thread.The similar is-a vs has-a debate comes up when you decide to inherit or delegate.
Plug-in You can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When you implement the inteface, you start from scratch without any default implementation. You have to obtain your tools from other classes; nothing comes with the interface other than a few constants. This gives you freedom to implement a radically different internal design. You must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on you. Depending on the cleverness of the author of the abstract class, this may be good or bad.

Core VS Peripheral


Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.


An abstract class defines the core identity of a class and there it is used for objects of the same type.
Maintenance If your client code talks only in terms of an interface , you can easily change the concrete implementation behind it, using a factory method . Just like an interface , if your client code talks only in terms of an abstract class, you can easily change the concrete implementation behind it, using a factory method .

Homogeneity


If the various implementations only share method signatures then it is better to use Interface.


If the various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed


Requires more time to find the actual method in the corresponding classes.


Fast
Terseness The constant declarations in an interface are all presumed public static final , so you may leave that part out. You can't call any methods to compute the initial values of your constants. You need not declare individual methods of an interface abstract . They are all presumed so. You can put shared code into an abstract class, where you cannot into an interface . If interfaces want to share code, you will have to write other bubblegum to arrange that. You may use methods to compute the initial values of your constants and variables, both instance and static. You must declare all the individual methods of an abstract class abstract .

Class vs. interface

Some say you should define all classes in terms of interfaces, but I think recommendation seems a bit extreme. I use interfaces when I see that something in my design will change frequently .

For example , the Strategy pattern lets you swap new algorithms and processes into your program without altering the objects that use them. A media player might know how to play CDs, MP3s, and wav files. Of course, you don't want to hardcode those playback algorithms into the player; that will make it difficult to add a new format like AVI. Furthermore, your code will be littered with useless case statements. And to add insult to injury, you will need to update those case statements each time you add a new algorithm. All in all, this is not a very object-oriented way to program.

With the Strategy pattern, you can simply encapsulate the algorithm behind an object. If you do that, you can provide new media plug-ins at any time. Let's call the plug-in class MediaStrategy . That object would have one method: playStream(Stream s) . So to add a new algorithm, we simply extend our algorithm class. Now, when the program encounters the new media type, it simply delegates the playing of the stream to our media strategy. Of course, you'll need some plumbing to properly instantiate the algorithm strategies you will need.

This is an excellent place to use an interface. We've used the Strategy pattern, which clearly indicates a place in the design that will change. Thus, you should define the strategy as an interface. You should generally favor interfaces over inheritance when you want an object to have a certain type; in this case, MediaStrategy . Relying on inheritance for type identity is dangerous; it locks you into a particular inheritance hierarchy. Java doesn't allow multiple inheritance, so you can't extend something that gives you a useful implementation or more type identity.
Interface vs. abstract class

Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks.
Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn't know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, Java calls the method defined by the child class. Many developers forget that a class that defines an abstract method can call that method as well. Abstract classes are an excellent way to create planned inheritance hierarchies. They're also a good choice for nonleaf classes in class hierarchies.

When To Use Interfaces
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface . To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.
When To Use Abstract classes
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.
When to Use Both
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

Không có nhận xét nào: