设计原则:请重新审视“多重继承”,找机会拥抱一下“掺入(Mixin)”
名称解释多重继承:我没有使用多重继承的经验,因此这里不多说,大学学的C++,可惜没有学好。
Mixin:一个Mixin是一个方法和属性的集合,不同的语言提供的实现机制不一样。类型定义的时候可以声明他想包含的Mixin(可以是多个),这些Mixin包含的方易做图成为类型的一部分。
使用动机代码复用 AND 运行时不改变。Mixin是推论,MixinTarget是定理。如:C#的IEnumerable(MixinTarget)只包含一个方法,根据这个方法(定理)Enumerable(Mixin)扩展了N个方法(推论)。示例(ExtJs4.2)
1 /// <reference path="../ext-all-debug-w-comments.js" />
2 Ext.define('Enjoyable', {
3 play: function () {
4 console.log(this.getName() + '-play');
5 }
6 });
7
8 Ext.define('Workable', {
9 work: function () {
10 console.log(this.getName() + '-work');
11 }
12 });
13
14 Ext.define('User', {
15 mixins: {
16 'enjoyable': 'Enjoyable',
17 'workable': 'Workable'
18 },
19 config: { name: 'unknow' },
20
21 constructor: function () {
22 var me = this;
23
24 me.initConfig(arguments);
25 },
26
27 eat: function () {
28 for (var i = 0; i < arguments.length; i++) {
29 console.log(arguments[i]);
30 }
31 }
32 });
33
34 var user = Ext.create('User');
35
36 user.setName('段光伟');
37
38 user.play();
39 user.work();示例(C#扩展方法)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace MixinDemo
8 {
9 public class User
10 {
11 public string Name { get; set; }
12 }
13
14 public static class Enjoyable
15 {
16 public static void Play(this User user)
17 {
18 Console.WriteLine(user.Name + "-play");
19 }
20 }
21
22 public static class Workable
23 {
24 public static void Work(this User user)
25 {
26 Console.WriteLine(user.Name + "-work");
27 }
28 }
29 }示例(C#动态代理)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 using Castle.DynamicProxy;
8 using Castle.DynamicProxy.Generators;
9
10 namespace MixinStudy
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 var proxy = Factory.Create<User>();
17 proxy.Id = Guid.NewGuid();
18 proxy.Name = "段光伟";
19
20 (proxy as ITeacher).Teach();
21 (proxy as IFather).Play();
22 }
23 }
24
25 [Mixin(typeof(Teacher))]
26 [Mixin(typeof(Father))]
27 public class User
28 {
29 public virtual Guid Id { get; set; }
30 public virtual string Name { get; set; }
31 }
32
33 public inte易做图ce ITeacher
34 {
35 User User { get; set; }
36
37 void Teach();
38 }
39
40 public class Teacher : ITeacher
41 {
42 [MixinTarget]
43 public User User { get; set; }
44
45 public void Teach()
46 {
47 Console.WriteLine("我教你读书吧:" + this.User.Name);
48 }
49 }
50
51 public inte易做图ce IFather
52 {
53 User User { get; set; }
54
55 void Play();
56 }
57
58 public class Father : IFather
59 {
60 [MixinTarget]
61 &nb
补充:web前端 , JavaScript ,