适配器模式-C#实现

本文详细介绍了适配器模式在WPF中的应用,通过创建PowerAdapter类作为三孔插头和两孔插头的适配器,展示了如何在新环境中利用已存在的接口实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

适配器模式——使得新环境中不需要去重复实现已经存在了的实现而很好地把现有对象(指原来环境中的现有对象)加入到新环境来使用。

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。
这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。
举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,
再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;

namespace 设计模式练习.Model.适配器模式
{

    //适配器类
    internal class PowerAdapter : Tworole, Threehole
    {   
        
        public string Msg { get; set; }

        public void requset()
        {
            //在三孔插头的内部调用两孔插头的方法
            string msg=string.Empty;
            speciTwoRequest(ref msg);
            Msg = msg;
        }

       
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.适配器模式
{

    //三孔插头
    public interface  Threehole
    {
        public void requset();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.适配器模式
{
    //两孔插头
    public abstract class Tworole
    {
        public void speciTwoRequest(ref string msg)
        {
            msg = "两孔插头";
        }
    }
}

二 View

<Window x:Class="设计模式练习.View.适配器模式.Adapter"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.适配器模式"
        mc:Ignorable="d"
        Title="Adapter" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Content="{Binding Res}" Grid.Column="0"/>
        <Button Content="适配器" Grid.Column="1" Command="{Binding adapterCommand}"/>
    </Grid>
</Window>

三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.适配器模式;

namespace 设计模式练习.ViewModel.适配器模式
{
    partial class Adapter_ViewModel : ObservableObject
    {
        [ObservableProperty]
        private string res;

        [RelayCommand]
        private void adapter()
        {
            PowerAdapter powerAdapter = new PowerAdapter();
            powerAdapter.requset();

            Res = powerAdapter.Msg;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值