七. 设计模式
23种设计模式
- 创建型模式:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
- 结构型模式:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
- 行为型模式:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
设计模式六大原则:
-
单一职责原则:一个类或接口只承担一个职责。
-
里氏替换原则:在继承类时,务必重写(override)父类中所有的方法,尤其需要注意父类的protected方法(它们往往是让你重写的),子类尽量不要暴露自己的public方法供外界调用。
-
依赖倒置原则:高层模块不应该依赖于低层模块,而应该依赖于抽象。抽象不应依赖于细节,细节应依赖于抽象。
-
接口隔离原则:不要对外暴露没有实际意义的接口。
-
迪米特法则:尽量减少对象之间的交互,从而减小类之间的耦合。
-
开闭原则:对软件实体的改动,最好用扩展而非修改的方式。
参考:
设计模式六大原则
创建型模式
一 抽象工厂模式
定义了一个接口用于创建相关或有依赖关系的对象族,而无需明确指定具体类
生产电脑的抽象工厂接口,工厂A,工厂B分别实现抽象工厂
//定义抽象工厂接口,生产CPU,GPU对象 public interface AbComputerFactory { CPU createCpu(); GPU createGpu(); } //A工厂生产英特尔CPU,英伟达显卡 public class AFactory implements AbComputerFactory{ @Override public CPU createCpu() { return new IntelCPU(); } @Override public GPU createGpu() { return new nvdiaGPU(); } } //B工厂生产高通,AMD public class BFactory implements AbComputerFactory { @Override public CPU createCpu() { return new gaotongCPU(); } @Override public GPU createGpu() { return new amdGPU(); } }
定义电脑零件CPU,GPU 接口,并实现不同类型
public interface CPU { void cpuName(); } public interface GPU { void gpuName(); } public class IntelCPU implements CPU { @Override public void cpuName() { System.out.println("英特尔CPU"); } } public class gaotongCPU implements CPU{ @Override public void cpuName() { System.out.println("高通CPU"); } } public class nvdiaGPU implements GPU { @Override public void gpuName() { System.out.println("英伟达显卡"); } } public class amdGPU implements GPU { @Override public void gpuName() { System.out.println("AMD显卡"); } }
具体工厂进行生产
public class Test { public static void main(String[] args) { //A工厂生产电脑 AbComputerFactory factory1 = new AFactory(); CPU C = factory1.createCpu(); GPU G = factory1.createGpu(); C.cpuName(); G.gpuName(); System.out.println("电脑" + C.toString() + G.toString()); } }
单例模式
确保一个类最多只有一个实例,并提供一个全局访问点
public class Singleton { private static Singleton instance = null; private Singleton() { System.out.println("构造方法私有,外界无法实例化"); } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } //测试,使用getInstance方法获取单例对象 public class test { public static void main(String[] args) { Singleton.getInstance(); } }
生成器模式
分步骤创建复杂对象。 该模式允许你使用相同的创建代码生成不同类型和形式的对象。
//需要创建的复杂对象,Computer 类 public class Computer { public String Master; public String Screen; public String Keyboard; public String Mouse; public String Audio; /** *忽略setter,getter; **/ } //抽象类生成器 ,ComputerBuilder public abstract class ComputerBuilder { protected Computer computer; //返回Computer对象的方法 public Computer getComputer(){ return new Computer(); } //抽象方法 public abstract ComputerBuilder buildComputer(); public abstract ComputerBuilder buildKeybord(); public abstract ComputerBuilder buildScream(); } //两个类实现ComputerBuilder ,分别实现戴尔电脑构建,惠普电脑构建 //生成不同类型和形式的对象 public class DellComputerBuilder extends ComputerBuilder { Computer computer = new Computer(); @Override public ComputerBuilder buildComputer() { System.out.println("生产戴尔电脑"); computer.setMaster("i7,16g,512SSD,3060"); System.out.println("(i7,16g,512SSD,3060)的戴尔主机"); computer.setMouse("戴尔 鼠标"); System.out.println("(戴尔 鼠标)的鼠标"); computer.setAudio("戴尔 音响"); System.out.println("(戴尔 音响)的音响"); return this; } @Override public Computer getComputer(){ return computer; } @Override public ComputerBuilder buildKeybord() { computer.setKeyboard("cherry 红轴机械键盘"); System.out.println("(cherry 红轴机械键盘)的键盘"); return this; } @Override public ComputerBuilder buildScream() { computer.setScreen("1080p"); System.out.println("(1080p)的戴尔显示屏"); return this; } } public class HPComputerBuilder extends ComputerBuilder{ Computer computer = new Computer(); @Override public ComputerBuilder buildComputer() { System.out.println("制造惠普电脑"); computer.setMaster("i7,16g,512SSD,1060"); System.out.println("(i7,16g,512SSD,1060)的惠普主机"); computer.setMouse("MI 鼠标"); System.out.println("(MI 鼠标)的鼠标"); computer.setAudio("飞利浦 音响"); System.out.println("(飞利浦 音响)的音响"); return this; } @Override public Computer getComputer(){ return computer; } @Override public ComputerBuilder buildKeybord() { computer.setKeyboard("cherry 青轴机械键盘"); System.out.println("(cherry 青轴机械键盘)的键盘"); return this; } @Override public ComputerBuilder buildScream() { computer.setScreen("1080p"); System.out.println("(1080p)的惠普显示屏"); return this; } } //使用dellComputerBuilder 生成戴尔电脑对象 DellComputerBuilder dellComputerBuilder = new DellComputerBuilder(); Computer computer = dellComputerBuilder.buildComputer().buildKeybord().buildScream().getComputer();
原型模式
通过复制现有实例来创建新的实例,无需知道相应类的信息。原型模式是一种创建型设计模式, 使你能够复制已有对象, 而又无需使代码依赖它们所属的类。
public interface MyColor extends Cloneable { Object clone(); void display(); } public class Red implements MyColor { @Override public Object clone() { Red r = null; try { r = (Red) super.clone(); } catch (Exception e) { } return r; } @Override public void display() { System.out.println("This is Red"); } } /** * 通过复制现有实例来创建新的实例,无需知道相应类的信息 * 可以解决构建复杂对象的资源消耗问题 * Cloneable 浅拷贝 */ public class TestProto { public static void main(String[] args) { Red red =new Red(); red.display(); Red Cred=(Red)red.clone(); Cred.display(); System.out.println(red==Cred); } }
结构型模式
适配器模式
适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。
装饰器模式
动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性。
//定义抽象类Drink public abstract class Drink { public String description; public int price; public String getDescription() { return description+"-"+this.getPrice(); } public void setDescription(String description) { this.description = description; } public int getPrice() { return price; } public void setPrice(int f) { this.price = f; } public abstract int cost(); } //Coffee类 继承Drink public class Coffee extends Drink { @Override public int cost() { return super.getPrice(); } } //Decaf 咖啡的子类 public class Decaf extends Coffee { public Decaf() { super.setDescription("Decaf"); super.setPrice(3); } } //定义Drink的装饰类 public class Decorator extends Drink{ //增加Drink 属性, private Drink Obj; public Decorator(Drink Obj) { this.Obj = Obj; }; @Override public int cost() { return super.getPrice() + Obj.cost(); } @Override public String getDescription(){ return super.description + "-" + super.getPrice() + "&&" + Obj.getDescription(); } } //装饰器的具体实现,(给饮料加牛奶) public class Milk extends Decorator { public Milk(Drink Obj) { super(Obj); super.setDescription("Milk"); super.setPrice(2); } } /** * 动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性。 * 装饰者和被装饰者之间必须是一样的类型,也就是要有共同的超类。 */ public class TestDecorator { public static void main(String[] args) { //下订单,买杯Decaf Drink order = new Decaf(); System.out.println("order1 price:" + order.cost()); System.out.println("order1 desc:" + order.getDescription());、 //咖啡再加牛奶 order = new Milk(order); System.out.println("order2 price:" + order.cost()); System.out.println("order2 desc:" + order.getDescription()); } }
代理模式
代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。通俗的来讲代理模式就是我们生活中常见的中介
//定义了买房子 interface BuyHouse{ void buyHouse(); } //买房子具体实现 public class BuyHouseImpl implements BuyHouse{ @Override public void buyHouse() { System.out.println("Buy house"); } } //通过代理类买房子 public class BuyHouseProxy implements BuyHouse { //代理买房子实现 BuyHouse buyHouse = new BuyHouseImpl(); public BuyHouseProxy (final BuyHouse buyHouse){ this.buyHouse=buyHouse; } @Override public void buyHouse() { System.out.println("买房前准备"); //买房子 buyHouse.buyHouse(); System.out.println("买房后装修"); } } public class TestProxy { public static void main(String[] args) { System.out.println("自己买房子:"); BuyHouse self = new BuyHouseImpl(); self.buyHouse(); System.out.println("通过中介,代理买房子"); BuyHouse proxy = new BuyHouseProxy(new BuyHouseImpl()); proxy.buyHouse(); } }
外观模式
隐藏了系统的复杂性,并向客户端提供了一个可以访问系统的接口。
电脑开关机为例,分别需要CPU,磁盘,内存开关
public class CPU { public void openCPU(){ System.out.println("open CPU"); } public void shutdownCPU(){ System.out.println("shutdown cpu"); } } public class Disk { public void openDisk(){ System.out.println("open disk"); } public void shutdownDisk(){ System.out.println("shutdown disk"); } } public class Memory { public void openMemory(){ System.out.println("open Memory"); } public void shutdownMemory(){ System.out.println("shutdown memory"); } }
使用Faced模式,开关电脑,调用者不需要知道子系统内部的构成,它只需要跟Facade类交互
public class ComputerFace { private CPU cpu; private Memory memory; private Disk disk; public ComputerFace() { cpu = new CPU(); disk = new Disk(); memory = new Memory(); } public void openComputer(){ System.out.println("Computer start begin"); cpu.openCPU(); disk.openDisk(); memory.openMemory(); System.out.println("Computer start end"); } public void shutdownComputer(){ System.out.println("Computer stop begin"); cpu.shutdownCPU(); disk.shutdownDisk(); memory.shutdownMemory(); System.out.println("Computer stop end"); } } /** * 把一些复杂的流程封装成一个接口供给外部用户更简单的使用。这个模式中 * 客户端根本不需要知道子系统内部的实现,或者根本不需要知道子系统内部的构成,它只需要跟Facade类交互即可 * 使得客户端和子系统之间解耦,让子系统内部的模块功能更容易扩展和维护 */ public class TestFacade { public static void main(String[] args) { ComputerFace computer = new ComputerFace(); computer.openComputer(); computer.shutdownComputer(); } }
桥接模式
将抽象部分与它的实现部分分离,使它们都可以独立地变化。
抽取其中一个维度并使之成为独立的类层次, 这样就可以在初始类中引用这个新层次的对象, 从而使得一个类不必拥有所有的状态和行为。
public abstract class Phone { //抽象类Phone,成员变量software(独立的类层次) protected software software; //安装软件 public void setSoftware(software software) { this.software = software; } //抽象方法run public abstract void run(); } public class Oppo extends Phone { //具体OPPO手机,实现run方法,运行软件 @Override public void run() { software.run(); } } //软件接口 public interface software { void run(); } //具体软件 public class Camera implements software { @Override public void run() { System.out.println("run camera"); } } public class AppStore implements software { @Override public void run() { System.out.println("run app store"); } } /** * 将抽象部分与它的实现部分分离,使它们都可以独立地变化 * */ public class BridageTest { public static void main(String[] args) { AppStore appStore = new AppStore(); Phone oppo = new Oppo(); oppo.setSoftware(appStore); oppo.run(); } }
组合模式
将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。
Composite ,Leaf 都是对Component 的实现、部分-整体 用户对单个对象和组合对象具有一致的访问性
public interface Component { public void add(Component c); public void remove(Component c); public Component getChild(int i); public void operation(); } /** * 树枝 */ public class Composite implements Component{ private ArrayList<Component> children = new ArrayList<Component>(); public void add(Component c) { children.add(c); } public void remove(Component c) { children.remove(c); } public Component getChild(int i) { return children.get(i); } public void operation() { for (Object obj : children) { ((Component) obj).operation(); } } } public class Leaf implements Component { private String name; public Leaf(String name) { this.name = name; } @Override public void add(Component c) {} @Override public void remove(Component c) {} @Override public Component getChild(int i) { return null; } @Override public void operation() { System.out.println("树叶"+name+":被访问!"); } } /** * 将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。 * */ public class ComponentTest { public static void main(String[] args) { Component leaf1 = new Leaf("yezi1"); Component leaf2 = new Leaf("yezi2"); Component shu = new Composite(); // leaf.operation(); shu.add(leaf1); shu.add(leaf2); shu.add(leaf2); shu.operation(); } }
享元模式
享元模式是一种结构型设计模式, 它摒弃了在每个对象中保存所有数据的方式, 通过共享多个对象所共有的相同状态, 让你能在有限的内存容量中载入更多对象。
public interface IFlyweight { void print(); } public class Flyweight implements IFlyweight { private String id; public Flyweight(String id){ this.id = id; } @Override public void print() { System.out.println("Flyweight.id = " + getId() + " ..."); } public String getId() { return id; } } public class FlyweightFactory { @SuppressWarnings({ "unchecked", "rawtypes" }) private Map<String, IFlyweight> flyweightMap = new HashMap(); public IFlyweight getFlyweight(String str){ IFlyweight flyweight = flyweightMap.get(str); if(flyweight == null){ flyweight = new Flyweight(str); flyweightMap.put(str, flyweight); } return flyweight; } public int getFlyweightMapSize(){ return flyweightMap.size(); } } /** * 在有大量对象时,有可能会造成内存溢出,我们把其中共同的部分抽象出来,如果有相同的业务请求,直接返回在内存中已有的对象,避免重新创建。 */ public class FlyweightTest { public static void main(String[] args) { //创建三个字符串,但是只会产生两个享元对象 FlyweightFactory flyweightFactory = new FlyweightFactory(); IFlyweight flyweight1 = flyweightFactory.getFlyweight("A"); IFlyweight flyweight2 = flyweightFactory.getFlyweight("B"); IFlyweight flyweight3 = flyweightFactory.getFlyweight("A"); flyweight1.print(); flyweight2.print(); flyweight3.print(); System.out.println(flyweightFactory.getFlyweightMapSize()); } }
行为型模式
策略模式
策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。
public interface Strategy { public int calc(int num1,int num2); } public class AddStrategy implements Strategy { @Override public int calc(int num1, int num2) { return num1+num2; } } public class SubstractStrategy implements Strategy { @Override public int calc(int num1, int num2) { return num1-num2; } } public class Environment { private Strategy strategy; public Environment(Strategy strategy) { this.strategy = strategy; } public int calculate(int a, int b) { return strategy.calc(a, b); } } /** * 策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户。 */ public class StrategyTest { public static void main(String[] args) { Environment test1 = new Environment(new AddStrategy()); System.out.println(test1.calculate(10, 11)); Environment test2 = new Environment(new SubstractStrategy()); System.out.println(test2.calculate(11,10)); } }
模板模式
定义一个操作中算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以不改变算法的结构即可重定义该算法的某些特定步骤。
抽象类,定义做饭的整个过程(模板) public abstract class Dish { /** * 具体的整个过程 */ protected void dodish(){ this.preparation(); this.doing(); this.carriedDishes(); } /** * 备料 */ public abstract void preparation(); /** * 做菜 */ public abstract void doing(); /** * 上菜 */ public abstract void carriedDishes (); } //继承Dish,参考模板进行实现 public class EggsWithTomato extends Dish{ @Override public void preparation() { System.out.println("洗并切西红柿,打鸡蛋。"); } @Override public void doing() { System.out.println("鸡蛋倒入锅里,然后倒入西红柿一起炒。"); } @Override public void carriedDishes() { System.out.println("将炒好的西红寺鸡蛋装入碟子里,端给客人吃。"); } } public class Bouilli extends Dish{ @Override public void preparation() { System.out.println("切猪肉和土豆。"); } @Override public void doing() { System.out.println("将切好的猪肉倒入锅中炒一会然后倒入土豆连炒带炖。"); } @Override public void carriedDishes() { System.out.println("将做好的红烧肉盛进碗里端给客人吃。"); } } /** * 定义一个操作中算法的骨架,而将一些步骤延迟到子类中,模板方法使得子类可以不改变算法的结构即可重定义该算法的某些特定步骤。 */ public class TemplateTest { public static void main(String[] args) { Dish eggTomato = new EggsWithTomato(); eggTomato.dodish(); System.out.println(eggTomato.getClass()); System.out.println("*************************"); Bouilli bouilli = new Bouilli(); bouilli.dodish(); } }
观察者模式
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
//观察者接口及实现 public interface Observer { public void update(String message); } public class User implements Observer { private String name; private String message; public User(String name) { this.name = name; } @Override public void update(String message) { this.message = message; read(); } public void read() { System.out.println(name + " 收到推送消息: " + message); } } // public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObserver(); } ublic class WechatServer implements Subject { private List<Observer> list; private String message; public WechatServer() { list = new ArrayList<Observer>(); } @Override public void registerObserver(Observer o) { list.add(o); } @Override public void removeObserver(Observer o) { if (!list.isEmpty()) { list.remove(o); } } @Override public void notifyObserver() { for (Observer o : list) { o.update(message); } } public void setInfomation(String s) { this.message = s; System.out.println("微信服务更新消息: " + s); // 消息更新,通知所有观察者 notifyObserver(); } } /** * 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。 * 一个对象状态改变给其他对象通知的问题,而且要考虑到易用和低耦合,保证高度的协作。 */ public class ObserverTest { public static void main(String[] args) { WechatServer server = new WechatServer(); Observer userZhang = new User("ZhangSan"); Observer userLi = new User("LiSi"); Observer userWang = new User("WangWu"); server.registerObserver(userZhang); server.registerObserver(userLi); server.registerObserver(userWang); server.setInfomation("PHP是世界上最好用的语言!"); System.out.println("----------------------------------------------"); server.removeObserver(userZhang); server.setInfomation("JAVA是世界上最好用的语言!"); } }
迭代器模式
提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示。
迭代器模式是一种行为设计模式, 让你能在不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素。
责任链模式
如果有多个对象有机会处理请求,责任链可使请求的发送者和接受者解耦,请求沿着责任链传递,直到有一个对象处理了它为止。
public interface Handler { // 返回Boolean.TRUE = 成功 // 返回Boolean.FALSE = 拒绝 // 返回null = 交下一个处理 Boolean process(Request request); } @Override public Boolean process(Request request) { // 如果超过1000元,处理不了,交下一个处理: if (request.getAmount()> 1000) { return null; } // 对Bob有偏见: return !request.getName().equalsIgnoreCase("bob"); } } public class DirectorHandler implements Handler{ @Override public Boolean process(Request request) { // 如果超过2000元,处理不了,交下一个处理: if (request.getAmount()>2000) { return null; } // 对Bob有偏见: return !request.getName().equalsIgnoreCase("bob"); } } public class DirectorHandler implements Handler{ @Override public Boolean process(Request request) { // 如果超过3000元,处理不了,交下一个处理: if (request.getAmount()>3000) { return null; } // 对Bob有偏见: return !request.getName().equalsIgnoreCase("bob"); } } public class HandlerChain { // 持有所有Handler: private List<Handler> handlers = new ArrayList<>(); public void addHandler(Handler handler) { this.handlers.add(handler); } public boolean process(Request request) { // 依次调用每个Handler: for (Handler handler : handlers) { Boolean r = handler.process(request); if (r != null) { // 如果返回TRUE或FALSE,处理结束: System.out.println(request + " " + (r ? "Approved by " : "Denied by ") + handler.getClass().getSimpleName()); return r; } } throw new RuntimeException("Could not handle request: " + request); } } //处理请求类 public class Request { private String name; private int amount; public Request(String name, int amount) { this.name = name; this.amount = amount; } public String getName() { return name; } public int getAmount() { return amount; } } /** * 责任链可使请求的发送者和接受者解耦,请求沿着责任链传递,直到有一个对象处理了它为止 * 在处理消息的时候以过滤很多道 */ public class ChainTest { public static void main(String[] args) { // 构造责任链: HandlerChain chain = new HandlerChain(); chain.addHandler(new ManagerHandler()); chain.addHandler(new DirectorHandler()); chain.addHandler(new CEOHandler()); // 处理请求: chain.process(new Request("Bob", 123)); // chain.process(new Request("Alice", 12345)); // chain.process(new Request("Bill", 123456)); // chain.process(new Request("John", 234566)); } }
命令模式
将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理。
public interface Command { public void excute(); public void undo(); } public class Control { public void CommandExcute(Command command) { command.excute(); } public void CommandUndo(Command command) { command.undo(); } } public class Light { String loc = ""; public Light(String loc) { this.loc = loc; } public void On() { System.out.println(loc + " On"); } public void Off() { System.out.println(loc + " Off"); } } public class TurnOffLight implements Command { private Light light; public TurnOffLight(Light light) { this.light = light; } @Override public void excute() { light.Off(); } @Override public void undo() { light.On(); } } /** * 将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。 * 这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理。 */ public class CommandTest { public static void main(String[] args) { Control control = new Control(); TurnOffLight turnOffLight = new TurnOffLight(new Light("green light")); control.CommandExcute(turnOffLight); } }
状态模式
在状态模式中,我们创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象。
状态模式是一种行为设计模式, 让你能在一个对象的内部状态变化时改变其行为, 使其看上去就像改变了自身所属的类一样。
public interface State { public void stop(); public void move(); } public class PlaceA implements State{ private Player context; public PlaceA(Player context) { this.context = context; } @Override public void move() { System.out.println("处于地点A,开始向B移动"); System.out.println("--------"); context.setDirection("AB"); context.setState(context.onMove); } @Override public void stop() { System.out.println("正处在地点A,不用停止移动"); System.out.println("--------"); } } public class PlaceB implements State { private Player context; public PlaceB(Player context) { this.context = context; } @Override public void move() { System.out.println("处于地点A,开始向B移动"); System.out.println("--------"); context.setDirection("AB"); context.setState(context.onMove); } @Override public void stop() { // TODO Auto-generated method stub System.out.println("正处在地点A,不用停止移动"); System.out.println("--------"); } } public class Player { State placeA; State placeB; State onMove; private State state; private String direction; public Player() { direction = "AB"; placeA = new PlaceA(this); placeB = new PlaceB(this); //onMove = new OnMove(this); this.state = placeA; } public void move() { System.out.println("指令:开始移动"); state.move(); } public void stop() { System.out.println("指令:停止移动"); state.stop(); } public State getState() { return state; } public void setState(State state) { this.state = state; } public void setDirection(String direction) { this.direction = direction; } public String getDirection() { return direction; } } /** * 创建表示各种状态的对象和一个行为随着状态对象改变而改变的 context 对象 */ public class ContextTest { public static void main(String[] args) { Player player = new Player(); player.move(); } }
备忘录模式
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。
public interface MementoIF { String getState(); } public class Memento implements MementoIF { private String state; public Memento(String state) { this.state = state; } public String getState(){ return state; } } public class Originator { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento saveToMemento() { return new Memento(state); } public String getStateFromMemento(MementoIF memento) { return ((Memento) memento).getState(); } } public class CareTaker { private List<MementoIF> mementoList = new ArrayList<MementoIF>(); public void add(MementoIF memento) { mementoList.add(memento); } public MementoIF get(int index) { return mementoList.get(index); } public void getStateFromMemento(MementoIF mem) { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'getStateFromMemento'"); } } public class MementoTest{ public static void main(String[] args) { Originator originator= new Originator(); originator.setState("state1"); // originator.saveToMemento(); CareTaker careTaker = new CareTaker(); careTaker.add(originator.saveToMemento()); MementoIF mem= careTaker.get(0); careTaker.getStateFromMemento(mem); // mem.getState(); //((Originator) mem).getState(); } }
访问者模式
将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式。它将对数据的操作与数据结构进行分离。
public interface Visitor { abstract public void Visit(Element element); } public class CompensationVisitor implements Visitor { @Override public void Visit(Element element) { Employee employee = ((Employee) element); System.out.println( employee.getName() + "'s Compensation is "); } } public interface Element { abstract public void Accept(Visitor visitor); } public class Employee { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public String getVacationDays() { return vacationDays; } public void setVacationDays(String vacationDays) { this.vacationDays = vacationDays; } String degree; String vacationDays; public void Accept(Visitor visitor) { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'Accept'"); } } public class ObjectStructure { private HashMap<String, Employee> employees; @SuppressWarnings({ "rawtypes", "unchecked" }) public ObjectStructure() { employees = new HashMap(); } public void Attach(Employee employee) { employees.put(employee.getName(), employee); } @SuppressWarnings("unlikely-arg-type") public void Detach(Employee employee) { employees.remove(employee); } public Employee getEmployee(String name) { return employees.get(name); } public void Accept(Visitor visitor) { for (Employee e : employees.values()) { e.Accept(visitor); } } } public class Employee { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDegree() { return degree; } public void setDegree(String degree) { this.degree = degree; } public String getVacationDays() { return vacationDays; } public void setVacationDays(String vacationDays) { this.vacationDays = vacationDays; } String degree; String vacationDays; public void Accept(Visitor visitor) { // TODO Auto-generated method stub throw new UnsupportedOperationException("Unimplemented method 'Accept'"); } }
中介者模式
定义一个中介对象来封装一系列对象之间的交互,使原有对象之间的耦合松散,且可以独立地改变它们之间的交互。中介者模式又叫调停模式,它是迪米特法则的典型应用
public interface Mediator { void register(Colleague colleague); // 客户注册 void replay(String from, String to,String ad); // 转发 } public class ConcreteMediator implements Mediator { private List<Colleague> colleagues = new ArrayList<Colleague>(); @Override public void register(Colleague colleague) { if (!colleagues.contains(colleague)) { colleagues.add(colleague); colleague.setMedium(this); } } @Override public void replay(String from, String to, String ad) { for (Colleague cl : colleagues) { String name = cl.getName(); if (name.equals(to)) { cl.receive(from, ad); } } } } public abstract class Colleague { protected Mediator mediator; protected String name; public Colleague(String name) { this.name = name; } public void setMedium(Mediator mediator) { this.mediator = mediator; } public String getName() { return name; } public abstract void Send(String to, String ad); public abstract void receive(String from, String ad); } public class Buyer extends Colleague { public Buyer(String name) { super(name); } @Override public void Send(String to, String ad) { mediator.relay(name, to, ad); } @Override public void receive(String from, String ad) { System.out.println(name + "接收到来自" + from + "的消息:" + ad); } } public class MediatorTest { public static void main(String[] args) { Mediator foo = new ConcreteMediator(); Colleague co = new Buyer("buyer1"); Colleague co2 = new Buyer("buyer2"); foo.register(co); foo.register(co2); foo.relay("buyer1", "buyer2", "lalalala"); } }
refer
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验