Thuta Learning
Dart
IntermediateProgrammingအခြေခံ

Mixins

Dartသင်ခန်းစာ 1735 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • mixin တစ်ခု ရေးပြီး with နဲ့ class တစ်ခုမှာ အများကြီး ပေါင်းထည့်နိုင်ဖို့
  • mixin ကို ဘာကြောင့် instance မဆောက်နိုင်ဘဲ constructor မကြေညာနိုင်လဲ၊ Dart 3 မှာ mixin class က ဘာကို ပြောင်းလဲလဲ ရှင်းပြနိုင်ဖို့
  • on နဲ့ superclass တစ်ခုကို တောင်းဆိုပြီး mixin ထဲကနေ super ကို ခေါ်နိုင်ဖို့
  • mixin နှစ်ခုက နာမည်တူ method ရှိရင် ဘယ်ဟာ run မလဲ (with list ထဲက နောက်ဆုံး mixin က နိုင်တယ်) ခန့်မှန်းနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Mixin ကို ရိုးရိုးလေး ပြောရရင်

Mixin ဆိုတာ field နဲ့ method အစုအဝေးတစ်ခုကို class တစ်ခုခုမှာ တပ်ဆင်လို့ရအောင် ထုတ်ပေးထားတာပါ။ power tool တစ်ခုမှာ attachment အမျိုးမျိုး တပ်သလိုပါပဲ: Logging attachment၊ Shareable attachment ကို BlogPost မှာလည်း တပ်လို့ရ၊ Video မှာလည်း တပ်လို့ရပါတယ်။ Class နှစ်ခုက မိဘ-သားစု ဆက်နွယ်မှု မရှိရင်တောင် ဘုံ behavior ကို မျှသုံးလို့ရပါတယ်။

Class တစ်ခုမှာ superclass တစ်ခုပဲ ရှိနိုင်ပေမဲ့ mixin ကတော့ လိုသလောက် ထပ်ထည့်လို့ရပါတယ်။ အဲဒါကြောင့် Dart မှာ 'ဘာလုပ်နိုင်တယ်' ဆိုတဲ့ capability တွေကို mixin နဲ့ ဖော်ပြပြီး 'ဘာတစ်ခုလဲ' ဆိုတာကိုတော့ extends နဲ့ ဖော်ပြလေ့ရှိပါတယ်။

Problem → Why → Solution

ပြဿနာ: မဆိုင်တဲ့ class နှစ်ခု၊ တူတဲ့ code နှစ်ခု

BlogPost နဲ့ Video နှစ်ခုလုံးမှာ log() နဲ့ share() လိုပါတယ်။ ဒါပေမဲ့ blog post က text content ဖြစ်ပြီး video က media ဖြစ်လို့ တူညီတဲ့ parent မရှိပါဘူး။ Single inheritance ကြောင့် 'Loggable ကို extends လုပ်ပြီး Shareable ကိုလည်း extends လုပ်' လို့ မရေးနိုင်ပါဘူး။ ဒါကြောင့် method တွေကို နှစ်နေရာ ကူးထည့်လိုက်ပါတယ်။

တစ်နေ့ share() ရဲ့ message format ပြောင်းရတဲ့အခါ တစ်နေရာကို မေ့သွားပါတယ်။ အခုတော့ post နဲ့ video က မတူတဲ့ format နဲ့ share နေပြီး ဘယ်သူမှ မသိပါဘူး။ တစ်နည်းဆို BaseContent class ကြီးတစ်ခုလုပ်ပြီး ဒီ method တွေအားလုံး ထည့်လိုက်ရင်တော့ အဲဒီ class က အကုန်လုံးကို သိတဲ့ god class ဖြစ်သွားပါတယ်။

pasted_methods.dart
class BlogPost {
  BlogPost(this.title);
  final String title;
  final List<String> _entries = [];

  void log(String message) => _entries.add(message);
  String share(String channel) => 'Sharing "$title" via $channel';
}

class Video {
  Video(this.title);
  final String title;
  final List<String> _entries = [];

  // The same two members, pasted again. Fix one and forget the other.
  void log(String message) => _entries.add(message);
  String share(String channel) => 'Sharing "$title" via $channel';
}

ဘာကြောင့် extends က မလုံလောက်လဲ

extends က 'ဘာတစ်ခုလဲ' ကို ပြောပြီး တစ်ခုပဲ ရွေးခွင့်ရှိပါတယ်။ ဒီမှာ လိုအပ်တာက 'ဘာလုပ်နိုင်လဲ' ဆိုတဲ့ capability နှစ်ခု၊ ဘယ် class မှာမဆို ထည့်လို့ရတာ ဖြစ်ရပါမယ်။ Mixin က ဒီလိုအတွက် ဒီဇိုင်းလုပ်ထားတာပါ: shared code ကို တစ်နေရာတည်းမှာ ရေးပြီး class မည်မျှမဆို တပ်လို့ရပြီး class တွေကြား 'is-a' လို့ မကြေညာပါဘူး။

Core concept: mixin, with, on နဲ့ order

mixin Logging { ... } က superclass မပါတဲ့ member အစုတစ်ခုကို ကြေညာတာပါ၊ class ဆီမှာ class BlogPost with Logging, Shareable { } လို့ ရေးပြီး တပ်ဆင်ပါတယ်။ Mixin ကို instance မဆောက်နိုင်ပါဘူး (M() ရေးရင် Couldn't find constructor 'M'. ဆိုပြီး error တက်ပါတယ်)၊ constructor လည်း ကြေညာလို့မရပါဘူး ('Mixins can't declare constructors.' လို့ ပြန်ထုတ်ကြည့်ထားပါတယ်)။ ဒါပေမဲ့ field (instance တစ်ခုချင်းစီမှာ သီးခြား copy ရှိတယ်)၊ method၊ getter၊ setter နဲ့ class က ဖြည့်ပေးရမယ့် abstract member တွေ ထည့်လို့ရပါတယ်။

Clause တွေရဲ့ အစီအစဉ်က extends၊ with၊ implements ဖြစ်ပါတယ်: class C extends Parent with M1, M2 implements Contract။ Mixin ရဲ့ type က C ရဲ့ supertype တစ်ခု ဖြစ်လာလို့ post is Shareable က true ပြီး List<Shareable> တစ်ခုမှာ BlogPost နဲ့ Video နှစ်မျိုးလုံး ထည့်လို့ရပါတယ်။

with M1, M2 က chain တစ်ခု ဆောက်ပါတယ်: အောက်ဆုံးမှာ Parent၊ အဲဒီပေါ်မှာ M1၊ အဲဒီပေါ်မှာ M2၊ အပေါ်ဆုံးမှာ သင့် class။ နာမည်တူ member ရှိရင် နောက်ကပါတဲ့ mixin က နိုင်ပါတယ် (M2 က M1 ကို၊ M1 က Parent ကို နိုင်တယ်)၊ M2 ထဲက super ဆိုတာ 'ကိုယ့်အောက်က chain' ဖြစ်လို့ M1 ကို ရည်ညွှန်းပါတယ်။ ဒါကို linearization လို့ ခေါ်ပါတယ်။

mixin Flyer on Animal လို့ ရေးရင် Flyer ကို Animal ဖြစ်တဲ့ superclass ပေါ်မှာပဲ တပ်လို့ရပါတယ်။ အဲဒီအစား Flyer ထဲမှာ Animal ရဲ့ member တွေကိုရော super.method() ကိုပါ သုံးခွင့်ရပါတယ်။ Dart 3 မှာတော့ ရိုးရိုး class ကို with နဲ့ မသုံးနိုင်တော့ပါဘူး၊ mixin ဒါမှမဟုတ် mixin class အဖြစ် ကြေညာထားမှ ရပါတယ်။

Keywordပေးတာကန့်သတ်ချက်
extendsMember၊ body နဲ့ constructor chainClass တစ်ခုပဲ
implementsMember စာရင်းပဲ၊ body မပါလိုသလောက် ရ
withMixin ရဲ့ member နဲ့ body၊ constructor မပါလိုသလောက်ရ၊ အစီအစဉ်က အရေးကြီး
Mixin တွေက parent ပေါ်မှာ အလွှာလိုက် ထပ်တယ်
  1. အောက်ဆုံးမှာ superclass ရှိ
  2. with တစ်ခုချင်းက အလွှာတစ်ခု ထပ်
  3. နောက်ဆုံး mixin က အပေါ်ဆုံး အလွှာ
  4. Call က အပေါ်ကစပြီး super နဲ့ ဆင်း

အလွှာနှစ်ခု နာမည်တူ method ကို ထိရင် with list ကို အစီအစဉ်ပြောင်းတာနဲ့ behavior ပြောင်းသွားနိုင်ပါတယ်။

အထွေထွေ ပုံစံ

dart
mixin Name {
  Type field = initialValue; // state: one copy per instance
  Type get required;         // abstract: the using class supplies it
  Result behaviour() => /* shared code */;
}
mixin Restricted on Parent {
  // may call Parent's members and super.method()
}
class Target extends Parent with Name, Restricted implements Contract {
  // clause order: extends, with, implements
}

Mixin က state၊ shared behavior နဲ့ class ဖြည့်ပေးရမယ့် requirement တွေကို ကိုင်ပါတယ်။ on က mixin ကို superclass တစ်မျိုးမျိုးပေါ်မှာပဲ တပ်ခွင့်ပေးပြီး with list ရဲ့ အစီအစဉ်က မည်သည့် override က နိုင်မလဲ ဆုံးဖြတ်ပါတယ်။

Worked example: BlogPost နဲ့ Video က logging နဲ့ sharing ကို မျှသုံးတယ်

Logging က state (log entry list) နဲ့ method ကို ပေးပြီး Shareable ကတော့ title ကို class ဆီက တောင်းပါတယ် (abstract getter)၊ ဒါကြောင့် share() ထဲမှာ title ကို သုံးလို့ရပါတယ်။ BlogPost နဲ့ Video နှစ်ခုက တစ်ခုနဲ့တစ်ခု မဆိုင်ပေမဲ့ နှစ်ခုလုံး with နဲ့ တပ်ထားပါတယ်။ နောက်ဆုံး line နှစ်ခုက post is Shareable က true ဖြစ်ကြောင်းနဲ့ log list တွေ သီးခြားစီ ရှိကြောင်း ပြပါတယ်။

mixins.dart
mixin Logging {
  final List<String> _entries = [];

  void log(String message) => _entries.add('[$runtimeType] $message');

  List<String> get history => List.unmodifiable(_entries);
}

mixin Shareable {
  // Requirement: the class that uses this mixin must provide a title.
  String get title;

  String share(String channel) => 'Sharing "$title" via $channel';
}

class BlogPost with Logging, Shareable {
  BlogPost(this.title);

  @override
  final String title;
}

class Video with Logging, Shareable {
  Video(this.title, this.seconds);

  @override
  final String title;
  final int seconds;
}

void main() {
  final post = BlogPost('Learn Dart');
  final clip = Video('Mixins in 5 minutes', 300);

  post.log('opened');
  print(post.share('email'));

  clip.log('played');
  clip.log('shared');
  print(clip.share('chat'));

  print(post.history);
  print(clip.history);
  print(post is Shareable);
  print('${post.history.length} vs ${clip.history.length}');
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. State in a mixinfinal List<String> _entries = [];

    Mixin ထဲက field က mixin ကို တပ်တဲ့ object တစ်ခုချင်းစီမှာ သီးခြား copy တစ်ခု ရပါတယ်။ BlogPost တစ်ခုရဲ့ log က Video ရဲ့ log နဲ့ မရောပါဘူး။

  2. Behaviour using that statevoid log(String message) => _entries.add('[$runtimeType] $message');

    log() က mixin ရဲ့ ကိုယ်ပိုင် field ကို သုံးပါတယ်။ runtimeType ကြောင့် entry တစ်ခုချင်းစီမှာ BlogPost ဒါမှမဟုတ် Video လို့ ဘယ် class ကနေ ရေးထားလဲ ပါလာပါတယ်။

  3. A requirementString get title;

    Body မပါတဲ့ getter က abstract ပါ။ Shareable ကို တပ်တဲ့ class တိုင်းက title ကို ကိုယ်တိုင် ပေးရမယ်၊ မပေးရင် compile မဖြစ်ပါဘူး။

  4. Applying mixinsclass BlogPost with Logging, Shareable {

    with နောက်က mixin နှစ်ခုက BlogPost ရဲ့ member တွေ ဖြစ်သွားပါတယ်။ BlogPost မှာ extends မရှိလို့ superclass က Object ပါ။

  5. Separate state per objectprint('${post.history.length} vs ${clip.history.length}');

    post က log() တစ်ကြိမ်၊ clip က နှစ်ကြိမ် ခေါ်ထားလို့ 1 vs 2 ကို print ထုတ်ပါတယ်။ Mixin ရဲ့ state က class အလိုက်ဖြစ်တဲ့ shared global မဟုတ်ပါဘူး။

You should see
Sharing "Learn Dart" via email
Sharing "Mixins in 5 minutes" via chat
[[BlogPost] opened]
[[Video] played, [Video] shared]
true
1 vs 2

Mixin order: နောက်ဆုံးက နိုင်တယ်

Mixin နှစ်ခုက နာမည်တူ method ကို override လုပ်ထားရင် with list ကို ဘယ်လိုစီထားလဲက အဖြေကို ပြောင်းပါတယ်။ အောက်က Base, A နဲ့ B မှာ who() ရှိပြီး A နဲ့ B က Base ကို 'on' လုပ်ထားလို့ ကိုယ့်အောက်က chain ကို super.who() နဲ့ ခေါ်နိုင်ပါတယ်။ C နဲ့ D ရဲ့ ကွာခြားချက်က with list ရဲ့ အစီအစဉ်တစ်ခုတည်းပါ။

linearization.dart
class Base {
  String who() => 'Base';
}

mixin A on Base {
  @override
  String who() => 'A>${super.who()}';
}

mixin B on Base {
  @override
  String who() => 'B>${super.who()}';
}

class C extends Base with A, B {}

class D extends Base with B, A {}

void main() {
  print(C().who()); // B>A>Base
  print(D().who()); // A>B>Base
}
text
class C extends Base with A, B
------------------------------
  C                  (no override of its own)
  ^
  Base+A+B           who() = 'B>' + super.who()   <- the call starts here
  ^
  Base+A             who() = 'A>' + super.who()
  ^
  Base               who() = 'Base'

  C().who()  ->  B>A>Base      (D, with B, A, gives A>B>Base)

Call က အပေါ်ဆုံးကစပါတယ်၊ ဒါကြောင့် နောက်ဆုံး mixin က ပထမဆုံး စကားပြောပြီး super နဲ့ အောက်ကို လက်ဆင့်ကမ်းပါတယ်။ ဒီအပေါ် အားကိုးရတဲ့ code မျိုးမှာ တစ်ယောက်ယောက်က with list ကို အစီအစဉ်ပြောင်းလိုက်ရင် စကားသံမကြားဘဲ behavior ပြောင်းသွားမှာမို့ အစီအစဉ်ပေါ် မူတည်တဲ့ mixin တွေကို comment နဲ့ မှတ်ထားပါ။

on နဲ့ mixin ကို ကန့်သတ်ခြင်း

Mixin က တစ်ခါတလေ သူ့ကို တပ်မယ့် class မှာ တစ်ခုခု ရှိနေတယ်လို့ ယူဆဖို့ လိုပါတယ်။ on ကို သုံးရင် compiler က အဲဒီ ယူဆချက်ကို စစ်ပေးပါတယ်။ Flyer ထဲမှာ Animal ရဲ့ eat() ကို ခေါ်လို့ရတာ on Animal ကြောင့်ပါ။

on_clause.dart
class Animal {
  void eat() => print('eating');
}

mixin Flyer on Animal {
  void fly() {
    eat(); // allowed: Flyer is declared on Animal
    print('flying');
  }
}

class Bird extends Animal with Flyer {}

void main() {
  Bird().fly(); // eating, then flying
}

class Rock with Flyer {} လို့ ရေးကြည့်ရင် compiler က "'Object' doesn't implement 'Animal' so it can't be used with 'Flyer'." လို့ ပြောပါတယ် (ပြန်ထုတ်ကြည့်ထားပါတယ်)။ ယူဆချက်က with ရဲ့ ဘယ်ဘက်မှာ ရှိတဲ့ superclass အတွက်ပါ၊ with ရဲ့ နောက်မှာ implements Animal ထည့်ရင်လည်း မပြေလည်ပါဘူး။ Rock ကို class Rock extends Animal with Flyer လို့ ပြင်ရပါမယ်။

mixin class နဲ့ Dart 3 ရဲ့ စည်းမျဉ်းတွေ

Dart 3 မတိုင်ခင်က class တိုင်းကို with နဲ့ သုံးလို့ရခဲ့ပေမဲ့ အခု မရတော့ပါဘူး။ 'with' နဲ့ သုံးမယ့် type ကို mixin နဲ့ ကြေညာရမယ်၊ ဒါမှမဟုတ် mixin class နဲ့ ကြေညာရပါမယ်။ mixin class က 'ရိုးရိုး class လည်း ဖြစ်၊ mixin လည်း ဖြစ်' တဲ့ တစ်ခုပါ။ extends လည်း သုံးလို့ရတယ်၊ with လည်း သုံးလို့ရတယ်၊ ကိုယ်တိုင်လည်း instance ဆောက်လို့ရတယ်။

mixin_class.dart
mixin class Counter {
  int count = 0;

  void increment() => count++;
}

class Clicks with Counter {}

class Votes extends Counter {}

void main() {
  final a = Clicks()..increment()..increment();
  final b = Clicks()..increment();
  final c = Counter()..increment();
  print('${a.count} ${b.count} ${Votes().count} ${c.count}');
}

ဒီ code က 2 1 0 1 ကို print ထုတ်ပါတယ်၊ object တစ်ခုချင်းစီမှာ ကိုယ်ပိုင် count ရှိလို့ပါ။ ရိုးရိုး class Animal {} ကို class Dog with Animal {} လို့ သုံးရင် "The class 'Animal' can't be used as a mixin because it isn't a mixin class nor a mixin." ဆိုတဲ့ error ပြန်ရပါတယ်၊ ဒါကို ဖြေရှင်းဖို့ Animal ကို mixin class ဒါမှမဟုတ် mixin အဖြစ် ပြင်ပါ။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ mixin ကို ဘယ်မှာ တွေ့ရလဲ

Flutter မှာ mixin တွေကို အမြဲလိုလို တွေ့ရပါတယ်: animation ပါတဲ့ State class တွေက SingleTickerProviderStateMixin ကို with နဲ့ တပ်ပြီး state ကို listener ပေးချင်ရင် with ChangeNotifier ရေးကြပါတယ်။ ဒီ mixin တွေရဲ့ ရည်ရွယ်ချက်က class တွေကို မျှသုံးလို့ရတဲ့ capability ပေးဖို့ပါပဲ။ အဲဒီ အခြေခံ ပုံစံက ကိုယ့် project ထဲမှာလည်း အတူတူ အသုံးဝင်ပါတယ်။

  • Cross-cutting concern တွေ: Logging, Analytics, Disposable (အလုပ်ပြီးရင် resource ပိတ်ရတဲ့ object) လို အလုပ်တွေက class များစွာမှာ လိုပေမဲ့ business logic နဲ့ မဆိုင်တာမို့ mixin က သင့်တော်ပါတယ်။
  • dart:collection ထဲက ListMixin, MapMixin လိုမျိုး: member အနည်းငယ်ကိုပဲ ကိုယ်တိုင်ရေးပြီး ကျန် method အားလုံးကို mixin ကို ယူသုံးပါတယ်၊ အောက်က ဥပမာမှာ ကြည့်ပါ။
  • Test helper တွေ: ပုံမှန် test class တိုင်းမှာ လိုတဲ့ setup/teardown helper ကို mixin တစ်ခုထဲ ထုတ်ပြီး test class တွေက with နဲ့ ယူသုံးတာပါ။
list_mixin.dart
import 'dart:collection';

// Four members written by hand; ListMixin supplies where, sort, reversed...
class Countdown with ListMixin<int> {
  Countdown(int from) : _items = [for (var i = from; i > 0; i--) i];

  final List<int> _items;

  @override
  int get length => _items.length;

  @override
  set length(int value) => _items.length = value;

  @override
  int operator [](int index) => _items[index];

  @override
  void operator []=(int index, int value) => _items[index] = value;
}

void main() {
  final c = Countdown(5);
  print(c); // [5, 4, 3, 2, 1]
  print(c.where((n) => n.isOdd).toList()); // [5, 3, 1]
  c.sort();
  print(c.reversed.take(2).toList()); // [5, 4]
}

အမှားများ

Mixin နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ

အရာအားလုံးကို mixin တစ်ခုထဲ ထည့်ခြင်း

mixin AppHelpers ထဲမှာ logging၊ date formatting၊ network call တွေ အားလုံးထည့်ထားရင် ဒါက god class ရဲ့ mixin version ပါပဲ။ တစ်ခုခုကို ပြင်ရင် mixin ကို သုံးတဲ့ class အားလုံး ထိခိုက်ပါတယ်။ Capability တစ်ခုချင်းစီ (Logging, Formatting) ကို သီးခြား mixin အဖြစ် ခွဲပါ။

with list ရဲ့ အစီအစဉ်ကို မရည်ရွယ်ဘဲ အားကိုးမိခြင်း

Mixin နှစ်ခုက နာမည်တူ method ကို ပိုင်ထားရင် with A, B နဲ့ with B, A က မတူတဲ့ ရလဒ် ပေးပါတယ် (အပေါ်က B>A>Base နဲ့ A>B>Base ဥပမာ)၊ compiler က ဘာမှ မသတိပေးပါဘူး။ နာမည်တူ တိုက်မှုကို ရှောင်ပါ၊ မရှောင်နိုင်ရင် on နဲ့ super ကို ရေးပြီး အစီအစဉ်ကို comment နဲ့ မှတ်ပါ။

ရိုးရိုး class ကို with နဲ့ သုံးဖို့ ကြိုးစားခြင်း

Dart 3 မှာ class Animal { } ကို class Dog with Animal { } လို့ သုံးရင် "can't be used as a mixin because it isn't a mixin class nor a mixin" error ရပါတယ်။ Animal ကို mixin သို့မဟုတ် mixin class ပြောင်းပါ၊ ဒါမှမဟုတ် is-a မှန်ရင် extends သုံးပါ။

Mixin state ကို class အားလုံး မျှသုံးတယ်လို့ ထင်ခြင်း

Mixin ထဲက field တစ်ခုက mixin ကို တပ်တဲ့ object တစ်ခုချင်းစီမှာ သီးခြား copy ဖြစ်ပါတယ် (အပေါ်က 1 vs 2 ရလဒ်)။ Class အားလုံး မျှသုံးတဲ့ counter လိုချင်ရင် static field ဒါမှမဟုတ် အပြင်က service object တစ်ခု သုံးရပါမယ်။

Best Practices

Best practices

  • Mixin ကို ဘာတစ်ခုလဲ မဟုတ်ဘဲ ဘာလုပ်နိုင်လဲနဲ့ နာမည်ပေးပါ: Logging, Shareable, Disposable, Validated။
  • Mixin တစ်ခုကို တာဝန်တစ်ခုပဲ ပေးပါ၊ ကြီးလာရင် ခွဲပါ။
  • Mixin က class ရဲ့ တစ်ခုခုကို လိုရင် abstract getter နဲ့ ကြေညာပါ၊ superclass ရဲ့ member လိုရင် on သုံးပါ။
  • Mixin က constructor argument မယူနိုင်လို့ configuration လိုတဲ့ behavior ကို composition (field တစ်ခု) နဲ့ ရေးပါ။

Mini challenge

Mini challenge: စျေးနှုန်း အလွှာတွေနဲ့ order

BasePrice class ပေါ်မှာ mixin နှစ်ခု ရေးပါ: FlatDiscount က စျေးကို 10 လျှော့ပြီး SalesTax က 1.2 နဲ့ မြှောက်ပါတယ်။ ဒီ mixin နှစ်ခုကို with နဲ့ ဆန့်ကျင်ဘက်အစီအစဉ်နှစ်မျိုး ပေါင်းထားတဲ့ class နှစ်ခုကို ဆောက်ပြီး total နှစ်ခု မတူတာကို print ထုတ်ပြပါ။

  • BasePrice.amount() က 100 ကို ပြန်ပေးပြီး mixin နှစ်ခုက BasePrice ပေါ်မှာ (on) ရှိရမယ်
  • FlatDiscount က super.amount() - 10၊ SalesTax က super.amount() * 1.2 ကို ပြန်ရမယ်
  • class နှစ်ခုက with FlatDiscount, SalesTax နဲ့ with SalesTax, FlatDiscount လို့ အစီအစဉ်ပြောင်းထားရမယ်
  • ရလဒ် နှစ်ခုကို label နဲ့ print ထုတ်ပြီး ဘာကြောင့် မတူလဲ comment ရေးရမယ်
price_layers.dart
class BasePrice {
  double amount() => 100;
}

// TODO 1: mixin FlatDiscount on BasePrice, amount() = super.amount() - 10
// TODO 2: mixin SalesTax on BasePrice, amount() = super.amount() * 1.2
// TODO 3: two classes that extend BasePrice and mix in both layers,
//         one with FlatDiscount first, the other with SalesTax first

void main() {
  // TODO 4: print both totals with a label, e.g. 'discount then tax: ...'
  // TODO 5: explain in a comment which layer runs first in each class
}

ရလဒ်: Run လိုက်ရင် ဒီလို print ထွက်ရပါမယ်: discount then tax: 108.0 tax then discount: 110.0

အရိပ်အမြွက် ကြည့်ရန်

with ရဲ့ နောက်ဆုံး mixin က အပေါ်ဆုံးမှာ ရှိပြီး ပထမဆုံး ခေါ်တာမို့ super.amount() ကနေ အောက်က အလွှာကို ခေါ်ပါတယ်။ with FlatDiscount, SalesTax ဆိုရင် SalesTax က (100 - 10) ကို 1.2 နဲ့ မြှောက်ပါတယ်။ ပြောင်းပြန်ဆိုရင် 100 * 1.2 ကနေ 10 နုတ်ပါတယ်။

အဖြေ ကြည့်ရန်
price_layers.dart
class BasePrice {
  double amount() => 100;
}

mixin FlatDiscount on BasePrice {
  @override
  double amount() => super.amount() - 10;
}

mixin SalesTax on BasePrice {
  @override
  double amount() => super.amount() * 1.2;
}

// Layers bottom to top: BasePrice, FlatDiscount, SalesTax.
class DiscountThenTax extends BasePrice with FlatDiscount, SalesTax {}

// Layers bottom to top: BasePrice, SalesTax, FlatDiscount.
class TaxThenDiscount extends BasePrice with SalesTax, FlatDiscount {}

void main() {
  print('discount then tax: ${DiscountThenTax().amount()}');
  print('tax then discount: ${TaxThenDiscount().amount()}');
}

Quick Quiz 1 / 3

class Base { String who() => 'Base'; } ရှိပြီး mixin A on Base နဲ့ mixin B on Base တစ်ခုချင်းက who() ကို 'A>' ဒါမှမဟုတ် 'B>' ထည့်ပြီး super.who() ကို ပေါင်းတယ်။ class C extends Base with A, B {} ဆိုရင် C().who() က ဘာလဲ?

Quick Quiz 2 / 3

mixin Flyer on Animal ရဲ့ အဓိပ္ပာယ်က ဘာလဲ?

Quick Quiz 3 / 3

Dart 3 မှာ class Animal { } ကို ရိုးရိုး class အဖြစ်ကြေညာပြီး class Dog with Animal { } လို့ ရေးလိုက်ရင် ဘာဖြစ်မလဲ?

မှတ်ထားရမယ့် အချက်များ

  • Mixin က state၊ method နဲ့ abstract requirement တွေကို class မည်မျှမဆို with နဲ့ တပ်ပေးပါတယ်၊ ဒါပေမဲ့ instance မဆောက်နိုင်ဘဲ constructor မရှိပါဘူး။
  • with list မှာ နောက်ဆုံး mixin က နာမည်တူ member ကို နိုင်ပြီး super က ကိုယ့်အောက်က အလွှာကို ရည်ညွှန်းတာမို့ အစီအစဉ်က behavior ကို ပြောင်းနိုင်ပါတယ်။
  • on နဲ့ mixin ကို superclass type တစ်ခုပေါ်မှာပဲ တပ်ခွင့်ပေးပြီး အဲဒီ type ရဲ့ member နဲ့ super ကို mixin ထဲမှာ သုံးခွင့်ရပါတယ်။
  • Dart 3 မှာ with နောက်ကို mixin ဒါမှမဟုတ် mixin class ပဲ ထည့်လို့ရပါတယ်၊ ရိုးရိုး class ကို မရပါဘူး။

Dart language overview — Dart

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Mixins | Thuta Learning