怎么不花钱建立网站,杭州品牌vi设计公司,字体 安装到wordpress,什么人适合学ui设计设计模式在日常软件开发中的重要性 目录
单例模式工厂模式策略模式代理模式观察者模式装饰器模式模板方法模式建造者模式总结 单例模式
单例模式确保一个类只有一个实例#xff0c;通常用于管理共享资源#xff0c;如配置、缓存、线程池等。
代码实现#xff1a;双重检查…设计模式在日常软件开发中的重要性 目录
单例模式工厂模式策略模式代理模式观察者模式装饰器模式模板方法模式建造者模式总结 单例模式
单例模式确保一个类只有一个实例通常用于管理共享资源如配置、缓存、线程池等。
代码实现双重检查锁
public class Singleton {private static volatile Singleton instance;private Singleton() {}public static Singleton getInstance() {if (instance null) {synchronized (Singleton.class) {if (instance null) {instance new Singleton();}}}return instance;}
}工厂模式
工厂模式用于封装对象的创建逻辑特别是当类实例化过程复杂时可以降低耦合度。
代码实现简单工厂
public class PaymentFactory {public static Payment createPayment(String type) {switch (type) {case AliPay:return new AliPay();case WeChatPay:return new WeChatPay();default:throw new IllegalArgumentException(Unknown payment type);}}
}策略模式
策略模式将不同算法封装为独立类并允许在运行时选择不同的策略。
代码实现促销策略
public interface PromotionStrategy {void applyPromotion();
}public class DiscountStrategy implements PromotionStrategy {Overridepublic void applyPromotion() {System.out.println(Applying discount...);}
}public class PromotionContext {private PromotionStrategy strategy;public PromotionContext(PromotionStrategy strategy) {this.strategy strategy;}public void executePromotion() {strategy.applyPromotion();}
}代理模式
代理模式通过代理对象控制对目标对象的访问常用于权限控制、日志记录等场景。
代码实现静态代理
public interface Service {void execute();
}public class RealService implements Service {Overridepublic void execute() {System.out.println(Executing real service...);}
}public class ServiceProxy implements Service {private RealService realService;Overridepublic void execute() {System.out.println(Checking permissions...);if (realService null) {realService new RealService();}realService.execute();}
}观察者模式
观察者模式定义一对多的依赖当一个对象状态变化时所有依赖它的对象都会收到通知。
代码实现事件通知
public interface Observer {void update(String message);
}public class User implements Observer {private String name;public User(String name) {this.name name;}Overridepublic void update(String message) {System.out.println(name received message: message);}
}public class Weibo {private ListObserver observers new ArrayList();public void follow(Observer observer) {observers.add(observer);}public void post(String message) {for (Observer observer : observers) {observer.update(message);}}
}装饰器模式
装饰器模式在不改变原始类的基础上动态扩展其功能。
代码实现咖啡加料
public interface Coffee {String getDescription();double getCost();
}public class SimpleCoffee implements Coffee {Overridepublic String getDescription() {return Simple Coffee;}Overridepublic double getCost() {return 5.0;}
}public class MilkDecorator implements Coffee {private Coffee coffee;public MilkDecorator(Coffee coffee) {this.coffee coffee;}Overridepublic String getDescription() {return coffee.getDescription() , Milk;}Overridepublic double getCost() {return coffee.getCost() 1.5;}
}模板方法模式
模板方法模式定义一个算法的骨架把具体的实现留给子类。
代码实现任务执行模板
public abstract class Task {public final void execute() {init();doWork();cleanup();}protected abstract void init();protected abstract void doWork();protected void cleanup() {System.out.println(Default cleanup...);}
}public class DataProcessingTask extends Task {Overrideprotected void init() {System.out.println(Initializing data...);}Overrideprotected void doWork() {System.out.println(Processing data...);}
}建造者模式
建造者模式用于创建复杂对象特别是当对象有多个可选参数时。
代码实现构建HTTP请求
public class HttpRequest {private String method;private String url;private String body;private HttpRequest(Builder builder) {this.method builder.method;this.url builder.url;this.body builder.body;}public static class Builder {private String method;private String url;private String body;public Builder method(String method) {this.method method;return this;}public Builder url(String url) {this.url url;return this;}public Builder body(String body) {this.body body;return this;}public HttpRequest build() {return new HttpRequest(this);}}
}总结
在软件开发的广阔天地中设计模式不仅是解决特定问题的模板更是提升代码质量、可维护性和可扩展性的有力工具。通过本文的探讨我们深入了解了8种设计模式单例模式、工厂模式、策略模式、代理模式、观察者模式、装饰器模式、模板方法模式和建造者模式。每种模式都以其独特的方式帮助我们应对软件开发中的复杂性提供了一种清晰、结构化的方法来构建灵活且健壮的系统。
设计模式的价值
代码重用与解耦设计模式通过提供通用解决方案减少了重复代码降低了模块间的耦合度使得代码更易于管理和维护。提高可读性和可维护性遵循设计模式编写的代码结构清晰易于新开发人员理解从而降低了代码维护的难度。增强代码的灵活性和可扩展性设计模式使得添加新功能或修改现有功能变得更加简单无需大规模重构现有代码。促进最佳实践设计模式是多年软件开发经验的结晶遵循这些模式可以避免一些常见的陷阱促进最佳实践。
设计模式的实际应用
在JDK和Spring框架中设计模式的应用无处不在这不仅证明了它们的有效性也为我们在实际项目中应用这些模式提供了参考。例如Spring框架中的单例模式管理着Bean的生命周期工厂模式在创建Bean时发挥着重要作用而策略模式则在动态选择数据库查询策略时大显身手。
学习设计模式的建议
理论与实践相结合不仅要理解每种设计模式的理论基础更要通过实际编码来加深理解。适度应用设计模式是解决问题的工具但不应过度使用或滥用。在适当的场景选择合适的模式才能发挥其最大价值。持续学习随着技术的发展新的模式和实践不断出现持续学习是保持技术竞争力的关键。
结语
设计模式的学习是一个持续的过程它要求我们不断地实践、反思和改进。希望本文能够作为您学习设计模式旅程中的一个起点激发您在实际项目中探索和应用这些强大工具的热情。如果您有任何疑问、见解或经验分享欢迎在评论区交流让我们共同进步。。