龙空技术网

java常用设计模式

四月燕儿归 119

前言:

今天大家对“java详细设计包括哪些内容”可能比较重视,我们都需要知道一些“java详细设计包括哪些内容”的相关文章。那么小编同时在网摘上收集了一些关于“java详细设计包括哪些内容””的相关资讯,希望看官们能喜欢,小伙伴们快快来学习一下吧!

1、工厂模式:创建一个工厂类,用于创建具有相同属性和行为的对象。例如,创建一个汽车工厂,用于生产不同类型的汽车。

举例:

public interface Animal {

void eat();

}

public class Dog implements Animal {

@Override

public void eat() {

System.out.println("Dog is eating...");

}

}

public class Cat implements Animal {

@Override

public void eat() {

System.out.println("Cat is eating...");

}

}

public class AnimalFactory {

public static Animal createAnimal(String type) {

if ("dog".equalsIgnoreCase(type)) {

return new Dog();

} else if ("cat".equalsIgnoreCase(type)) {

return new Cat();

} else {

return null;

}

}

}

public class Test {

public static void main(String[] args) {

Animal dog = AnimalFactory.createAnimal("dog");

dog.eat();

Animal cat = AnimalFactory.createAnimal("cat");

cat.eat();

}

}

2、单例模式:确保一个类只有一个实例,并提供全局访问点。例如,创建一个日志类,用于记录应用程序的所有事件。

public class Singleton {

private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

}

public class Test {

public static void main(String[] args) {

Singleton singleton1 = Singleton.getInstance();

Singleton singleton2 = Singleton.getInstance();

System.out.println(singleton1 == singleton2); // true

}

}

3、观察者模式:定义对象之间的一对多依赖关系,当一个对象状态改变时,所有依赖它的对象都会得到通知并自动更新。例如,创建一个邮件订阅服务,当有新邮件时,所有订阅者都会收到通知。

public interface Observer {

void update(String message);

}

public class User implements Observer {

private String name;

public User(String name) {

this.name = name;

}

@Override

public void update(String message) {

System.out.println(name + " received message: " + message);

}

}

public interface Subject {

void registerObserver(Observer observer);

void removeObserver(Observer observer);

void notifyObservers(String message);

}

public class WeChat implements Subject {

private List<Observer> observers = new ArrayList<>();

@Override

public void registerObserver(Observer observer) {

observers.add(observer);

}

@Override

public void removeObserver(Observer observer) {

observers.remove(observer);

}

@Override

public void notifyObservers(String message) {

for (Observer observer : observers) {

observer.update(message);

}

}

}

public class Test {

public static void main(String[] args) {

Subject weChat = new WeChat();

Observer user1 = new User("User1");

Observer user2 = new User("User2");

weChat.registerObserver(user1);

weChat.registerObserver(user2);

weChat.notifyObservers("Hello, world!");

}

}

4、适配器模式:将一个类的接口转换成客户端所期望的另一个接口。例如,创建一个适配器,将一个旧的接口转换成一个新的接口,以便与新的客户端进行交互。

public interface Target {

void request();

}

public class Adaptee {

public void specificRequest() {

System.out.println("Adaptee's specific request");

}

}

public class Adapter implements Target {

private Adaptee adaptee;

public Adapter(Adaptee adaptee) {

this.adaptee = adaptee;

}

@Override

public void request() {

adaptee.specificRequest();

}

}

public class Test {

public static void main(String[] args) {

Adaptee adaptee = new Adaptee();

Target target = new Adapter(adaptee);

target.request();

}

}

5、装饰器模式:动态地将责任附加到对象上,以扩展其功能。例如,创建一个装饰器类,用于向一个文本编辑器添加更多的字体和颜色选项。

public interface Component {

void operation();

}

public class ConcreteComponent implements Component {

@Override

public void operation() {

System.out.println("Concrete component operation");

}

}

public abstract class Decorator implements Component {

protected Component component;

public Decorator(Component component) {

this.component = component;

}

@Override

public void operation() {

component.operation();

}

}

public class ConcreteDecoratorA extends Decorator {

public ConcreteDecoratorA(Component component) {

super(component);

}

@Override

public void operation() {

super.operation();

System.out.println("Concrete decorator A operation");

}

}

public class ConcreteDecoratorB extends Decorator {

public ConcreteDecoratorB(Component component) {

super(component);

}

@Override

public void operation() {

super.operation();

System.out.println("Concrete decorator B operation");

}

}

public class Test {

public static void main(String[] args) {

Component component = new ConcreteComponent();

component = new ConcreteDecoratorA(component);

component = new ConcreteDecoratorB(component);

component.operation();

}

}

6、策略模式:定义一系列算法,将它们封装起来,并使它们可以相互替换。例如,创建一个排序算法库,其中包含多种不同的排序算法,可以根据需要进行切换。

public interface Strategy {

void execute();

}

public class ConcreteStrategyA implements Strategy {

@Override

public void execute() {

System.out.println("Concrete strategy A execute");

}

}

public class ConcreteStrategyB implements Strategy {

@Override

public void execute() {

System.out.println("Concrete strategy B execute");

}

}

public class Context {

private Strategy strategy;

public Context(Strategy strategy) {

this.strategy = strategy;

}

public void executeStrategy() {

strategy.execute();

}

}

public class Test {

public static void main(String[] args) {

Strategy strategyA = new ConcreteStrategyA();

Strategy strategyB = new ConcreteStrategyB();

Context context = new Context(strategyA);

context.executeStrategy();

context = new Context(strategyB);

context.executeStrategy();

}

}

7、模板方法模式:定义一个操作中的算法骨架,将一些步骤延迟到子类中。例如,创建一个游戏类,其中包含多个不同的游戏模式,每个模式都有自己的规则和玩法。

public abstract class AbstractClass {

public final void templateMethod() {

primitiveOperation1();

primitiveOperation2();

concreteOperation();

hook();

}

protected abstract void primitiveOperation1();

protected abstract void primitiveOperation2();

private void concreteOperation() {

System.out.println("Concrete operation");

}

protected void hook() {}

}

public class ConcreteClass extends AbstractClass {

@Override

protected void primitiveOperation1() {

System.out.println("Primitive operation 1 in concrete class");

}

@Override

protected void primitiveOperation2() {

System.out.println("Primitive operation 2 in concrete class");

}

@Override

protected void hook() {

System.out.println("Hook in concrete class");

}

}

public class Test {

public static void main(String[] args) {

AbstractClass abstractClass = new ConcreteClass();

abstractClass.templateMethod();

}

}

8、建造者模式:将一个复杂对象的构建过程分离成多个简单的步骤,以便于构建不同的对象。例如,创建一个房屋建造者,其中包含多个不同的建造步骤,可以根据需要进行组合。

public class Product {

private String partA;

private String partB;

private String partC;

public void setPartA(String partA) {

this.partA = partA;

}

public void setPartB(String partB) {

this.partB = partB;

}

public void setPartC(String partC) {

this.partC = partC;

}

public void show() {

System.out.println("Part A: " + partA);

System.out.println("Part B: " + partB);

System.out.println("Part C: " + partC);

}

}

public abstract class Builder {

protected Product product = new Product();

public abstract void buildPartA();

public abstract void buildPartB();

public abstract void buildPartC();

public Product getResult() {

return product;

}

}

public class ConcreteBuilder extends Builder {

@Override

public void buildPartA() {

product.setPartA("Part A");

}

@Override

public void buildPartB() {

product.setPartB("Part B");

}

@Override

public void buildPartC() {

product.setPartC("Part C");

}

}

public class Director {

public void construct(Builder builder) {

builder.buildPartA();

builder.buildPartB();

builder.buildPartC();

}

}

public class Test {

public static void main(String[] args) {

Builder builder = new ConcreteBuilder();

Director director = new Director();

director.construct(builder);

Product product = builder.getResult();

product.show();

}

}

标签: #java详细设计包括哪些内容