设计模式

发布于: 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 日 阅读数: 9
用户头像

丿淡忘

关注

还未添加个人签名 2018.05.23 加入

还未添加个人简介

评论

发布
暂无评论
设计模式