可以每一个模式给一个java写的案例代码吗
时间: 2024-05-09 21:20:42 浏览: 92
好的,以下是一些常见的设计模式及其Java代码示例:
1. 单例模式
单例模式保证一个类只有一个实例,并提供全局访问点。
```java
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
```
2. 工厂模式
工厂模式通过定义一个创建对象的接口,让子类决定实例化哪个类。
```java
public interface Shape {
void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Drawing a rectangle");
}
}
public class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType.equalsIgnoreCase("circle")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("rectangle")) {
return new Rectangle();
}
return null;
}
}
```
3. 观察者模式
观察者模式定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会得到通知并自动更新。
```java
public interface Observer {
void update(String message);
}
public interface Subject {
void attach(Observer observer);
void detach(Observer observer);
void notifyObservers(String message);
}
public class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
@Override
public void attach(Observer observer) {
observers.add(observer);
}
@Override
public void detach(Observer observer) {
observers.remove(observer);
}
@Override
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;
}
@Override
public void update(String message) {
System.out.println(name + " received message: " + message);
}
}
```
4. 策略模式
策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互换使用。
```java
public interface Strategy {
void execute(int num1, int num2);
}
public class AddStrategy implements Strategy {
@Override
public void execute(int num1, int num2) {
System.out.println("Result: " + (num1 + num2));
}
}
public class SubtractStrategy implements Strategy {
@Override
public void execute(int num1, int num2) {
System.out.println("Result: " + (num1 - num2));
}
}
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void executeStrategy(int num1, int num2) {
strategy.execute(num1, num2);
}
}
```
5. 建造者模式
建造者模式将一个复杂对象的构建过程与其表示分离,使得同样的构建过程可以创建不同的表示。
```java
public class Computer {
private String cpu;
private String memory;
private String hardDisk;
private String display;
public Computer(String cpu, String memory, String hardDisk, String display) {
this.cpu = cpu;
this.memory = memory;
this.hardDisk = hardDisk;
this.display = display;
}
@Override
public String toString() {
return "Computer{" +
"cpu='" + cpu + '\'' +
", memory='" + memory + '\'' +
", hardDisk='" + hardDisk + '\'' +
", display='" + display + '\'' +
'}';
}
}
public interface ComputerBuilder {
void buildCPU(String cpu);
void buildMemory(String memory);
void buildHardDisk(String hardDisk);
void buildDisplay(String display);
Computer build();
}
public class DellComputerBuilder implements ComputerBuilder {
private Computer computer;
public DellComputerBuilder() {
computer = new Computer("", "", "", "");
}
@Override
public void buildCPU(String cpu) {
computer.cpu = cpu;
}
@Override
public void buildMemory(String memory) {
computer.memory = memory;
}
@Override
public void buildHardDisk(String hardDisk) {
computer.hardDisk = hardDisk;
}
@Override
public void buildDisplay(String display) {
computer.display = display;
}
@Override
public Computer build() {
return computer;
}
}
public class Director {
public void constructDellComputer(ComputerBuilder builder) {
builder.buildCPU("Intel i5");
builder.buildMemory("8GB");
builder.buildHardDisk("1TB");
builder.buildDisplay("15.6 inches");
}
}
```
阅读全文