一款就算你‘hello word’都不会写可以学会设计模式的免费网站(Refactoring.Guru)

大家好,我是才浅。
设计模式的好处我们都听说过,并且也是大厂面试必须要有的基础功。但是一看到那么生硬的文章,还有各种复杂的代码和伪代码就觉得特别的头疼。
那么有没有什么办法可以很轻松的去学习掌握设计模式呢,在我上班摸鱼的时候还真让我发现了一个可以简单学习设计模式的网站,
网站路径为:Refactoring Guru
这个是一个国外的网站,但是小伙伴们不要一看是国外的网站心里就打退堂鼓,怕自己英语不好,看不懂,这个网站提供中文版本的翻译(现阶段还没完全翻译完成),大家可以直接看中文版的。
在这里插入图片描述
这个网站建立的初衷就是向让人们可以简单轻松的学习设计模式和重构。
Refactoring Guru包含了23种基础设计模式。并且每一个模式都有详细的说明,并配上生动的图片讲解,让人们可以更好的理解设计模式。
在这里插入图片描述
在每个设计模式中都会提到这个设计模式适用场景,让开发者在自己的程序中使用设计模式时有取舍。
在这里插入图片描述
并且每一个设计模式会给出不同编程语言的代码,开发者根据代码可以更好的理解这些设计模式
在这里插入图片描述
这里拿工厂方法的Java代码示例让大家看一下:

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();
    }
}

总的来说 这个网站还是很好的,如果你想学好设计模式,利用这个网站的免费开放资源,应该可以轻松的开始设计模式的学习,并且这种图片的方式也会让人吸收的很快。当然如果你不想在网站上学习也可以在平台上购买离线阅读的电子版。

文末,再度放上该网站地址,感兴趣的同学可前往学习:Refactoring Guru

文章创作不易,如果对您有用,请点赞鼓励一下。

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值