Why Java Sucks and C # Rocks (3): attribute and annotation

Source: Internet
Author: User
Tags abstract xunit

In the previous article I talked about the different attitudes of the Java and C # languages to the underlying type, and I think C # is more convenient to use as the object of the basic type than Java. In addition, C # has a feature that does not exist in Java 1.4, that is, attribute (custom attribute), and in the following Java 5.0 also added a similar function, this is annotation (callout). So what is the role of attribute, what is the difference between annotation in Java and attribute in C #, and what are the advantages of Java 5.0 that are absorbed from C # 1.0? We are now going to focus on this.

Custom Features and design

attribute is an important function in C # 1.0 that is used to attach some metadata to a member, such as a class, method, or parameter, while in a program you can get the data by reflection. For example, in the. NET Framework, each type cannot be serialized by default unless we add a serializable tag to the type. As follows:

[Serializable]
public class Product { ... }

After the product class is marked serializable, it can be serialized or deserialized by tool classes such as Binaryserializer or DataContractSerializer. C # has a convention: all attributes (directly or indirectly) inherit the System.Attribute class, and the class name ends with attribute, but can be omitted when used. So, in fact, the serializable tag is actually a SerializableAttribute class, which is a subclass of System.Attribute.

attribute in C # plays a very important role in software design, as Kent Beck evaluates:

NUnit 2.0 is a excellent example of idiomatic design. Most folks who port XUnit just transliterate the Smalltalk or Java version. That's what we did with NUnit at a, too. This new version are NUnit as it would have been done had it been do in C # to begin with.

In short, most xunit frameworks are simply porting JUnit code, but NUnit uses C # 's attributes to provide a more elegant design, and a similar view is more specifically discussed in the magazine Martin Fowler. As a result, C # is significantly ahead of Java 1.4 in this regard. Fortunately, two years after the release of C #, the Java language also launched the 5.0 version, adding annotation functionality, which undoubtedly reduced the gap with C #.

Unfortunately, the annotation feature in the Java language, I think, has at least two drawbacks relative to the C # language attribute function.

Disadvantage 1: Blood loss model

Speaking of the attribute of C # and the annotation of Java, the biggest difference is that the attribute in C # is the class, and the annotation in Java is the interface.

Because the attribute of C # is also the standard "class" in. NET, it can be used in design methods related to classes, such as abstract classes, abstract methods, overloaded methods, interfaces, and so on. This type of feature creates some very common design patterns, such as probably for most. NET programmers are very familiar with the "verification tag."

Simply put, this is a way of representing "validation logic" by means of markup, for example, we can define a base class first:

public class ValidationResult { ... }

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public abstract class ValidationAttribute : Attribute
{
   public abstract ValidationResult Validate(object value);
}

The Validationattribute class inherits the System.Attribute, that is, it can serve as the base class for other attributes. For example, we can define some common validation classes:

public class RangeAttribute : ValidationAttribute
{
   public int Min { get; set; }

   public int Max { get; set; }

   public override ValidationResult Validate(object value) { ... }
}

public class RegexAttribute : ValidationAttribute
{
   public string Pattern { get; set; }

   public override ValidationResult Validate(object value) { ... }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.