第三周作业
作业 1:请在草稿纸上手写一个单例模式的实现代码。
作业 2:
package demo.pattern;
public class DrawApplication {
public static void main(String[] args) {
WinForm winform = new WinForm("主窗口");
Component picture = new Picture("LOGO 图片");
winform.add(picture);
Component loginButton = new Button("登陆");
winform.add(loginButton);
Component registButton = new Button("注册");
winform.add(registButton);
//
Frame frame = new Frame("Frame1");
Component usernameLabel = new Label("用户名");
frame.add(usernameLabel);
Component usernameTextBox = new TextBox("文本框");
frame.add(usernameTextBox);
Component passwordLabel = new Label("密码");
frame.add(passwordLabel);
Component passwordTextBox = new TextBox("密码框");
frame.add(passwordTextBox);
Component checkBox = new CheckBox("复选框");
frame.add(checkBox);
Component textBox = new TextBox("记住用户名");
frame.add(textBox);
Component linkLabel = new LinkLabel("忘记密码");
frame.add(linkLabel);
winform.add(frame);
//开始绘制窗口
winform.draw();
}
}
----------------------------------------------------------------------------------------
package demo.pattern;
public interface Component {
public void draw();
}
-----------------------------------------------------------------------
package demo.pattern;
public class Button implements Component {
private String name = null;
public Button(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("Draw Button: "+name);
}
}
-----------------------------------------------------------------------------------
package demo.pattern;
public class CheckBox implements Component {
private String name = null;
public CheckBox(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("draw CheckBox: "+name);
}
}
-------------------------------------------------------------------------------------
package demo.pattern;
public class Label implements Component {
private String text;
public Label(String text) {
this.text = text;
}
@Override
public void draw() {
System.out.println("draw Label: "+text);
}
}
-------------------------------------------------------------------------------
package demo.pattern;
public class LinkLabel implements Component {
private String text;
public LinkLabel(String text) {
this.text = text;
}
@Override
public void draw() {
System.out.println("draw LinkLabel: "+text);
}
}
--------------------------------------------------------------------------
package demo.pattern;
public class PasswordBox implements Component {
private String name = null;
public PasswordBox(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("draw PasswordBox: "+name);
}
}
-------------------------------------------------------------------------------
package demo.pattern;
public class Picture implements Component {
private String name;
public Picture(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("Draw Picture: "+name);
}
}
------------------------------------------------------------------------------------------
package demo.pattern;
public class TextBox implements Component {
private String name = null;
public TextBox(String name) {
this.name = name;
}
@Override
public void draw() {
System.out.println("draw TextBox: "+name);
}
}
--------------------------------------------------------------------------------------
package demo.pattern;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public abstract class Container implements Component {
public Container() {
}
private List<Component> children = new ArrayList<Component>();
public void add(Component c) {
children.add(c);
}
public void remove(int index) {
children.remove(index);
}
public Component getChild(int index) {
return children.get(index);
}
@Override
public void draw() {
drawSelf();
// 绘制子空间
Iterator<Component> it = children.iterator();
while (it.hasNext()) {
Component c = it.next();
c.draw();
}
}
abstract public void drawSelf();
}
--------------------------------------------------------------------------------------------
package demo.pattern;
public class Frame extends Container {
private String name;
public Frame(String name) {
this.name = name;
}
public void draw() {
super.draw();
}
@Override
public void drawSelf() {
System.out.println("draw Frame: "+name);
}
}
------------------------------------------------------------------------------------------------
package demo.pattern;
public class WinForm extends Container {
private String name;
public WinForm(String name) {
this.name = name;
}
@Override
public void drawSelf() {
System.out.println("draw WinForm: "+name);
}
}
-----------------------------------------------------------------------------------------------
评论