设计模式
发布于: 2020 年 06 月 24 日
作业 1 手写单例
作业 2 组合模式
Component.cs 组件抽象类
public abstract class Component{ public String Name { get; set; }
public Component(String name) { Name = name; }
public abstract void Show();}复制代码
Contains.cs 容器组件抽象类
public abstract class Contains : Component { private List<Component> m_subCompents = new List<Component>();
public Contains(String name) : base(name) { }
protected abstract void ShowOwn();
public void AddSubComponent(Component component) { m_subCompents.Add(component); }
public override void Show() { ShowOwn(); foreach (var item in m_subCompents) { item.Show(); } } }复制代码
Button.cs 组件类 Lable 等组件代码实现方式类似
public class Button : Component { public Button(string name) :base(name) { }
public override void Show() { Console.WriteLine("print Button(" + Name + ")"); } }复制代码
Winform.cs 容器组件类 Frame 类实现方式类似
public class Winform : Contains { public Winform(String name) : base(name) { }
protected override void ShowOwn() { Console.WriteLine("print Frame(" + Name + ")"); } }复制代码
Program.cs
class Program { static void Main(string[] args) { Winform winform = new Winform("WINDOW窗口"); winform.AddSubComponent(new Picture("LOGO图片")); winform.AddSubComponent(new Button("登录")); winform.AddSubComponent(new Button("注册")); Frame frame = new Frame("FRAME1"); winform.AddSubComponent(frame);
frame.AddSubComponent(new Lable("用户名")); frame.AddSubComponent(new TextBox("文本框")); frame.AddSubComponent(new Lable("密码")); frame.AddSubComponent(new PasswordBox("密码框")); frame.AddSubComponent(new CheckBox("复选框")); frame.AddSubComponent(new TextBox("记住用户名")); frame.AddSubComponent(new LinkLable("忘记密码"));
winform.Show();
} }复制代码
输出结果
划线
评论
复制
发布于: 2020 年 06 月 24 日阅读数: 52
版权声明: 本文为 InfoQ 作者【丿淡忘】的原创文章。
原文链接:【http://xie.infoq.cn/article/3f7f7419d3d81a5b8e508a9d4】。
本文遵守【CC BY-NC】协议,转载请保留原文出处及本版权声明。
丿淡忘
关注
还未添加个人签名 2018.05.23 加入
还未添加个人简介











评论