簡(jiǎn)介:
本文是個(gè)系列一次會(huì)出兩個(gè)設(shè)計(jì)者模式作用,如果有關(guān)聯(lián)就三個(gè),除此外還會(huì)講解在spring中作用。
23設(shè)計(jì)者模式以及重點(diǎn)模式
我們都知道設(shè)計(jì)者模式有3類23種設(shè)計(jì)模式,標(biāo)紅是特別重要的設(shè)計(jì)者模式建議都會(huì),而且熟讀于心,標(biāo)藍(lán)是指其次重要建議也要明白。
(1)創(chuàng)建型模式:單例模式、抽象工廠模式、構(gòu)建者模式、工廠模式、原型模式。
(2)結(jié)構(gòu)型模式:適配器模式、橋接模式、裝飾模式、組合模式、外觀模式、享元模式、代理模式。
(3)行為型模式:訪問者模式、模版方法模式、命令模式、迭代器模式、觀察者模式、中介者模式、備忘錄模式、解釋器模式、狀態(tài)模式、策略模式、職責(zé)鏈模式。
命令模式(Command Pattern)
命令模式是一種行為型設(shè)計(jì)模式,它將請(qǐng)求或簡(jiǎn)單的操作封裝成一個(gè)對(duì)象。這種模式的主要目的是將請(qǐng)求發(fā)送者與接收者解耦,使得發(fā)送者和接收者不直接交互。命令模式允許將請(qǐng)求排隊(duì)處理、記錄請(qǐng)求日志以及實(shí)現(xiàn)可撤銷的操作。
組成部分:
- 命令接口(Command):定義執(zhí)行操作的接口。
- 具體命令(ConcreteCommand):實(shí)現(xiàn)命令接口的類,定義了接收者和動(dòng)作之間的綁定關(guān)系。
- 接收者(Receiver):知道如何實(shí)施和執(zhí)行一個(gè)請(qǐng)求相關(guān)的操作。
- 調(diào)用者(Invoker):要求命令對(duì)象執(zhí)行請(qǐng)求。
- 客戶端(Client):創(chuàng)建具體命令對(duì)象,并設(shè)置其接收者。
命令模式 主要用于將操作的請(qǐng)求者和執(zhí)行者解耦,允許將請(qǐng)求封裝成對(duì)象,從而使用不同的請(qǐng)求、隊(duì)列或日志參數(shù)化其他對(duì)象,并支持可撤銷的操作。
示例
// 命令接口
interface Command {
void execute();
}
// 接收者
class Light {
public void turnOn() {
System.out.println("The light is on");
}
public void turnOff() {
System.out.println("The light is off");
}
}
// 具體命令
class TurnOnCommand implements Command {
private Light light;
public TurnOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOn();
}
}
class TurnOffCommand implements Command {
private Light light;
public TurnOffCommand(Light light) {
this.light = light;
}
public void execute() {
light.turnOff();
}
}
// 調(diào)用者
class RemoteControl {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public void pressButton() {
command.execute();
}
}
// 客戶端
public class CommandPatternDemo {
public static void main(String[] args) {
Light light = new Light();
Command turnOn = new TurnOnCommand(light);
Command turnOff = new TurnOffCommand(light);
RemoteControl control = new RemoteControl();
control.setCommand(turnOn);
control.pressButton();
control.setCommand(turnOff);
control.pressButton();
}
}
????????在這個(gè)例子中,Light
是一個(gè)接收者,TurnOnCommand
和 TurnOffCommand
是具體命令,它們實(shí)現(xiàn)了 Command
接口。RemoteControl
是調(diào)用者,它通過 pressButton
方法來執(zhí)行命令。客戶端代碼創(chuàng)建了命令對(duì)象并將其與接收者關(guān)聯(lián)。
迭代器模式(Iterator Pattern)
迭代器模式是一種行為型設(shè)計(jì)模式,它提供一種方法來順序訪問聚合對(duì)象中的各個(gè)元素,而又無需暴露該對(duì)象的內(nèi)部表示。
組成部分:
- 迭代器接口(Iterator):定義訪問和遍歷元素的接口。
- 具體迭代器(ConcreteIterator):實(shí)現(xiàn)迭代器接口,負(fù)責(zé)管理當(dāng)前元素的位置,并能夠計(jì)算出下一個(gè)元素。
- 聚合接口(Aggregate):定義創(chuàng)建相應(yīng)迭代器對(duì)象的接口。
- 具體聚合(ConcreteAggregate):實(shí)現(xiàn)聚合接口,返回一個(gè)具體迭代器的實(shí)例。
迭代器模式 用于順序訪問集合對(duì)象的元素,而不需要暴露其底層表示,提供了一種統(tǒng)一的方法來遍歷各種類型的集合。文章來源:http://www.zghlxwxcb.cn/news/detail-821094.html
示例
import java.util.*;
// 迭代器接口
interface Iterator {
boolean hasNext();
Object next();
}
// 聚合接口
interface Container {
Iterator getIterator();
}
// 具體聚合
class NameRepository implements Container {
public String names[] = {"John", "Doe", "Jane", "Smith"};
@Override
public Iterator getIterator() {
return new NameIterator();
}
private class NameIterator implements Iterator {
int index;
@Override
public boolean hasNext() {
return index < names.length;
}
@Override
public Object next() {
if (this.hasNext()) {
return names[index++];
}
return null;
}
}
}
// 客戶端
public class IteratorPatternDemo {
public static void main(String[] args) {
NameRepository namesRepository = new NameRepository();
for (Iterator iter = namesRepository.getIterator(); iter.hasNext();) {
String name = (String) iter.next();
System.out.println("Name : " + name);
}
}
}
????????在這個(gè)示例中,NameRepository
是一個(gè)具體聚合,它實(shí)現(xiàn)了 Container
接口,并內(nèi)部定義了 NameIterator
,這是一個(gè)具體的迭代器實(shí)現(xiàn)??蛻舳送ㄟ^ NameRepository
的 getIterator
方法獲取迭代器,并使用它來遍歷名稱。文章來源地址http://www.zghlxwxcb.cn/news/detail-821094.html
到了這里,關(guān)于Java 設(shè)計(jì)者模式以及與Spring關(guān)系(七) 命令和迭代器模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!