Decorator

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication2
{
    
//componnent
    abstract class libraryitem
    {
        
private int numbers;
        
public int Numbers
        {
            
get { return numbers; }
            
set { numbers = value; }
        }
        
public abstract void display();
    }
    
//concretecomponent

    
class book : libraryitem
    {
        
private string author;
        
public book(string author, int numbers)
        {
            
this.author = author;
            
this.Numbers = numbers;
        }
        
public override void display()
        {
            Console.WriteLine(
"\nBook----------");
            Console.WriteLine(
"author {0}", author);
            Console.WriteLine(
"numbers{0}", Numbers);
        }
    }


    
//decorator

    
abstract class decorate : libraryitem
    {
        
protected libraryitem libraryitem;
        
public decorate(libraryitem libraryitem)
        {
            
this.libraryitem = libraryitem;
        }

        
public override void display()
        {
            libraryitem.display();
        }
    }
    
//conceredecorator
    class borrowable : decorate
    {
        
protected ArrayList borrwoers = new ArrayList();
        
public borrowable(libraryitem libraryitem)
            : 
base(libraryitem)
        { }
        
public void borrowitem(string name)
        {
            borrwoers.Add(name);
            libraryitem.Numbers
--;
        }
        
public void returnitem(string name)
        {
            borrwoers.Remove(name);
            libraryitem.Numbers
++;
        }

        
public override void display()
        {
            
base.display();
            
foreach (string borrower in borrwoers)
            {
                Console.WriteLine(
"borrowers:{0}", borrower);
            }
        }
    }
    
//conceredecorator
    class colorable : decorate
    {
        
private string color;
        
public colorable(libraryitem libraryitem)
            : 
base(libraryitem)
        {

        }
        
public void setColor(string color)
        {
            
this.color = color;
        }
        
public override void display()
        {
            
base.display();

            Console.WriteLine(
"book's color{0}",color);
        }
    }

}

 

 

posted on 2010-03-18 10:27  孟凡龙  阅读(114)  评论(0)    收藏  举报