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

推荐订阅源

Google DeepMind News
Google DeepMind News
C
Check Point Blog
J
Java Code Geeks
腾讯CDC
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
罗磊的独立博客
Last Week in AI
Last Week in AI
B
Blog
IT之家
IT之家
S
SegmentFault 最新的问题
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
博客园 - 聂微东
U
Unit 42
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
MyScale Blog
MyScale 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();
  }

}