Java设计模式是指在编程时,按一定的规则或思想来组织相关类和对象的结构,以解决特定问题的一系列解决方案。Java设计模式是对过去经验的总结和提炼,它在编程中广泛应用,有助于提高编程效率和程序可重用性。
一、单例模式
单例模式是一种常见的设计模式,它的功能是确保一个类只有一个实例,并且提供全局访问点。
单例模式的实例化方式分为饿汉式和懒汉式两种:
饿汉式:
public class Singleton { // 类初始化时,立即加载这个对象(只会有一个实例) private static Singleton instance = new Singleton(); private Singleton() { } // 方法没有同步,调用效率高 public static Singleton getInstance() { return instance; } }懒汉式:
public class Singleton { private static Singleton instance; private Singleton() { } // 加入同步锁,解决线程安全问题,但效率低 public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
饿汉式的优点是线程安全,但在类装载时就实例化,造成程序启动较慢。懒汉式需要加同步锁保证线程安全,但每次获取实例时都需要同步,效率较低。
二、工厂模式
工厂模式又称工厂方法模式,是一种常见的面向对象编程设计模式,用于实现解耦,让对象的创建和实现完全分离。
工厂模式可以分为三种:
简单工厂模式:
public interface Fruit {
void get();
}
public class Apple implements Fruit {
public void get() {
System.out.println("get an apple");
}
}
public class Orange implements Fruit {
public void get() {
System.out.println("get an orange");
}
}
public class Factory {
public static Fruit create(String type) {
if ("apple".equals(type)) {
return new Apple();
} else if ("orange".equals(type)) {
return new Orange();
} else {
return null;
}
}
}工厂方法模式:
public interface Fruit {
void get();
}
public class Apple implements Fruit {
public void get() {
System.out.println("get an apple");
}
}
public class Orange implements Fruit {
public void get() {
System.out.println("get an orange");
}
}
public interface Factory {
Fruit create();
}
public class AppleFactory implements Factory {
public Fruit create() {
return new Apple();
}
}
public class OrangeFactory implements Factory {
public Fruit create() {
return new Orange();
}
}
public class Main {
public static void main(String[] args) {
Factory appleFactory = new AppleFactory();
Factory orangeFactory = new OrangeFactory();
Fruit apple = appleFactory.create();
Fruit orange = orangeFactory.create();
apple.get();
orange.get();
}
}抽象工厂模式:
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("draw a circle");
}
}
public class Square implements Shape {
public void draw() {
System.out.println("draw a square");
}
}
public interface Factory {
Shape createShape();
Color createColor();
}
public class RedCircleFactory implements Factory {
public Shape createShape() {
return new Circle();
}
public Color createColor() {
return new Red();
}
}
public class BlueSquareFactory implements Factory {
public Shape createShape() {
return new Square();
}
public Color createColor() {
return new Blue();
}
}工厂模式可以根据需求的不同,选择合适的方式来创建对象,实现代码的灵活和可扩展性。
三、适配器模式
适配器模式是一种结构型设计模式,适用于将一个类的接口转换成客户期望的另一个接口,可以协调不兼容接口之间的关系,提高代码可重用性。
public interface Target {
void request();
}
public class Adaptee {
public void specialRequest() {
System.out.println("special request");
}
}
public class Adapter extends Adaptee implements Target {
public void request() {
specialRequest();
}
}适配器模式通过继承、组合等方式来实现接口的转换,可以解决需要在不兼容的接口之间进行通信的情况,提高代码的可维护性。
四、装饰器模式
装饰器模式是一种结构型设计模式,用于在不修改现有代码的情况下动态扩展对象的功能。
public interface Shape {
void draw();
}
public class Circle implements Shape {
public void draw() {
System.out.println("draw a circle");
}
}
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape) {
this.decoratedShape = decoratedShape;
}
public void draw() {
decoratedShape.draw();
}
}
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape) {
System.out.println("set red border");
}
}装饰器模式可以在不改变原有代码的情况下,动态地为一个对象添加额外的职责,可以有效地提高代码的复用性和可维护性。
五、观察者模式
观察者模式是一种行为型设计模式,用于在对象间建立一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会得到通知并自动更新。
public interface Observer {
void update(String message);
}
public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
public class ConcreteSubject implements Subject {
private List observers = new ArrayList();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
}
public class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
public void update(String message) {
System.out.println(name + ": " + message);
}
}观察者模式可以实现对象间的松耦合,提高代码的可扩展性和可重用性。
原创文章,作者:NQHW,如若转载,请注明出处:https://www.506064.com/n/132518.html
微信扫一扫
支付宝扫一扫