zoukankan      html  css  js  c++  java
  • [原创]适配器模式的用例

     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5
    6 namespace Adapter
    7 {
    8 //打印文本用的虚基类
    9 abstract class WordProcessing
    10 {
    11 abstract public void prints();
    12 }
    13
    14 //本地派生类1
    15 class WordProcessingOne : WordProcessing
    16 {
    17 public override void prints()
    18 {
    19 Console.WriteLine("WordProcessingOne");
    20 }
    21 }
    22
    23 //本地派生类2
    24 class WordProcessingTwo : WordProcessing
    25 {
    26 public override void prints()
    27 {
    28 Console.WriteLine("WordProcessingTwo");
    29 }
    30 }
    31
    32 //来自第三方的文本类
    33 class NewWord
    34 {
    35 public void myPrint()
    36 {
    37 Console.WriteLine("NewWord");
    38 }
    39 }
    40
    41 //为了让第三方的类适应本地类中的方法,
    42 //采用适配器(adapter)模式
    43 //重新扩展出一个类WordProcessingThree
    44 /*---------------------------------------------*/
    45 class WordProcessingThree : WordProcessing
    46 {
    47 private NewWord xindewenben;
    48
    49 public WordProcessingThree(NewWord x)
    50 {
    51 xindewenben = x;
    52 }
    53
    54 public override void prints()
    55 {
    56 xindewenben.myPrint();
    57 }
    58 }
    59 /*---------------------------------------------*/
    60
    61 class Program
    62 {
    63 static void Main(string[] args)
    64 {
    65 WordProcessing ch1 = new WordProcessingOne();
    66 ch1.prints();
    67
    68 WordProcessing ch2 = new WordProcessingTwo();
    69 ch2.prints();
    70
    71 //通过本地类中的方法使用第三方类中的方法
    72 NewWord p = new NewWord();
    73 WordProcessing ch3 = new WordProcessingThree(p);
    74 ch3.prints();
    75
    76 Console.ReadKey();
    77 }
    78 }
    79 }
  • 相关阅读:
    第三十一篇 玩转数据结构——并查集(Union Find)
    第三十篇 玩转数据结构——字典树(Trie)
    开发Electron可能用到的工具
    最新NetMonitor代码
    用C++/CLI搭建C++和C#之间的桥梁
    xaml实现无边框窗口
    用xaml画的带阴影3D感的圆球
    创作了一个xml的替代格式
    域名投资入门和技巧
    Compaq Visual Fortran生成静态库的方法及使用
  • 原文地址:https://www.cnblogs.com/Elijah/p/2223154.html
Copyright © 2011-2022 走看看