最常用6种java设计模式

阅读: 评论:0

2024年2月4日发(作者:)

最常用6种java设计模式

最常用6种java设计模式

Java设计模式是解决常见软件设计问题的最佳实践。这些模式已经被广泛地使用,通过它们,我们可以减少代码冗余,提高代码重用性,降低代码复杂度。在本文中,我将介绍几种常见的Java设计模式,并提供相应的示例代码。

单例模式

单例模式是一种创建型模式,它确保类只有一个实例,并提供全局访问点。在Java中,我们可以通过将构造函数私有化,创建一个静态变量来实现单例模式。以下是一个示例代码:

javaCopy codepublic class Singleton {

private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

}

工厂模式

工厂模式是一种创建型模式,它将对象的创建委托给工厂类。这种模式可以提高代码的可维护性和可扩展性。在Java中,我们可以通过定义一个接口和多个实现类来实现工厂模式。以下是一个示例代码:

javaCopy codepublic interface Shape {

void draw();

}

public class Rectangle implements Shape {

@Override

public void draw() {

n("Rectangle draw method.");

}

}

public class Circle implements Shape {

@Override

public void draw() {

n("Circle draw method.");

}

}

public class ShapeFactory {

public Shape getShape(String shapeType) {

if (shapeType == null) {

return null;

}

if (IgnoreCase("RECTANGLE")) {

return new Rectangle();

} else if (IgnoreCase("CIRCLE")) {

return new Circle();

}

return null;

}

}

观察者模式

观察者模式是一种行为型模式,它允许对象在其状态发生变化时通知其他对象。这种模式可以使对象之间的通信更加松散,增加代码的可维护性和可扩展性。在Java中,我们可以通过实现观察者和主题接口来实现观察者模式。以下是一个示例代码:

javaCopy codeimport ist;

import ;

public interface Observer {

void update();

}

public class Subject {

private List observers = new ArrayList<>();

public void attach(Observer observer) {

(observer);

}

public void detach(Observer observer) {

(observer);

}

public void notifyObservers() {

for (Observer observer : observers) {

();

}

}

}

public class ConcreteObserver implements Observer {

private String observerState;

public void update() {

observerState = "updated";

}

}

public class ConcreteSubject extends Subject {

private String subjectState;

public String getState() {

return subjectState;

}

public void setState(String state) {

tState = state;

notifyObservers();

}

}

装饰器模式

装饰器模式是一种结构型模式,它允许在运行时动态地给对象添加额外的职责。在Java中,我们可以通过实现一个接口并将实例传递给装饰器来实现装饰器模式。以下是一个示例代码:

javaCopy codepublic interface Component {

void operation();

}

public class ConcreteComponent implements Component {

public void operation() {

n("ConcreteComponent operation.");

}

}

public abstract class Decorator implements Component {

private Component component;

public Decorator(Component component) {

ent = component;

}

public void operation() {

ion();

}

}

public class ConcreteDecoratorA extends Decorator {

public ConcreteDecoratorA(Component component) {

super(component);

}

public void operation() {

ion();

n("ConcreteDecoratorA operation.");

}

}

public class ConcreteDecoratorB extends Decorator {

public ConcreteDecoratorB(Component component) {

super(component);

}

public void operation() {

ion();

n("ConcreteDecoratorB operation.");

}

}

策略模式

策略模式是一种行为型模式,它允许在运行时动态地改变对象的算法。在Java中,我们可以通过定义一个接口和多个实现类来实现策略模式。以下是一个示例代码:

javaCopy codepublic interface Strategy {

int doOperation(int num1, int num2);

}

public class OperationAdd implements Strategy {

public int doOperation(int num1, int num2) {

return num1 + num2;

}

}

public class OperationSubtract implements Strategy {

public int doOperation(int num1, int num2) {

return num1 - num2;

}

}

public class OperationMultiply implements Strategy {

public int doOperation(int num1, int num2) {

return num1 * num2;

}

}

public class Context {

private Strategy strategy;

public Context(Strategy strategy) {

gy = strategy;

}

public int executeStrategy(int num1, int num2) {

return ation(num1, num2);

}

}

代理模式

代理模式是一种结构型模式,它允许在不改变对象的情况下控制对象的访问。在Java中,我们可以通过实现一个接口并将实例传递给代理来实现代理模式。以下是一个示例代码:

javaCopy codepublic interface Image {

void display();

}

public class RealImage implements Image {

private String fileName;

public RealImage(String fileName) {

me = fileName;

loadFromDisk(fileName);

}

public void display() {

n("Displaying " + fileName);

}

private void loadFromDisk(String fileName) {

n("Loading " + fileName);

}

}

public class ProxyImage implements Image {

private RealImage realImage;

private String fileName;

public ProxyImage(String fileName) {

me = fileName;

}

public void display() {

if (realImage == null) {

realImage = new RealImage(fileName);

}

y();

}

}

最常用6种java设计模式

本文发布于:2024-02-04 06:52:55,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170700077552526.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模式   代码   实现   对象   接口   允许   创建
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23