Reader small image

You're reading from  Hands-On Design Patterns and Best Practices with Julia

Product typeBook
Published inJan 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838648817
Edition1st Edition
Languages
Right arrow
Author (1)
Tom Kwong
Tom Kwong
author image
Tom Kwong

Tom Kwong, CFA, is an experienced software engineer with over 25 years of industry programming experience. He has spent the majority of his career in the financial services industry. His expertise includes software architecture, design, and the development of trading/risk systems. Since 2017, he has uncovered the Julia language and has worked on several open source packages, including SASLib.jl. He currently works at Western Asset Management Company, a prestige asset management company that specializes in fixed income investment services. He holds an MS degree in computer science from the University of California, Santa Barbara (from 1993), and he holds the Chartered Financial Analyst designation since 2009.
Read more about Tom Kwong

Right arrow

Reusability Patterns

In this chapter, we will learn about several patterns related to software reusability. As you may recall from Chapter 1Design Patterns and Related Principles, reusability is one of the four software quality objectives that is required for building large-scale applications. Nobody wants to reinvent the wheel. The ability to reuse an existing software component saves both time and energy—an overall humanity gain! The patterns in this chapter are proven techniques that can help us improve application design, reuse existing code, and reduce overall code size.

In this chapter, we will cover the following topics:

  • The delegation pattern
  • The holy traits pattern
  • The parametric type pattern

Technical requirements

The delegation pattern

Delegation is a pattern that is commonly applied in software engineering. The primary objective is to leverage the capabilities of an existing component by wrapping it via a has-a relationship.

The delegation pattern is widely adopted, even in the object-oriented programming community. In the early days of object-oriented programming, people thought that code reuse could be achieved beautifully using inheritance. However, people came to realize that this promise couldn't be completely fulfilled due to a variety of issues related to inheritance. Since then, many software engineers prefer composition over inheritance. The concept of composition is to wrap one object within another. In order to reuse existing functions, we must delegate functions calls to the wrapped object. This section will explain how delegation can be implemented in Julia.

The...

The holy traits pattern

The holy traits pattern has an interesting name. Some people also call it the Tim Holy Traits Trick (THTT). As you might have guessed the pattern is named after Tim Holy, who is a long-time contributor to the Julia language and ecosystem.

What are traits? In a nutshell, a trait corresponds to the behavior of an object. For example, birds and butterflies can fly, so they both have the CanFly trait. Dolphins and turtles can swim, so they both have the CanSwim trait. A duck can fly and swim, so it has both the CanFly and CanSwim traits. Traits are typically binary  you either exhibit the trait or not although that is not a mandatory requirement.

Why do we want traits? Traits can be used as a formal contract about how a data type can be used. For example, if an object has the CanFly trait, then we would be quite confident...

The parametric type pattern

Parametric type is a core language feature that's used to materialize data types with parameters. It is a very powerful technique because the same object structure can be reused for different data types in its fields. In this section, we will demonstrate how parametric types can be applied effectively.

When designing applications, we often create composite types to conveniently hold multiple field elements. In its simplest form, composite types only serve as the containers of fields. As we create more and more composite types, it may become clear that some of these types look almost the same. Furthermore, the functions that operate on these types may be very similar as well. We could end up with a lot of boilerplate code. Wouldn't it be cool to have a template that allows us to customize a general composite type for a specific use? ...

Summary

In this chapter, we have explored several patterns related to reusability. These patterns are highly valuable and can be utilized in many places within an application. In addition, people coming from an object-oriented background will probably find this chapter indispensable when it comes to designing Julia applications.

First, we went into great detail about the delegation pattern, which can be used to create new capabilities and lets us reuse functions from an existing object. The general technique involves defining a new data type that contains a parent object. Then, forwarding functions are defined so that we can reuse the functionalities of the parent object. We learned implementing delegation can be largely simplified by using @forward, which is provided by the Lazy.jl package.

Then, we examined the holy trait pattern, which is a formal way to define the behavior...

Questions

  1. How does the delegation pattern work?
  2. What is the purpose of traits?
  3. Are traits always binary?
  4. Can traits be used for objects from a different type hierarchy?
  5. What are the benefits of parametric types?
  6. How do we store the information of a parametric type?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Design Patterns and Best Practices with Julia
Published in: Jan 2020Publisher: PacktISBN-13: 9781838648817
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Tom Kwong

Tom Kwong, CFA, is an experienced software engineer with over 25 years of industry programming experience. He has spent the majority of his career in the financial services industry. His expertise includes software architecture, design, and the development of trading/risk systems. Since 2017, he has uncovered the Julia language and has worked on several open source packages, including SASLib.jl. He currently works at Western Asset Management Company, a prestige asset management company that specializes in fixed income investment services. He holds an MS degree in computer science from the University of California, Santa Barbara (from 1993), and he holds the Chartered Financial Analyst designation since 2009.
Read more about Tom Kwong