惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - I坐标

重新开源UDS Google Wave 提名邀请发送 时尚的轮回-Ajax 用动态代理实现AOP Catalysis的建模的3个层次 Catalysis 的构成部分与框架 使用log4net将日志记入PostgreSql Log4Net Appender 之 ADONetAppender 谁对谁失去了信心? Castle与Spring.Net在用法和配置上的区别 - I坐标 - 博客园 ICE:Slice语言(六)--接口、操作和异常(一) ICE:Slice语言(五)--用户定义类型 在ClickOnce和Windows Installer之间选择 合并XML文件与在客户端管理Web服务引用 ICE:Slice语言(四)--基本类型 ICE:Slice语言(三)--模块 ICE:Slice语言(二)--源文件和词法规则 ICE:Slice语言(一)-编译 ICE:5个服务
Castle与Mixin
I坐标 · 2005-04-07 · via 博客园 - I坐标

        Mixin是一种将某个类中的方法混入其他类中的软件开发风格。简单的说,就是一个类可以使用其他类的方法。这个初听起来有点像类的继承,但是这与传统的类继承的方式不一样。
        首先,Mixin不是类的继承。传统的,一个类A继承了某个类B,那么A就可以直接调用B中的非private的方法。但是在Mixin中,A与B没有继承关系,而A却能使用B的非private的方法。
        其次,Mixin的这些行为是在运行时发生的,而不是静态指定的。

        下面是一个来自Python的Mixin的例子:

John 翻著新的一期 LJ(Linux Journal) 

[John@mars /tmp]# vi Lover.py 

class lover:
    def __init__(self,man='man',woman='woman'):
        self.lover='When a '+man+' love a '+woman
        print self.lover
    def smile(self):
        print self.lover+':-)'
    def kiss(self):
        print self.lover+':-)(-:'


[John@mars /tmp]# python
Python 2.1 (#1, Apr 18 2001, 17:55:45)
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.

>>>from Lover import lover

>>>John_and_Rose=lover()
When a man love a woman

>>>John_and_Rose.smile()
when a man love a woman:-)

>>>John_and_Rose.kiss()
when a man love a woman:-)(-:

>>>John_and_Rose.sayGoodBye()
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: lover instance has no attribute 'sayGoodBye'

>>>John_and_rose.JohnAskWhy()
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: lover instance has no attribute 'JohnAskWhy'

>>>class RoseLoveSomebodyElse:
   def sayGoodBye(self):
       print "Let's say goodbye tonight."

>>>lover.__bases__+=(RoseLoveSomebodyElse,)

>>>John_and_Rose.sayGoodBye()
Let's say goodbye tonight.
>>>

练习完 Using Mix-ins with Python 之後, 
John 在闪著 >>> 的萤幕前大声地哭了起来。 

        注意,Python代码中的class RoseLoveSomebodyElse之后的代码,John定义了一个类,并将这个类与lover执行了Mixin,之后,lover具备了sayGoodBye的能力。
        这就是Mixin的威力。
        在学习Castle中,我发现Castle也具有Mixin的能力,所以,我模仿上面Python的例子,利用Castle尝试了Mixin。
        当然,C#不能像Python那样动态的执行以及支持多重继承,因此,我对上面的代码作了改动,使之适应C#。

        首先,我定义了一个类Lover及其接口ILover:

        与Python代码不同的是,我多增加了一个SayGoodbye2Lover的方法。

        然后,我再定义了一个LoverLoveSomebodyElse及其接口ILoverLoveSomebodyElse,这个类对应Python例子中的RoseLoveSomebodyElse类。

        随后,定义了一个Matchmaker类,这只是一个普通的类,不起任何作用。主要是为了创建代理用。

        好了,现在可以利用这三个类测试Mixin。这里主要对两个类测试Mixin:Lover和LoverLoveSomebodyElse

        在执行函数中输入如下代码:

        执行后的结果如下:

       就这样,proxy具备了Lover和LoverLoveSomebodyElse的所有可用的功能。

       很显然,从代码中可以看出,如果proxy是一个代码级的类的话,那么proxy继承了ILover和ILoverLoveSomebodyElse两个接口。正如我们在传统的给一个类添加行为的方法一样。

       例如,有一个类Foo,它只有一个方法:WriteMessage。如果我们想给Foo添加一个可选的Log行为,我们可以定义一个接口ILog,并给让Foo继承ILog,这样,Foo就变成了LoggableFoo了。

       Castle正是这样的做的。如我的另一篇Castle与动态代理中所说的,Castle创建一个类,这个类继承了ILover和ILoverLoveSomebodyElse接口,并使用Interceptor和Invocation织入Lover和LoverLoveSomebodyElse类的方法,从而使Proxy具备了两者的方法。

        使用Mixin可以让我们动态的决定哪些类在运行时可以具备哪些可选的功能。当然,由于C#不支持类多重继承,因此,我们必须要把能够Mixin的类抽象出接口。