前言
这是给自己看的
本章可以下载word
文档进行打印: https://www.aliyundrive.com/s/hVijAWNNL1W
感谢gjx,yfm,hyt等同学的贡献
原则
设计模式的七大原则
开闭原则
开闭原则的意思是:对扩展开放,对修改关闭。在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果。简言之,是为了使程序的扩展性好,易于维护和升级。想要达到这样的效果,我们需要使用接口和抽象类,后面的具体设计中我们会提到这点。
关键点:无法修改自身,但是可以进行拓展。例子:继承
里氏代换原则
里氏代换原则是面向对象设计的基本原则之一。 里氏代换原则中说,任何基类可以出现的地方,子类一定可以出现。LSP 是继承复用的基石,只有当派生类可以替换掉基类,且软件单位的功能不受到影响时,基类才能真正被复用,而派生类也能够在基类的基础上增加新的行为。里氏代换原则是对开闭原则的补充。实现开闭原则的关键步骤就是抽象化,而基类与子类的继承关系就是抽象化的具体实现,所以里氏代换原则是对实现抽象化的具体步骤的规范。
继承重写方法,子类可以额外添加点东西
依赖倒转原则
这个原则是开闭原则的基础,具体内容:针对接口编程,依赖于抽象而不依赖于具体。
电脑类不再依赖于具体的键盘类,而是依赖于抽象接口
接口隔离原则
这个原则的意思是:使用多个隔离的接口,比使用单个接口要好。它还有另外一个意思是:降低类之间的耦合度。由此可见,其实设计模式就是从大型软件架构出发、便于升级和维护的软件设计思想,它强调降低依赖,降低耦合。
接口隔离原则要求将庞大臃肿的接口拆分为更小、更具体的接口,以便客户端只需了解和依赖于自己需要的接口,而不需要了解和依赖于多余的接口。
迪米特法则(最少知道原则)
最少知道原则是指:一个实体应当尽量少地与其他实体之间发生相互作用,使得系统功能模块相对独立。
迪米特法则的目的是为了减少类之间的耦合度,使得系统的各个模块之间相互独立,修改一个模块时不会影响到其他模块。这样可以提高系统的可维护性、可扩展性和可测试性。
合成复用原则
合成复用原则是指:尽量使用合成/聚合的方式,而不是使用继承。
合成复用原则的核心思想是:在设计系统时,应优先使用对象组合(composition)或聚合(aggregation)来实现复用,而不是通过继承来实现。
单一职责原则
一个对象应该只包含单一的职责,并且该职责被完整地封装在一个类中,另外一种定义:就一个类而言,应该仅有一个引起它变化的原因。
单一职责原则的核心思想是将一个类的功能和责任限定在一个领域或关注点上,这样可以提高类的内聚性和可维护性,并降低类之间的耦合度。当一个类承担过多 的责任时,它变得复杂而脆弱,容易受到变化的影响,导致代码难以理解、修改和维护。
考试重点
P90-2
public interface autocar { public void yunxing(); } public class Benzautocar implements autocar{ public void yunxing() { System.out.println("Benz汽车运行"); } } public class BMWautocar implements autocar{ public void yunxing() { System.out.println("BMW汽车运行"); } } public interface autocarFactory { public autocar productautocar(); } public class BenzautocarFactory implements autocarFactory{ public autocar productautocar(){ System.out.println("Benz汽车工厂生产的Benz汽车"); return new Benzautocar(); } } public class BMWautocarFactory implements autocarFactory{ public autocar productautocar(){ System.out.println("BMW汽车工厂生产的BMW汽车"); return new BMWautocar(); } } public class Client { public static void main(String[] args) { autocarFactory autocarfactory=new BenzautocarFactory(); autocar autocar=autocarfactory.productautocar(); autocar.yunxing(); } }
|
P399-策略模式-旅游出行
public interface TravelStrategy { void travel(); }
public class CarStrategy implements TravelStrategy { @Override public void travel() { System.out.println("使用汽车出行"); } }
public class AirplaneStrategy implements TravelStrategy { @Override public void travel() { System.out.println("使用飞机出行"); } }
public class WalkStrategy implements TravelStrategy { @Override public void travel() { System.out.println("徒步旅行"); } }
public class TravelStrategyFactory { public static TravelStrategy getTravelStrategy(String strategyType) { if (strategyType.equals("car")) { return new CarStrategy(); } else if (strategyType.equals("airplane")) { return new AirplaneStrategy(); } else if (strategyType.equals("walk")) { return new WalkStrategy(); } } }
public class Main { public static void main(String[] args) { TravelStrategy travelStrategy = TravelStrategyFactory.getTravelStrategy("car"); travelStrategy.travel(); travelStrategy = TravelStrategyFactory.getTravelStrategy("walk"); travelStrategy.travel(); travelStrategy = TravelStrategyFactory.getTravelStrategy("airplane"); travelStrategy.travel(); } }
|
工厂方法模式实例之日志记录器P91-3
public interface LogFactory { public Log createLog(); }
public interface Log { public void writeLog(); }
public class DatabaseLog implements Log { public void writeLog() { System.out.println("写数据日志"); } }
public class DatabaseLogFactory implements LogFactory { public Log createLog() { System.out.println("创建数据文件"); return new DatabaseLog(); } }
public class FileLog implements Log { public void writeLog() { System.out.println("写日志"); } }
public class FileLogFactory implements LogFactory { public Log createLog() { System.out.println("创建新的日志文件"); return new FileLog(); } }
public class LogTest { public static void main(String[] args) { Log log; LogFactory factory; factory = new FileLogFactory(); log = factory.createLog(); log.writeLog(); factory = new DatabaseLogFactory(); log = factory.createLog(); log.writeLog(); } }
|
结果:创建新的日志文件
写日志
创建数据文件
写数据日志
数据库操作工厂 P105-1
public class Client { public static void main(String[] args) {
DatabaseFactory databasefactory = new MysqlDatabaseFactory(); Connection connection; Statement statement; connection = databasefactory.produceConnection(); connection.connect(); statement = databasefactory.produceStatement(); statement.execute(); } }
public interface Connection { public void connect(); }
public interface DatabaseFactory { Connection produceConnection(); Statement produceStatement(); }
public class MysqlConnection implements Connection { public void connect() { System.out.println("Mysql connect success"); } }
public class MysqlDatabaseFactory implements DatabaseFactory { public Connection produceConnection() { return new MysqlConnection(); }
public Statement produceStatement() { return new MysqlStatement(); } }
public class MysqlStatement implements Statement { public void execute() { System.out.println("Mysql statement execute success"); } }
public class OracleConnection implements Connection { public void connect() { System.out.println("Oracle connect success"); } }
public class OracleDatabaseFactory implements DatabaseFactory { public Connection produceConnection() { return new OracleConnection(); }
public Statement produceStatement() { return new OracleStatement(); } } public class OracleStatement implements Statement { public void execute() { System.out.println("Oracle statement execute success"); } }
public interface Statement { public void execute(); }
|
结果:Mysql connect success
Mysql statement execute success
P105-2 抽象工厂模式
谢谢hyt同学的贡献
public interface CPU { public void dispose(); }
public class MacCPU implements CPU { public void dispose() { System.out.println("MacCPU正在处理信息"); } }
public class PCCPU implements CPU { public void dispose() { System.out.println("PCCPU正在处理信息"); } }
public interface RAM { public void dispose(); }
public class MacRAM implements RAM { public void store() { System.out.println("MacRAM正在存储信息.."); } }
public class PCRAM implements RAM { public void store() { System.out.println("PCRAM正在存储信息.."); } }
public interface CFactory { public CPU produceCPU();
public RAM produceRAM(); }
public class PCFactory implements CFactory { public CPU produceCPU() { return new PCCPU(); }
public RAM produceRAM() { return new PCRAM(); } }
public class MacFactory implements CFactory{ public CPU produceCPU() { return new MacCPU(); }
public RAM produceRAM() { return new MacRAM(); } }
public class Client { public static void main(String[] args) { CFactory factory; CPU cpu; RAM ram; factory = new MacFactory(); cpu = factory.produceCPU(); cpu.dispose(); ram = factory.produceRAM(); ram.store(); } }
|
P167-3 双向适配器模式
public interface RAM { public void store(); } public interface Cat { public void catchmouse();
public void Catbehaviours(); }
public interface Dog { public void cry();
public void Dogbehaviours(); }
public class Icat implements Cat { public void Catbehaviours() { System.out.println("我是猫,爱吃鱼"); }
public void catchmouse() { System.out.println("会捉老鼠"); } }
public class Idog implements Dog { public void Dogbehaviours() { System.out.println("我是狗,会啃骨头"); }
public void cry() { System.out.println("会狗叫"); } }
public class ChangeBodyadapter implements Cat, Dog { Cat cat = null;
public ChangeBodyadapter(Cat cat) { this.cat = cat; }
public void Dogbehaviours() { System.out.println("我也是一只狗,会啃骨头"); }
public void cry() { cat.catchmouse(); }
Dog dog = null;
public ChangeBodyadapter(Dog dog) { this.dog = dog; }
public void Catbehaviours() { System.out.println("我也是一只猫,爱吃鱼"); }
public void catchmouse() { dog.cry(); } }
public class Client { public static void main(String[] args) { Cat JCat = new ChangeBodyadapter(new Idog()); JCat.Catbehaviours(); JCat.catchmouse(); Dog JDog = new ChangeBodyadapter(new Icat()); JDog.Dogbehaviours(); JDog.cry(); } }
|
P173 例子:毛笔
public interface Color { void bepaint(String penType, String name); }
public class BigPen extends Pen{ public void draw(String name){ String penType="大号毛笔绘制"; this.color.bepaint(penType,name); } }
public class Black implements Color{ public void bepaint(String penType,String name){ System.out.println(penType + "黑色的"+ name + "."); } }
public class Blue implements Color{ public void bepaint(String penType,String name){ System.out.println(penType + "蓝色的"+ name + "."); } }
public class MiddlePen extends Pen{ public void draw(String name){ String penType="中号毛笔绘制"; this.color.bepaint(penType,name); } }
public abstract class Pen{ protected Color color; public void setColor(Color color){ this.color=color; } public abstract void draw(String name); }
public class Red implements Color{ public void bepaint(String penType,String name){ System.out.println(penType + "红色的"+ name + "."); } } public class SmallPen extends Pen{ public void draw(String name){ String penType="小号毛笔绘制"; this.color.bepaint(penType,name); } }
public class White implements Color{ public void bepaint(String penType,String name){ System.out.println(penType + "白色的"+ name + "."); } } public class Main{ public static void main(String a[]){ Color color; Pen pen; color=(Color)XMLUtilPen.getBean("color"); pen=(Pen)XMLUtilPen.getBean("pen"); pen.setColor(color); pen.draw("鲜花"); } }
|
输入:蓝色 小号毛笔
结果是:小号毛笔绘制蓝色的鲜花.