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

如题,本文是一篇关于架构师训练营课后习题的文章,主要为草稿纸上手写一个单例模式的实现代码,以及用组合设计模式编写程序,打印窗口和窗口组件的树结构。

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

由于使用的编程语言是 C#,实现单例模式非常简单,代码如下图示:

单例模式代码
单例模式代码

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

作业配图
作业配图

我是使用 .NET 实现窗口开发,实现效果如下:

窗口模式示例
窗口模式示例

代码如下:

private void InitializeComponent() 
{ 
   System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WinForm)); 
   this.pictureBox1 = new System.Windows.Forms.PictureBox(); 
   this.panel1 = new System.Windows.Forms.Panel(); 
   this.label1 = new System.Windows.Forms.Label(); 
   this.label2 = new System.Windows.Forms.Label(); 
   this.textBox1 = new System.Windows.Forms.TextBox(); 
   this.textBox2 = new System.Windows.Forms.TextBox(); 
   this.checkBox1 = new System.Windows.Forms.CheckBox(); 
   this.linkLabel1 = new System.Windows.Forms.LinkLabel(); 
   this.button1 = new System.Windows.Forms.Button(); 
   this.button2 = new System.Windows.Forms.Button(); 
   ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 
   this.panel1.SuspendLayout(); 
   this.SuspendLayout(); 
   //  
   // pictureBox1 
   //  
   this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image"))); 
   this.pictureBox1.Location = new System.Drawing.Point(18, 30); 
   this.pictureBox1.Name = "pictureBox1"; 
   this.pictureBox1.Size = new System.Drawing.Size(455, 169); 
   this.pictureBox1.TabIndex = 0; 
   this.pictureBox1.TabStop = false; 
   //  
   // panel1 
   //  
   this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128))))); 
   this.panel1.Controls.Add(this.linkLabel1); 
   this.panel1.Controls.Add(this.checkBox1); 
   this.panel1.Controls.Add(this.textBox2); 
   this.panel1.Controls.Add(this.textBox1); 
   this.panel1.Controls.Add(this.label2); 
   this.panel1.Controls.Add(this.label1); 
   this.panel1.Location = new System.Drawing.Point(18, 229); 
   this.panel1.Name = "panel1"; 
   this.panel1.Size = new System.Drawing.Size(455, 181); 
   this.panel1.TabIndex = 1; 
   //  
   // label1 
   //  
   this.label1.AutoSize = true; 
   this.label1.Location = new System.Drawing.Point(57, 29); 
   this.label1.Name = "label1"; 
   this.label1.Size = new System.Drawing.Size(67, 15); 
   this.label1.TabIndex = 0; 
   this.label1.Text = "用户名:"; 
   //  
   // label2 
   //  
   this.label2.AutoSize = true; 
   this.label2.Location = new System.Drawing.Point(72, 82); 
   this.label2.Name = "label2"; 
   this.label2.Size = new System.Drawing.Size(52, 15); 
   this.label2.TabIndex = 1; 
   this.label2.Text = "密码:"; 
   //  
   // textBox1 
   //  
   this.textBox1.Location = new System.Drawing.Point(159, 29); 
   this.textBox1.Name = "textBox1"; 
   this.textBox1.Size = new System.Drawing.Size(207, 25); 
   this.textBox1.TabIndex = 2; 
   //  
   // textBox2 
   //  
   this.textBox2.Location = new System.Drawing.Point(159, 82); 
   this.textBox2.Name = "textBox2"; 
   this.textBox2.Size = new System.Drawing.Size(207, 25); 
   this.textBox2.TabIndex = 3; 
   //  
   // checkBox1 
   //  
   this.checkBox1.AutoSize = true; 
   this.checkBox1.Location = new System.Drawing.Point(60, 138); 
   this.checkBox1.Name = "checkBox1"; 
   this.checkBox1.Size = new System.Drawing.Size(104, 19); 
   this.checkBox1.TabIndex = 4; 
   this.checkBox1.Text = "记住用户名"; 
   this.checkBox1.UseVisualStyleBackColor = true; 
   //  
   // linkLabel1 
   //  
   this.linkLabel1.AutoSize = true; 
   this.linkLabel1.Location = new System.Drawing.Point(284, 141); 
   this.linkLabel1.Name = "linkLabel1"; 
   this.linkLabel1.Size = new System.Drawing.Size(82, 15); 
   this.linkLabel1.TabIndex = 5; 
   this.linkLabel1.TabStop = true; 
   this.linkLabel1.Text = "忘记密码?"; 
   //  
   // button1 
   //  
   this.button1.Location = new System.Drawing.Point(93, 444); 
   this.button1.Name = "button1"; 
   this.button1.Size = new System.Drawing.Size(75, 23); 
   this.button1.TabIndex = 2; 
   this.button1.Text = "登录"; 
   this.button1.UseVisualStyleBackColor = true; 
   //  
   // button2 
   //  
   this.button2.Location = new System.Drawing.Point(281, 444); 
   this.button2.Name = "button2"; 
   this.button2.Size = new System.Drawing.Size(75, 23); 
   this.button2.TabIndex = 2; 
   this.button2.Text = "注册"; 
   this.button2.UseVisualStyleBackColor = true; 
   //  
   // WinForm 
   //  
   this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); 
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
   this.ClientSize = new System.Drawing.Size(503, 491); 
   this.Controls.Add(this.button2); 
   this.Controls.Add(this.button1); 
   this.Controls.Add(this.panel1); 
   this.Controls.Add(this.pictureBox1); 
   this.Name = "WinForm"; 
   this.Text = "Form1"; 
   ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 
   this.panel1.ResumeLayout(false); 
   this.panel1.PerformLayout(); 
   this.ResumeLayout(false); 
}

由于 C# 与Java实现窗体机制不同,这里不打印组件输出示例。

《架构师训练营第3周课后作业》的相关评论

发表评论

必填项已用 * 标记,邮箱地址不会被公开。