写点什么

架构师训练营第 3 周 _ 课后作业

用户头像
方舟勇士
关注
发布于: 2020 年 06 月 21 日

作业一:

请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。



下图是我最常用的单例实现方式(使用C#)。手写代码果然懵逼。

作业二:

请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。



下面是用C#代码实现的

public abstract class Control
{
public List<Control> Children { get; set; }
public String DataTemplate { get; set; }
public Control()
{
Children = new List<Control>();
DataTemplate = "";
}
public virtual void Draw()
{
this.OnDraw();
foreach (var e in Children)
{
e.Draw();
}
}
protected abstract void OnDraw();
}
public class WinForm : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "WinForm", DataTemplate));
}
}
public class Picture : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "Picture", DataTemplate));
}
}
public class Button : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "Button", DataTemplate));
}
}
public class Frame : Control {
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "Frame", DataTemplate));
}
}
public class Label : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "Label", DataTemplate));
}
}
public class TextBox : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "TextBox", DataTemplate));
}
}
public class PasswordBox : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "PasswordBox", DataTemplate));
}
}
public class CheckBox : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "CheckBox", DataTemplate));
}
}
public class LinkLabel : Control
{
protected override void OnDraw()
{
Console.WriteLine(String.Format("print {0} ({1})", "LinkLabel", DataTemplate));
}
}

组合对象应用场景

class Program
{
static void Main(string[] args)
{
var wnd = new WinForm()
{
DataTemplate = "WINDOW窗口"
};
var picLogo = new Picture
{
DataTemplate = "LOGO图片"
};
wnd.Children.Add(picLogo);
var btnLogin = new Button
{
DataTemplate = "登录"
};
wnd.Children.Add(btnLogin);
var btnReg = new Button()
{
DataTemplate = "注册"
};
wnd.Children.Add(btnReg);
var frame1 = new Frame()
{
DataTemplate = "FRAME1"
};
var labelUserName = new Label()
{
DataTemplate = "用户名"
};
frame1.Children.Add(labelUserName);
var tbUserName = new TextBox()
{
DataTemplate = "文本框"
};
frame1.Children.Add(tbUserName);
var labelPassword = new Label()
{
DataTemplate = "密码"
};
frame1.Children.Add(labelPassword);
var pbPassword = new PasswordBox()
{
DataTemplate = "密码框"
};
frame1.Children.Add(pbPassword);
var cblabelRememberUserName = new CheckBox()
{
DataTemplate = "复选框"
};
frame1.Children.Add(cblabelRememberUserName);
var labelRememberUserName = new Label()
{
DataTemplate = "记住用户名"
};
frame1.Children.Add(labelRememberUserName);
var forgetPassword = new LinkLabel()
{
DataTemplate = "忘记密码"
};
frame1.Children.Add(forgetPassword);
wnd.Children.Add(frame1);
wnd.Draw();
Console.ReadKey();
}
}

输出结果



用户头像

方舟勇士

关注

还未添加个人签名 2018.08.27 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第 3 周 _ 课后作业