大家好,我是才淺。
設計模式的好處我們都聽說過,並且也是大廠面試必須要有的基礎功。但是一看到那麼生硬的文章,還有各種複雜的代碼和僞代碼就覺得特別的頭疼。
那麼有沒有什麼辦法可以很輕鬆的去學習掌握設計模式呢,在我上班摸魚的時候還真讓我發現了一個可以簡單學習設計模式的網站,
網站路徑爲: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
文章創作不易,如果對您有用,請點贊鼓勵一下。