设计模式(4)
设计模式(4)
jit-hakase 发表于4个月前
设计模式(4)
  • 发表于 4个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 行为型

设计模式

行为型模式

策略模式(strategy)

策略模式多用在算法决策系统中, 外部用户只需要决定用哪个算法即可.

interface Algorithm {
	void exec();
}

class Dijkstra implements Algorithm {

	@Override
	public void exec() {
		System.out.println("dijkstra algorithm...");
	}
}

class Floyd implements Algorithm {

	@Override
	public void exec() {
		System.out.println("floyd algorithm...");
	}
	
}

public class Main {
	
	public static void main(String[] args) {
		
		Algorithm algo = new Dijkstra();
		algo.exec();
	}
}

中介者模式(Mediator)

使用这种模式能够减少对象两两之间复杂的引用关系, 使之成为一个松耦合的系统.

与Facade不同的是, Facade是单向的, 而Mediator是双向的.

interface Hardware {  
	void startup();
	void shutdown();
}

class CPU implements Hardware {
	
	private Mediator mediator;
	
	public CPU(Mediator mediator) {
		this.mediator = mediator;
	}
	
	public Mediator getMediator() {
		return mediator;
	}
	
	@Override
	public void startup() {
		System.out.println("CPU startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("CPU shutdown...");
	}
}

class Memory implements Hardware {
	
	private Mediator mediator;
	
	public Memory(Mediator mediator) {
		this.mediator = mediator;
	}
	
	public Mediator getMediator() {
		return mediator;
	}
	
	@Override
	public void startup() {
		System.out.println("memory startup...");
	}
	@Override
	public void shutdown() {
		System.out.println("memory shutdown...");
	}
}

class Mediator {
	
	private CPU cpu;
	private Memory memory;
	
	public void createMediator() {
		cpu = new CPU(this);
		memory = new Memory(this); 
	}
	
	public void startup() {
		cpu.startup();
		memory.startup();
	}
	
	public void shutdown() {
		memory.shutdown();
		cpu.shutdown();
	}

	public CPU getCpu() { return cpu; }
	public Memory getMemory() { return memory; }
}

public class Main {
	
	public static void main(String[] args) {
		
		Mediator mediator = new Mediator();
		mediator.createMediator();
		
		mediator.startup();
		mediator.shutdown();
	}
}

责任链模式(Chain of Responsibility)

每个对象持有对下一个对象的引用, 这样就会形成一条链, 请求在这条链上传递, 直到某一对象决定处理该请求. 但发出者并不清楚到底最终那个对象会处理该请求, 所以在隐瞒客户端的情况下, 可以对系统进行动态调整. 链接上的请求可以是链和树, 还可以是一个环, 命令只允许由一个对象传给另一个对象, 而不允许传给多个对象.

class Handler {
	
	private String operator;
	private Handler handler;
	
	public Handler(String operator) {
		this.operator = operator;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}
	
	public void operate() {
		System.out.println(operator + " operate...");
		if (handler != null) {
			handler.operate();
		}
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		Handler startupHandler = new Handler("startup");
		Handler actionHandler = new Handler("action");
		Handler shutdownHandler = new Handler("shutdown");
		
		startupHandler.setHandler(actionHandler);
		actionHandler.setHandler(shutdownHandler);
		
		startupHandler.operate();
	}
}

模板方法模式(Template method)

定义一个操作中的算法的骨架, 而将一些步骤延迟到子类中.

abstract class OS {
	
	public final void process() {
		startup();
		loadCore();
		shutdown();
	}
	
	private void startup() {
		System.out.println("startup...");
	}
	
	public abstract void loadCore();
	
	private void shutdown() {
		System.out.println("shutdown...");
	}
}

class Linux extends OS {
	@Override
	public void loadCore() {
		System.out.println("load linux-core...");	
	}
}

class Windows extends OS {
	@Override
	public void loadCore() {
		System.out.println("load windows-core...");	
	}
}

public class Main {
	
	public static void main(String[] args) {
      
		OS os = new Linux();
		os.process();
	}
}

观察者模式(Observer)

定义对象间的一种一对多的依赖关系, 当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新, 常用于事件触发.

import java.util.ArrayList;
import java.util.List;

interface Observer {
	void update();
}

class MouseObserver implements Observer {
	@Override
	public void update() {
		System.out.println("update mouse observer");
	}
}

class KeyboardObserver implements Observer {
	@Override
	public void update() {
		System.out.println("update keyboard observer");
	}
}

interface Subject {
	void add(Observer observer);
	void remove(Observer observer);
	void notifyObservers();
	void operate();
}

abstract class AbstractSubject implements Subject {
  
	private List<Observer> observers = new ArrayList<>();
	
	@Override
	public void add(Observer observer) {
		observers.add(observer);
	}
	@Override
	public void remove(Observer observer) {
		observers.remove(observer);
	}
	@Override
	public void notifyObservers() {
		for (Observer ob : observers) {
			ob.update();
		}
	}
}

class Game extends AbstractSubject {
	@Override
	public void operate() {
		System.out.println("notify observers...");
		super.notifyObservers();
	}
}

public class Main {
  
	public static void main(String[] args) {
		
		Game game = new Game();
		
		game.add(new MouseObserver());
		game.add(new KeyboardObserver());
		
		game.operate();
	}
}

访问者模式(Visitor)

访问者模式适用于数据结构相对稳定的系统, 能解除数据结构和算法的耦合性.

不同身份访问不同对象的例子

interface Visitor {
	void visit(Subject subject);
}

class AdminVisitor implements Visitor {
	@Override
	public void visit(Subject subject) {
		System.out.println("admin visit: " + subject.getName());
	}
}

class GuestVisitor implements Visitor {
	@Override
	public void visit(Subject subject) {
		if (subject.getName().equals("admin")) {
			System.out.println("deny...");
		} else {
			System.out.println("guest visit: " + subject.getName());
		}
	}
}

interface Subject {
	void accept(Visitor visitor);
	String getName();
}

class GuestSubject implements Subject {
	@Override
	public String getName() {
		return "guest";
	}
	@Override
	public void accept(Visitor visitor) {
		visitor.visit(this);
	}
}

class AdminSubject implements Subject {
	@Override
	public String getName() {
		return "admin";
	}
	@Override
	public void accept(Visitor visitor) {
		visitor.visit(this);
	}
}

public class Main {
	
	public static void main(String[] args) {
		
		Subject adminSubject = new AdminSubject();
		Subject guestSubject = new GuestSubject();
		
		Visitor adminVisitor = new AdminVisitor();
		Visitor guestVisitor = new GuestVisitor();
		
		adminSubject.accept(adminVisitor);
		adminSubject.accept(guestVisitor);
		
		guestSubject.accept(adminVisitor);
		guestSubject.accept(guestVisitor);
	}
}
  • 点赞
  • 收藏
  • 分享
粉丝 0
博文 23
码字总数 29680