Design principles: Review "Multi-inheritance" and look for opportunities to embrace "Mixin )"

Source: Internet
Author: User

Name explanation multi-inheritance: I have no experience in using multi-inheritance, so I will not talk about it here. It is a pity that I did not learn it well.

Mixin: A Mixin is a set of methods and attributes. Different Languages provide different implementation mechanisms. When defining a type, you can declare the expected Mixin (which can be multiple). The methods contained in these mixins become part of the type.

Use motivation code reuse AND run without changing. Mixin is an inference, while MixinTarget is a theorem. For example, IEnumerable (MixinTarget) of C # only contains one method. According to this method (Theorem), Enumerable (Mixin) extends N methods (inferences ). Example (ExtJs4.2)
1 // <reference path = "../ext-all-debug-w-comments.js"/>
2 Ext. define ('enable ',{
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 'enable': 'enable ',
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 ('duan Guangwei ');
37
38 user. play ();
39 user. work (); example (C # Extension Method)
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} example (C # dynamic proxy)
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 = "Duan Guangwei ";
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 interface ITeacher
34 {
35 User {get; set ;}
36
37 void Teach ();
38}
39
40 public class Teacher: ITeacher
41 {
42 [MixinTarget]
43 public User {get; set ;}
44
45 public void Teach ()
46 {
47 Console. WriteLine ("I will teach you how to read:" + this. User. Name );
48}
49}
50
51 public interface IFather
52 {
53 User {get; set ;}
54
55 void Play ();
56}
57
58 public class Father: IFather
59 {
60 [MixinTarget]
61 public User {get; set ;}
62
63 public void Play ()
64 {
65 Console. WriteLine ("let me play with you:" + this. User. Name );
66}
67}
68
69 public static class Factory
70 {
71 public static T Create <T> (params object [] args)
72 where T: class
73 {
74 var generator = new ProxyGenerator ();
75
76 var options = new ProxyGenerationOptions ();
77
78 foreach (MixinAttribute attribute in typeof (User). GetCustomAttributes (true ))
79 {
80 var mixin = Activator. CreateInstance (attribute. MixinType );
81
82 options. AddMixinInstance (mixin );
83}
84
85 var target = Activator. CreateInstance (typeof (T), args) as T;
86
87 var proxy = generator. CreateClassProxyWithTarget (target, options );
88
89 foreach (var mixin in options. MixinsAsArray ())
90 {
91 foreach (var property in mixin. GetType (). GetProperties ())
92 {
93 if (property. GetCustomAttributes (typeof (MixinTargetAttribute), true). Any ())
94 {
95 property. SetValue (mixin, target );
96}
97}
98}
99
100 return proxy;
101}
102}
103
104 [AttributeUsage (AttributeTargets. Class, AllowMultiple = true)]
105 public class MixinAttribute: Attribute
106 {
107 public MixinAttribute (Type mixinType)
108 {
109 this. MixinType = mixinType;
110}
111
112 public Type MixinType {get; set ;}
113}
114
115 public class MixinTargetAttribute: Attribute
116 {
117}
118} example (C ++ )? 1
Class Enjoyable
{
Public:
Enjoyable (void );
~ Enjoyable (void );
Void Play ();
Virtual string GetName () = 0;
};

Class Workable
{
Public:
Workable (void );
~ Workable (void );
Void Work ();
Virtual string GetName () = 0;
};

Class User: public Enjoyable, public Workable
{
Public:
User (void );
~ User (void );

Private:
String name;

Public:
String GetName ();
Void SetName (string name );
};

User: User (void)
{
}

User ::~ User (void)
{
}

String User: GetName ()
{
Return this-> name;
}

Void User: SetName (string name)
{
This-> name = name;
}

Void Enjoyable: Play (){
Cout <(this-> GetName () + "play ");
}

Void Workable: Work (){
Cout <(this-> GetName () + "work ");
}
Example (Ruby)
1 module Enjoyable
2 def play
3 puts (self. name + "-play ")
4 end
5 end
6
7 module Workable
8 def work
9 puts (self. name + "-work ")
10 end
11 end
12
13 class User
14 include Enjoyable, Workable
15
16 attr_accessor: name
17 end
18
19 user = User. new
20 user. name = "Duan Guangwei"
21 user. play
22 user. work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.