Usage of Dart Constructor and Factory Concepts
Usage of Dart Constructor and Factory Concepts
What is a Dart Constructor?
One of the fundamental concepts when developing object-oriented applications with the Dart programming language is the constructor, in other words, constructor methods. When you create a new object from a class, the constructor acts as the first function called and arranges the initial values of the class. In Dart, to define a constructor, it is enough to create a method with the same name as the class, and you can optionally add parameters. This way, the initial data assignments of objects become easy.
Constructor Usage and Code Example
In Dart, you can define as many parameterized and non-parameterized constructors as you like. At the same time, you can perform advanced initialization using named constructors and initializer lists.
class Kullanici {
String ad;
int yas;
// Standard constructor
Kullanici(this.ad, this.yas);
// Named constructor
Kullanici.yeniKullanici() : ad = "", yas = 0;
}
void main() {
var kisi1 = Kullanici("Ayşe", 30);
var kisi2 = Kullanici.yeniKullanici();
print(kisi1.ad); // Ayşe
print(kisi2.yas); // 0
}
In this example, both a standard and a named constructor are shown for the Kullanici class. With the Dart constructor concept, you increase your flexibility in object creation.
Using Dart Factory Constructor
In some special cases, instead of producing a new object with every call, you may need to return an object from cache, return an object of a different type, or apply the singleton pattern. This is where the factory constructor comes into play. The factory constructor is a special structure that always returns an object but is not obliged to produce a new instance directly as a classical constructor does.
class Veritabani {
static final Veritabani _instance = Veritabani._icKonstruktor();
Veritabani._icKonstruktor();
factory Veritabani() {
return _instance;
}
}
void main() {
var db1 = Veritabani();
var db2 = Veritabani();
print(identical(db1, db2)); // true
}
In this example, the singleton pattern is applied to the Veritabani class using Dart factory constructor. This way, the same object is returned on every call.
Differences Between Dart Constructor and Factory
- The Constructor always creates a new object. The factory constructor, on the other hand, does not have to produce a new object; it can return an already existing one.
- With a factory constructor, you can manage instance caching, singleton patterns, or different types of objects.
- From an SEO perspective, correctly using the concepts of "dart constructor" and "dart factory" makes your code both readable and performant.
Conclusion: Usage of Dart Constructor and Factory
The Dart programming language offers various ways for developers to create objects through the use of constructors and factory constructors. Using both structures in the right scenarios will strengthen your application's architecture and increase the readability of your code. Understanding the fundamentals of Dart constructor concepts is important, especially for ease of maintenance and code quality in large projects.

Yorum Gönder