Extensions - Extensions let you add new functions and properties to an existing type without you having to create a whole new subtype.
Before getting deep into it lest understand some points about Design Patterns.
Object declarations provide a way of implementing the Singleton patterns as each declaration creates a single instance of that object.
Extensions may be used in place of the Decorator patterns as they allow you to extend the behaviour of classes and objects And if you are interested in using the Delegation patterns as an alternative to the inheritance.
For example:
// Defines a function named toDollar() which extends Double.
fun Double.toDollar() : String
{
return "$$this" // returns the current value prefixed with $.
}
No comments:
Post a Comment