<<interface>>Button
public interface Button {
void render();
void onClick();
}
HTMLButton
public class HtmlButton implements Button {
public void render() {
System.out.println("<button> Test Button</button>");
onClick();
}
public void onClick() {
System.out.println("Click ! button says - 'Hello'");
}
}
WindowsButton
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WindowsButton implements Button {
JFrame frame = new JFrame(); // 컨테이너 생성
JPanel panel = new JPanel(); // 컨테이너 내에 각각 다른 컴포넌트를 생성해 나눌 때 필요.
JButton button;
@Override
public void render() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello welcome");
label.setOpaque(true);
label.setBackground(new Color(234,233,126));
label.setFont(new Font("Dialog", Font.BOLD, 20));
//label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
frame.getContentPane().add(panel);
panel.add(label);
onClick();
panel.add(button);
frame.setSize(320,200);
frame.setVisible(true);
onClick();
}
@Override
public void onClick() {
button = new JButton("Exit");
button.addActionListener((ActionListener) new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
System.exit(0);
}
});
}
}
Dialog
public abstract class Dialog {
public void renderWindow() {
Button okButton = createButton();
okButton.render();
}
public abstract Button createButton();
}
HTMLDIalog
public class HtmlDialog extends Dialog{
@Override
public Button createButton() {
return new HtmlButton();
}
}
WindowsDialog
public class WindowsDialog extends Dialog{
@Override
public Button createButton() {
return new WindowsButton();
}
}
Demo
public class Demo {
private static Dialog dialog;
public static void main(String[] args) {
configure();
runBusinessLogic();
}
static void configure() {
if (System.getProperty("os.name").contentEquals("Windows 10")) {
dialog = new WindowsDialog();
}
else {
dialog = new HtmlDialog();
}
}
static void runBusinessLogic() {
dialog.renderWindow();
}
}
'OLD_달려라 > 소프트웨어공학' 카테고리의 다른 글
Design Pattern ] Singleton Pattern (0) | 2020.07.02 |
---|---|
Design Pattern] Builder (0) | 2020.07.02 |
Design Pattern] Abstract Factory (0) | 2020.06.28 |
Design Pattern] Factory Method (0) | 2020.06.27 |
댓글