*图片详细页*/>
日期:2020-12-30 类型:网站建设
package refactoring_guru.factory_method.example.buttons; * Common interface for all buttons. public interface Button { void render(); void onClick(); //buttons/HtmlButton.java: 具体产品 package refactoring_guru.factory_method.example.buttons; * HTML button implementation. 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 World! ); //buttons/WindowsButton.java: 另一个具体产品 package refactoring_guru.factory_method.example.buttons; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; * Windows button implementation. public class WindowsButton implements Button { JPanel panel new JPanel(); JFrame frame new JFrame(); JButton button; public void render() { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel label new JLabel( Hello World! ); label.setOpaque(true); label.setBackground(new Color(235, 233, 126)); label.setFont(new Font( Dialog , Font.BOLD, 44)); label.setHorizontalAlignment(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(); public void onClick() { button new JButton( Exit ); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.setVisible(false); System.exit(0); //factory //factory/Dialog.java: 基础创建者 package refactoring_guru.factory_method.example.factory; import refactoring_guru.factory_method.example.buttons.Button; * Base factory class. Note that factory is merely a role for the class. It * should have some core business logic which needs different products to be * created. public abstract class Dialog { public void renderWindow() { // ... other code ... Button okButton createButton(); okButton.render(); * Subclasses will override this method in order to create specific button * objects. public abstract Button createButton(); //factory/HtmlDialog.java: 具体创建者 package refactoring_guru.factory_method.example.factory; import refactoring_guru.factory_method.example.buttons.Button; import refactoring_guru.factory_method.example.buttons.HtmlButton; * HTML Dialog will produce HTML buttons. public class HtmlDialog extends Dialog { Override public Button createButton() { return new HtmlButton(); //factory/WindowsDialog.java: 另一个具体创建者 package refactoring_guru.factory_method.example.factory; import refactoring_guru.factory_method.example.buttons.Button; import refactoring_guru.factory_method.example.buttons.WindowsButton; * Windows Dialog will produce Windows buttons. public class WindowsDialog extends Dialog { Override public Button createButton() { return new WindowsButton(); //Demo.java: 客户端代码 package refactoring_guru.factory_method.example; import refactoring_guru.factory_method.example.factory.Dialog; import refactoring_guru.factory_method.example.factory.HtmlDialog; import refactoring_guru.factory_method.example.factory.WindowsDialog; * Demo class. Everything comes together here. public class Demo { private static Dialog dialog; public static void main(String[] args) { configure(); runBusinessLogic(); * The concrete factory is usually chosen depending on configuration or * environment options. static void configure() { if (System.getProperty( os.name ).equals( Windows 10 )) { dialog new WindowsDialog(); } else { dialog new HtmlDialog(); * All of the client code should work with factories and products through * abstract interfaces. This way it does not care which factory it works * with and what kind of product it returns. static void runBusinessLogic() { dialog.renderWindow();
总的来说 这个网站还是很好的 如果你想学好设计模式 利用这个网站的免费开放资源 应该可以轻松的开始设计模式的学习 并且这种图片的方式也会让人吸收的很快。当然如果你不想在网站上学习也可以在平台上购买离线阅读的电子版。
文末 再度放上该网站地址 感兴趣的同学可前往学习
文章创作不易 如果对您有用 请点赞鼓励一下。
抵扣说明:
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。
Copyright © 2002-2020 微信小程序页面设计_python小程序_小程序首页模板_如何建立微信小程序_怎么做微信小程序 版权所有 (网站地图) 粤ICP备10235580号