alonestarsz

博客园 首页 新随笔 联系 订阅 管理
using System;

namespace Adapter.TextShape
{
    
/// <summary>
    
/// 将两个不兼容的类纠合在一起使用,属于结构型模式,
    
/// 需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.
    
/// 实现方法:组合(composition)和继承(inheritance).
    
/// </summary>

    public interface  Shape
    
{
         
void Draw();
         
void Border();
    }

    
        
/// <summary>
    
/// TextShape 的摘要说明。
    
/// </summary>

    public class TextShape
    
{
        
private string content; 
        
public TextShape() 
        
{
        
        }

        
public void SetContent(string str) 
        
{
            content 
= str;
        }

        
public string GetContent() 
        
{
            
return content;
        }

    }

    
    
/// <summary>
    
/// TextShapeObject 的摘要说明。
    
/// </summary>

    public class TextShapeObject:Shape
    
{

        
private TextShape  text ;
        
        
        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="text"></param>

        public TextShapeObject(TextShape text)
        
{
            
this.text = text;
        }


        
public void Draw() 
        
{
            Console.WriteLine(
"Draw a shap ! Impelement Shape interface !");
        }


        
public void Border() 
        
{
            Console.WriteLine(
"Set the border of the shap ! Impelement Shape interface !");
        }


        
public void SetContent(string str) 
        
{
            text.SetContent(str);
        }

        
public string GetContent() 
        
{
            
return text.GetContent();
        }

//        [STAThread]
//        public static void Main(string[] args) 
//        {
//            TextShape myText = new TextShape();
//            TextShapeObject myTextShapeObject = new TextShapeObject(myText);
//            myTextShapeObject.Draw();
//            myTextShapeObject.Border();
//            myTextShapeObject.SetContent("A test text !");
//            Console.WriteLine("The content in Text Shape is :" + myTextShapeObject.GetContent());
//            Console.ReadLine();
//        
//        }
    }

    
    
    
class TextShapeClass:Adapter.TextShape.TextShape,Shape
    
{
        
/// <summary>
        
///以下为测试代码
        
/// </summary>
        
/// 

        public TextShapeClass() 
        
{
        }

        
public void Draw() 
        
{
            Console.WriteLine(
"Draw a shap ! Impelement Shape interface !");
        }

        
public void Border() 
        
{
            Console.WriteLine(
"Set the border of the shap ! Impelement Shape interface !");
        }

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// TODO: 在此处添加代码以启动应用程序
            
//
            TextShapeClass myTextShapeClass = new TextShapeClass();
            myTextShapeClass.Draw();
            myTextShapeClass.Border();
            myTextShapeClass.SetContent(
"A test text !");
            Console.ReadLine();
        }

    }

}

Adapter: 将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.
posted on 2004-11-19 16:39  天煞孤星  阅读(123)  评论(0编辑  收藏  举报