表紙 編集 差分 一覧 最近 短縮 付箋 ログイン 凍結中

/Go言語学習帳/08_decorator の差分

N2Wiki > Go言語学習帳 > 08_decorator
ページに戻る
- #start
+ #start decorator
  #?anch -e
- **** 内容が未設定のページです
- ブラウザの「戻る」で前のページに戻ってください。
- または[[編集|EDIT]]のリンクをクリックして内容を入力してください。
+ package main
+ import "fmt"
+ 
+ type staff struct {
+     payment float;
+ }
+ 
+ type employee interface {
+     getOvertimePay() float;
+ }
+ 
+ func showOvertimePay(e employee, name string) {
+     fmt.Printf("%s: I'm getting %f yen. Haha!\n", 
+         name, e.getOvertimePay()
+     );
+ }
+ 
+ func (self *staff) getOvertimePay() float {
+     return self.payment;
+ }
+ 
+ type beTechnical struct {
+     prototype employee;
+ }
+ 
+ func (self *beTechnical) getOvertimePay() float {
+     return self.prototype.getOvertimePay() * 1.1;
+ }
+ 
+ type beLicensed struct {
+     prototype employee;
+ }
+ 
+ func (self *beLicensed) getOvertimePay() float {
+     return self.prototype.getOvertimePay() * 1.2;
+ }
+ 
+ type beManager struct {
+     prototype employee;
+ }
+ 
+ func (self *beManager) getOvertimePay() float {
+     return 0;
+ }
+ 
+ type beTemporary struct {
+     prototype employee;
+ }
+ 
+ func (self *beTemporary) getOvertimePay() float {
+     return 1000;
+ }
+ 
+ func main() {
+     john := &beTechnical{&beLicensed{&staff{1000}}};
+     showOvertimePay(john, "John");
+     
+     elen := &beTemporary{&beTechnical{&staff{1000}}};
+     showOvertimePay(elen, "Elen");
+ }
  #end
»