面向东西计划形式是软件开辟中的一种重要不雅点,它源于现实中反复呈现的成绩及其处理打算的总结。经由过程遵守计划形式,开辟者可能晋升代码的可读性、可保护性跟体系弹性,同时避免代码反复,优化软件架构。本文将深刻剖析面向东西计划形式的核心头脑,并探究如何在现实项目中利用这些形式来晋升代码品质。
面向东西计划形式是一套被反复利用、少数人知晓、经过分类编目标、代码计划经验的总结。利用计划形式是为了可重用代码、让代码更轻易被他人懂得、保证代码坚固性。
总体来说,计划形式分为三大年夜类:
创建型形式重要关注东西的创建过程,以下是一些罕见的创建型形式:
工厂方法形式定义一个接口,让子类决定实例化哪一个类。工厂方法让类的实例化推迟到子类。
public interface Creator {
Product factoryMethod();
}
public class ConcreteCreatorA implements Creator {
public Product factoryMethod() {
return new ConcreteProductA();
}
}
public class ConcreteCreatorB implements Creator {
public Product factoryMethod() {
return new ConcreteProductB();
}
}
public class Product {
// 产品类
}
public class ConcreteProductA extends Product {
// 具体产品A
}
public class ConcreteProductB extends Product {
// 具体产品B
}
抽象工厂形式供给一个创建一系列相干或相互依附东西的接口,而无需指定它们的具体类。
public interface AbstractFactory {
Color createColor();
Shape createShape();
}
public class ConcreteFactory1 implements AbstractFactory {
public Color createColor() {
return new Red();
}
public Shape createShape() {
return new Circle();
}
}
public class ConcreteFactory2 implements AbstractFactory {
public Color createColor() {
return new Green();
}
public Shape createShape() {
return new Square();
}
}
public class Color {
// 色彩类
}
public class Shape {
// 外形类
}
public class Red extends Color {
// 白色
}
public class Green extends Color {
// 绿色
}
public class Circle extends Shape {
// 圆形
}
public class Square extends Shape {
// 正方形
}
构外型形式重要关注怎样组合东西跟类,以下是一些罕见的构外型形式:
适配器形式将一个类的接口转换成客户盼望的另一个接口。适配器让底本接口不兼容的类可能一同任务。
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// ...
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
public void request() {
adaptee.specificRequest();
}
}
装潢器形式静态地给一个东西增加一些额定的职责,而不改变其接口。
public interface Component {
void operation();
}
public class ConcreteComponent implements Component {
public void operation() {
// ...
}
}
public class Decorator implements Component {
private Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
// 增加额定职责
}
}
行动型形式重要关注东西之间的通信跟交互,以下是一些罕见的行动型形式:
战略形式定义一系列算法,将每个算法封装起来,并使它们可能相互调换。
public interface Strategy {
void execute();
}
public class ConcreteStrategyA implements Strategy {
public void execute() {
// ...
}
}
public class ConcreteStrategyB implements Strategy {
public void execute() {
// ...
}
}
public class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public void executeStrategy() {
strategy.execute();
}
}
察看者形式定义东西间的一对多依附关联,当一个东西改变状况时,全部依附于它的东西都会掉掉落告诉并主动更新。
public interface Observer {
void update();
}
public class ConcreteObserver implements Observer {
public void update() {
// ...
}
}
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
面向东西计划形式是软件开辟中的一项重要技能,经由过程控制这些形式,开辟者可能晋升代码品质,进步软件的可保护性跟可扩大年夜性。本文介绍了面向东西计划形式的核心头脑以及一些罕见的形式,盼望对读者有所帮助。在现实项目中,开辟者应根据具体须要抉择合适的计划形式,以实现最佳后果。