Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter design patterns

Flutter design patterns

In this session, I will describe OOP design patterns, their implementation in Dart programming language and how to use them when developing Flutter applications.
This talk is heavily based on the “Gang of Four” (GoF) book “Design Patterns: Elements of Reusable Object-Oriented Software”, which, in my opinion, is one of the most iconic software engineering books about software design patterns. This book contains more than 20 different OOP design patterns.
We will look at:
1. Singleton
2. Adapter
3. Strategy
4. State
5. Facade
6. Prototype

Etornam Sunu Bright

June 25, 2022
Tweet

More Decks by Etornam Sunu Bright

Other Decks in Programming

Transcript

  1. “A design pattern names, abstracts, and identifies the key aspects

    of a common design structure that makes it useful for creating a reusable object-oriented design.” Gang of Four - (GoF) book
  2. Creational design patterns provide various object creation mechanisms, which increase

    flexibility and reuse of existing code. Creational 01 Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioural 02 Structural design patterns explain how to assemble objects and classes into larger structures, while keeping these structures flexible and efficient. Structural 03 Classification of Design Patterns
  3. A creational design pattern that ensures that a class has

    only one instance and also provides a global point of access to it. Singleton
  4. Adobe Stock#243026154 A constructor call must always return a new

    object by design. Ensure that a class has just a single instance Just like a global variable, the Singleton pattern lets you access some object from anywhere in the program. However, it also protects that instance from being overwritten by other code. Provide a global access point to that instance Problem
  5. Adobe Stock#243026154 Imagine that you created an object, but after

    a while decided to create a new one. Instead of receiving a fresh object, you’ll get the one you already created. One instance and one instance only Solution
  6. Adobe Stock#243026154 You can’t use the analytics library “as is”

    because it expects the data in a format that’s incompatible with your app. Problem
  7. Adobe Stock#243026154 You can create an adapter. This is a

    special object that converts the interface of one object so that another object can understand it. An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes. The wrapped object isn’t even aware of the adapter. Solution
  8. A behavioral design pattern that lets you define a family

    of algorithms, put each of them into a separate class, and make their objects interchangeable. Strategy
  9. Adobe Stock#243026154 Let’s say that your e-shop business offers several

    different shipping types for your customers: • Picking up the ordered items from a physical store (or any other physical place, e.g. a warehouse); • Sending order items using parcel terminal services; • Sending order items directly to your customers in the shortest delivery time possible — priority shipping. Problem
  10. Adobe Stock#243026154 The Strategy pattern suggests that you take a

    class that does something specific in a lot of different ways and extract all of these algorithms into separate classes called strategies. The original class, called context, must have a field for storing a reference to one of the strategies. The context delegates the work to a linked strategy object instead of executing it on its own. Solution
  11. A behavioral design pattern that lets an object alter its

    behavior when its internal state changes. It appears as if the object changed its class. State
  12. Adobe Stock#243026154 The main idea is that, at any given

    moment, there’s a finite number of states which a program can be in. Within any unique state, the program behaves differently, and the program can be switched from one state to another instantaneously. However, depending on a current state, the program may or may not switch to certain other states. These switching rules, called transitions, are also finite and predetermined. Problem
  13. Adobe Stock#243026154 The State pattern suggests that you create new

    classes for all possible states of an object and extract all state-specific behaviors into these classes. Instead of implementing all behaviors on its own, the original object, called context, stores a reference to one of the state objects that represents its current state, and delegates all the state-related work to that object. Solution
  14. A structural design pattern that provides a simplified interface to

    a library, a framework, or any other complex set of classes. Facade
  15. Adobe Stock#243026154 Imagine that you must make your code work

    with a broad set of objects that belong to a sophisticated library or framework. Ordinarily, you’d need to initialize all of those objects, keep track of dependencies, execute methods in the correct order, and so on. As a result, the business logic of your classes would become tightly coupled to the implementation details of 3rd-party classes, making it hard to comprehend and maintain. Problem
  16. Adobe Stock#243026154 A class that provides a simple interface to

    a complex subsystem which contains lots of moving parts. A facade might provide limited functionality in comparison to working with the subsystem directly. However, it includes only those features that clients really care about. Having a facade is handy when you need to integrate your app with a sophisticated library that has dozens of features, but you just need a tiny bit of its functionality. Solution
  17. A creational design pattern that lets you copy existing objects

    without making your code dependent on their classes. Prototype
  18. Adobe Stock#243026154 Say you have an object, and you want

    to create an exact copy of it. How would you do it? First, you have to create a new object of the same class. Then you have to go through all the fields of the original object and copy their values over to the new object. Nice! But there’s a catch. Not all objects can be copied that way because some of the object’s fields may be private and not visible from outside of the object itself. Problem
  19. Adobe Stock#243026154 The Prototype pattern delegates the cloning process to

    the actual objects that are being cloned. The pattern declares a common interface for all objects that support cloning. This interface lets you clone an object without coupling your code to the class of that object. Usually, such an interface contains just a single clone method. Solution