日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

四:轻松学设计模式:创建型、结构型、行为型概览

發布時間:2024/1/18 asp.net 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四:轻松学设计模式:创建型、结构型、行为型概览 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在軟件開發的世界中,有一個眾所周知的詞匯——設計模式。設計模式是一種解決特定問題的優雅方案,代碼讓人看起來就有種自然的感覺。現在,我們就來輕松、幽默地了解一下這些神秘的設計模式吧!

創建型(Creational):生孩子還是領養?這里有五種方法!

1、單例模式(Singleton):只要一個寶貝!

想象一下,你的家里只能有一臺電視機。這臺電視機就像單例模式中的唯一實例。Java代碼示例:

public class Television {private static Television instance;private Television() {}public static Television getInstance() {if (instance == null) {instance = new Television();}return instance;} }

2、工廠方法模式(Factory Method):專業的孩子制造工廠!

每個孩子都是獨一無二的,工廠方法模式就像一個專業的孩子制造工廠,可以根據需求生產出不同類型的孩子。Java代碼示例:

public interface Child {void play(); }public class NaughtyChild implements Child {public void play() {System.out.println("I'm a naughty child!");} }public class QuietChild implements Child {public void play() {System.out.println("I'm a quiet child.");} }public abstract class ChildFactory {public abstract Child createChild(); }

3、抽象工廠模式(Abstract Factory):萬能孩子制造廠!

抽象工廠模式就像一個萬能孩子制造廠,不僅可以生產孩子,還能生產他們的玩具、衣服等。Java代碼示例:

public interface Toy {void playWith(); }public class CarToy implements Toy {public void playWith() {System.out.println("Vroom, vroom!");} }public class DollToy implements Toy {public void playWith() {System.out.println("Let's have a tea party!");} }public abstract class AbstractChildFactory {public abstract Child createChild();public abstract Toy createToy(); }

4、建造者模式(Builder):DIY你的孩子!

建造者模式就像用積木搭建玩具,你可以自由搭配各種屬性,組成不同的孩子。Java代碼示例:

public class ChildBuilder {private String name;private int age;private String hobby;public ChildBuilder setName(String name) {this.name = name;return this;}public ChildBuilder setAge(int age) {this.age = age;return this;}public ChildBuilder setHobby(String hobby) {this.hobby = hobby;return this;}public Child build() {return new Child(name, age, hobby);} }

5、原型模式(Prototype):克隆孩子,簡單快捷!

想要快速復制一個孩子?原型模式就像克隆技術,讓你輕松復制孩子。Java代碼示例:

public class Child implements Cloneable {private String name;public Child(String name) {this.name = name;}public String getName() {return name;}@Overrideprotected Object clone() throws CloneNotSupportedException {return super.clone();} }

結構型(Structural):孩子們的組織秘籍!

1、適配器模式(Adapter):兼容新老玩具!

適配器模式就像一個萬能插頭,讓新舊玩具可以共用。Java代碼示例:

public interface OldToy {void oldPlay(); }public interface NewToy {void newPlay(); }public class ToyAdapter implements NewToy {private OldToy oldToy;public ToyAdapter(OldToy oldToy) {this.oldToy = oldToy;}public void newPlay() {oldToy.oldPlay();} }

2、橋接模式(Bridge):孩子與玩具的多樣組合!

橋接模式就像拼圖游戲,孩子和玩具可以任意組合。Java代碼示例:

public interface Child {void play(Toy toy); }public interface Toy {void playWith(); }public class Boy implements Child {public void play(Toy toy) {System.out.println("I'm a boy, and I'm playing with:");toy.playWith();} }public class Girl implements Child {public void play(Toy toy) {System.out.println("I'm a girl, and I'm playing with:");toy.playWith();} }

3、組合模式(Composite):組織孩子們的團隊!

組合模式就像樹形結構,可以輕松地組織孩子們的團隊。Java代碼示例:

public abstract class ChildComponent {public void add(ChildComponent component) {throw new UnsupportedOperationException();}public void remove(ChildComponent component) {throw new UnsupportedOperationException();}public ChildComponent getChild(int index) {throw new UnsupportedOperationException();}public void play() {throw new UnsupportedOperationException();} }public class ChildGroup extends ChildComponent {private List<ChildComponent> components = new ArrayList<>();@Overridepublic void add(ChildComponent component) {components.add(component);}@Overridepublic void remove(ChildComponent component) {components.remove(component);}@Overridepublic ChildComponent getChild(int index) {return components.get(index);}@Overridepublic void play() {for (ChildComponent component : components) {component.play();}} }

行為型(Behavioral):孩子們的行為規范!

1、觀察者模式(Observer):媽媽總是知道!

觀察者模式就像是媽媽總是知道孩子們在做什么。當孩子們的行為發生變化時,媽媽會立即得知。Java代碼示例:

public interface Observer {void update(String message); }public interface Subject {void registerObserver(Observer observer);void removeObserver(Observer observer);void notifyObservers(); }public class Child implements Subject {private List<Observer> observers;private String activity;public Child() {observers = new ArrayList<>();}public void setActivity(String activity) {this.activity = activity;notifyObservers();}@Overridepublic void registerObserver(Observer observer) {observers.add(observer);}@Overridepublic void removeObserver(Observer observer) {observers.remove(observer);}@Overridepublic void notifyObservers() {for (Observer observer : observers) {observer.update(activity);}} }public class Mom implements Observer {@Overridepublic void update(String message) {System.out.println("Mom knows that the child is: " + message);} }

2、策略模式(Strategy):適應不同的環境!

策略模式就像孩子們會根據不同的環境選擇不同的玩法。Java代碼示例:

public interface PlayStrategy {void play(); }public class IndoorPlayStrategy implements PlayStrategy {public void play() {System.out.println("Playing indoors!");} }public class OutdoorPlayStrategy implements PlayStrategy {public void play() {System.out.println("Playing outdoors!");} }public class Child {private PlayStrategy playStrategy;public void setPlayStrategy(PlayStrategy playStrategy) {this.playStrategy = playStrategy;}public void play() {playStrategy.play();} }

3、責任鏈模式(Chain of Responsibility):家庭責任制!

責任鏈模式就像家庭責任制,孩子們可以按照家庭規矩來處理問題。Java代碼示例:

public abstract class Handler {protected Handler nextHandler;public void setNextHandler(Handler nextHandler) {this.nextHandler = nextHandler;}public abstract void handleRequest(String request); }public class Father extends Handler {@Overridepublic void handleRequest(String request) {if ("homework".equals(request)) {System.out.println("Father: I'll help with the homework.");} else if (nextHandler != null) {nextHandler.handleRequest(request);}} }public class Mother extends Handler {@Overridepublic void handleRequest(String request) {if ("housework".equals(request)) {System.out.println("Mother: I'll help with the housework.");} else if (nextHandler != null) {nextHandler.handleRequest(request);}} }

以上就是創建型、結構型、行為型設計模式的概覽,希望通過這些生動、幽默的例子能讓你對設計模式有更深入的了解。設計模式的運用能讓你的代碼更加優雅、易讀,讓軟件開發變得更加輕松愉快!

總結

以上是生活随笔為你收集整理的四:轻松学设计模式:创建型、结构型、行为型概览的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。