Explore the reasons behind Flutter's preference for `composition` instead of `inheritance`, and understand how this design choice benefits app developers in managing complex widgets.
---
This video is based on the question https://stackoverflow.com/q/62992063/ asked by the user 'Programmer_3' ( https://stackoverflow.com/u/9209825/ ) and on the answer https://stackoverflow.com/a/63602178/ provided by the user 'usman' ( https://stackoverflow.com/u/14171300/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why Flutter favour composition over inheritence?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Flutter's Preference for Composition over Inheritance
In the world of app development with Flutter, one question that frequently arises is: Why does Flutter favor composition over inheritance? This question digs deep into the design philosophy and architecture of Flutter and its widgets.
The Concept of Composition and Inheritance
Before delving into Flutter's choice, let's clarify the two concepts:
Inheritance: This is a way to form new classes using classes that have already been defined. When a class inherits from another, it gets access to all the methods and properties of the parent class.
Composition: This refers to building complex objects by combining simpler objects or components. In Flutter, it implies that a widget can contain other widgets (or properties) to build a more complex functionality.
Example of Composition in Flutter
To illustrate, consider the RaisedButton widget:
[[See Video to Reveal this Text or Code Snippet]]
In this example:
The RaisedButton widget is composed of a Text widget and an onPressed callback.
This composition allows the button to encapsulate its behavior while retaining flexibility with its child widget.
Why Composition is Preferred
1. Ease of Management
Using composition simplifies management. Each component can be modified independently without affecting the entire system. This is especially useful in UI design where changes happen frequently.
Better Scalability: Adding new properties or abilities to a button is easier with composition. You can add more children or combinations of widgets without rewriting the entire class.
2. Encapsulation of Behavior
Composition promotes a "has-a" relationship. For instance, a Person class might contain an Address class:
[[See Video to Reveal this Text or Code Snippet]]
In this scenario:
The Person class "has an" Address. This encapsulation allows the Address to be a highly customizable and reusable part, containing its properties like house number, city, country, etc.
If we were to put all this information directly into the Person class, the code would become unwieldy, harder to read, and difficult to manage.
3. Flexibility and Reusability
Since widgets in Flutter are composed, you can easily reuse them in different contexts:
Reusable Widgets: You can develop custom widgets that wrap standard ones, adding new functionality without disturbing the original widget's functionality.
Customizability: Different configurations can be achieved simply by changing the components that get passed to the parent widget.
4. Avoiding Complexity of Inheritance Hierarchies
Using inheritance can lead to deep class hierarchies, which can become cumbersome and difficult to manage. If you need to override behavior, it can lead to complicated structures:
Deep inheritance trees can make understanding code tough and increase risks of bugs.
Changes at one level can necessitate changes at other levels, making the code less stable and more fragile.
Conclusion
In summary, Flutter's preference for composition over inheritance is rooted in the desire for simplicity, scalability, and maintainability. By leveraging composition, developers can build robust applications that are easier to manage and evolve over time. So if you're working with Flutter, remember that composing your widgets effectively will allow you to take full advantage of what Flutter has to offer!
With these insights, you should be more equipped to reflect on your own use of Flutter and its design patterns in your projects.
Информация по комментариям в разработке