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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - AlanPaoPao

23)Visitor 22)Template 21)Strategy 20)State 19)Observer 18)Memento 17)Mediator 16)Iterator 15)Interpreter 14)Command 13)Chain Of Responsibility 12)Proxy 11)Flyweight 10)Facade 08)Composite 07)Bridge 06)Adapter 05)Prototype 04)Factory Method
09)Decorator
AlanPaoPao · 2007-09-19 · via 博客园 - AlanPaoPao

    装饰模式的目的是: 动态地给一个对象添加一些额外的职责(比生成子类更为灵活)
    实例代码:

interface IItemLibrary
{
  
void Display();
  
int ItemCount getset; }
  
double Price getset; }
}

abstract class ItemLibrary : IItemLibrary
{
  
protected int count;
  
protected double price;
  
public double Price
  
{
    
get return price; }
    
set { price = value; }
  }

  
public int ItemCount
  
{
    
get return count; }
    
set { count = value; }
  }

  
public abstract void Display();
}

class Curtain : ItemLibrary
{
  
private string mode;
  
private string name;
  
public Curtain(string mode, string name, int count,double price)
  
{
    
this.mode = mode;
    
this.name = name;
    
this.count = count;
    
this.price = price;
  }

  
public override void Display()
  
{
    Console.WriteLine(
"窗帘 ------ ");
    Console.WriteLine(
" 样式: {0}", mode);
    Console.WriteLine(
" 名称: {0}", name);
    Console.WriteLine(
" 数量: {0}", count.ToString());
    Console.WriteLine(
" 价格: {0}", price.ToString());
  }

}

class Sofa : ItemLibrary
{
  
private string type;
  
private string color;
  
private string name;
  
public Sofa(string type, string color, string name, int count, double price)
  
{
    
this.type = type;
    
this.color = color;
    
this.name = name;
    
this.count = count;
    
this.price = price;
  }

  
public override void Display()
  
{
    Console.WriteLine(
"沙发 ------ ");
    Console.WriteLine(
" 类型: {0}", type);
    Console.WriteLine(
" 名称: {0}", name);
    Console.WriteLine(
" 颜色: {0}", color);
    Console.WriteLine(
" 数量: {0}", count.ToString());
    Console.WriteLine(
" 价格: {0}", price.ToString());
  }

}

abstract class Decorator : ItemLibrary
{
  
protected IItemLibrary iItem;
  
public Decorator(IItemLibrary libraryItem)
  
{
    
this.iItem = libraryItem;
  }

  
public override void Display()
  
{
    iItem.Display();
  }

}

class Rebate : Decorator
{
  
protected double agio;
  
public Rebate(IItemLibrary libraryItem,Double agio)
    : 
base(libraryItem)
  
{
    
this.agio = agio;
  }

  
public Double BuyItem(Int32 count)
  
{
    iItem.ItemCount 
-= count;
    
return count * iItem.Price * this.agio;
  }

  
public override void Display()
  
{
    
base.Display();
    Console.WriteLine(
"折扣率: " + agio.ToString());
  }

}

class MainApp
{
  
static void Main()
  
{
    IItemLibrary rome 
= new Curtain("罗马帘","ITEM00001",100,98.9);
    rome.Display();
    IItemLibrary smallSofa 
= new Sofa("三人沙发","红色","ITEM010101"405300);
    smallSofa.Display();
    Console.WriteLine(
"\nBegion Rebate:");
    Rebate rebate 
= new Rebate(smallSofa, 0.8);
    Console.WriteLine(
"购买{0}台沙发{1}钱:""5", rebate.BuyItem(5).ToString());
    Console.WriteLine(
"购买{0}台沙发{1}钱:""2", rebate.BuyItem(2).ToString());
    rebate.Display();
    Console.Read();
  }

}