A Beginners Guide to Dart - Generics, Exceptions, Factory Constructors, and Enumerated Types - Part Seven
What Will I Learn?
- You will learn to use Generic Types
- You will learn about Exceptions and how to make custom Exceptions
- You will learn about factory constructors
- You will learn how to use Factory constructors to build singleton objects
- You will learn about Enumerated Types
OS Support for Dart:
- The Dart SDK
- A Dart supported text editor (Dart Pad can be used)
- A little time to sit and watch a video and some patience to learn the language
Resources for Dart:
- Dart Website: https://www.dartlang.org/
- Dart Official Documentation: https://www.dartlang.org/guides/language/language-tour
- Dart GitHub repository: https://github.com/dart-lang/sdk
- Awesome Dart GitHub Repository: https://github.com/yissachar/awesome-dart
- Pub website: https://pub.dartlang.org
Dart Logo (Google): https://www.dartlang.org/
In this Dart Tutorial Video, we look at Generic Types, Exceptions, Factory Constructors, Enumerated Types and Singleton Objects. While many of these ideas are more complex than any of the concepts that we've looked at before, they can be very powerful. Generic Types and Enumerated Types help build out the type system and Exceptions help us deal with different types of errors. Factory constructors are important because they allow us to build Single instance classes or Singleton Objects.
Generic and Enumerated Types
The Idea of Generic Types is an idea that has been around for awhile and it is usually found it functional statically typed languages. Generic Types allow the developer to build out Polymorphic objects, functions and variables which are targeted to specific patterns. This enables us to create functions and classes that can be reused with various different types and it cuts down on the boilerplate that needs to be built for an application. Enumerated Types on the other hand give us the ability to deal with binary types. We can again created targeted situations with Enumerated types that narrow down how our application state can be derived.
In the above image, we have a
Status enum type and a function that uses two generic types
Status enum type can be only one of three different states,
Status.paused. Each of these type has its own index representation as well with the first type being index 0 by default. The generic function
add takes in two types
V with both types extending the
num type. This means that these types can both be independent types that must also extend the
num type. This narrows down how the function must be used in our code and will also decrease how the virtual machine unpacks this code.
Factory Constructors and Singleton Objects
Factory Constructors are an idea that came from the Factory pattern. The main idea behind this pattern was to create a method that doesn't have to specify the exact object that will be created by the constructor. Factories can be used to create sub classes and singleton objects as a result and you can use them to defer the instantiation of a class. They were added to the Dart specification because the pattern was very common in other Object Oriented Languages prior to the creation of the language.
The class above features and example of a simple singleton object class. This class
Item contains a static field
item which holds a reference to the singleton object after it is instantiated. The Factory constructor for this class takes in the initial state and determines whether or not that
item field contains a object or not inside of it. If it does contain an object, then the factory constructor just returns that object with the new added state appended to it. If it doesn't contain an object then it creates a new
Item object using the
Item._internal() named constructor. In this way, we can call to this
Item constructor as many times as we want, but only have one single Item object in the entire program.
The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-6
- A Beginners Guide to Dart - Scope, Iterators, Functional Programming and Collections - Part Six
- A Beginners Guide to Dart - Inheritance, Abstract Classes, Interfaces, Mixins and Casting - Part Five
- A Beginners Guide to Dart - Methods, Final, Static, and Class Inheritance - Part Four
- A Beginners Guide to Dart - Intro to Classes and Objects - Part Three
- A Beginners Guide to Dart - Control Flow and Low Level Compilation - Part Two
- A Beginners Guide to Dart - Types, Functions, Variables and Objects - Part One