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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - tf.li

清理MVC4 Internaet 项目模板清理 SQL2014 尝试读取或写入受保护的内存。这通常指示其他内存已损坏 nodejs 环境配置技巧 mongdb window 服务安装批处理 bower 安装后 jade 引用404问题 我的Prototype模式框架 转自CnBeta 令人深思 [译稿]为什么我们不要 .NET 程序员 JSON.NET、WebService与JQuery协同工作 我修改的IP地址掩码 呵呵~~开心 Ext.Ajax.request 传值问题 从小做大之一---------一切从建模开始 [求助]关于文本行转列的疑问~~ To Terrylee 我的设计模型之小议工厂模式 我对线程池的一点理解 面向接口编程之惑 第二版 面向接口编程之惑 我的设计模型之适配器模式 我的设计模型之简单工厂
我的设计模型之装饰者模式
tf.li · 2008-07-30 · via 博客园 - tf.li

今天听了webcast的C#设计模型 感觉更好了,因为之前看过《小菜与大鸟不得不说的事》今天温习一下感觉装饰者模式真的很爽啊好了 废话少说 放代码

Code

此为抽象类,是抽象的哦。

   class T50:Tank
    {
        
public override void Shot()
        {
            Console.WriteLine(
"我是T50坦克");
        }
public override void Run()
        {
            Console.WriteLine(
"我可走正常的路");
        }
    }

一个实体类继承自抽象

下面轮到我们的主角装饰者类上场了

    class JiaNongPo:Tank
    
{
        
private Tank tank;
        
public JiaNongPo(Tank tank)
        
{
            
this.tank = tank;
        }

        
public override void Shot()
        
{
            Console.WriteLine(
"装备加农炮");
        }

        
public override void Run()
        
{
            tank.Run();
        }

    }

    
class Papo : Tank
    
{
        
private Tank tank;
        
public Papo(Tank tank)
        
{
            
this.tank = tank;
        }

        
public override void Run()
        
{
            Console.WriteLine(
"装备爬坡器!");
        }

        
public override void Shot()
        
{
            tank.Shot();
        }


    }

这是我们的装饰者类,之前用过另外一个抽象装饰者类来继承Tank类 后来发现状态保存比较麻烦,所以不用了 直接用

具体的装饰者来继承但是一个缺点就是当Tank类中的方法较多的时候会要求全部重载 Tank类的方法比较麻烦啊,不知各位有什么好的解决办法。好了天不早了 我要休息了啊 努力中....