java实现编写适配器模式 编写三种工厂模式 编写三种单例模式 编写两种代理模式
时间: 2023-07-18 17:47:24 浏览: 111
三种工厂模式java实现
好的,我会回答你的问题。首先,适配器模式的实现可以参考以下代码:
```java
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// do something
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
```
以上代码中,`Target` 接口定义了客户端需要的接口,`Adaptee` 类是需要适配的类,`Adapter` 类是适配器,将 `Adaptee` 类的接口适配成 `Target` 接口。
接下来是三种工厂模式的实现:
- 简单工厂模式
```java
public interface Product {
void doSomething();
}
public class ConcreteProductA implements Product {
@Override
public void doSomething() {
// do something
}
}
public class ConcreteProductB implements Product {
@Override
public void doSomething() {
// do something
}
}
public class SimpleFactory {
public static Product createProduct(String type) {
if (type.equals("A")) {
return new ConcreteProductA();
} else if (type.equals("B")) {
return new ConcreteProductB();
} else {
return null;
}
}
}
```
- 工厂方法模式
```java
public interface Product {
void doSomething();
}
public class ConcreteProductA implements Product {
@Override
public void doSomething() {
// do something
}
}
public class ConcreteProductB implements Product {
@Override
public void doSomething() {
// do something
}
}
public interface Factory {
Product createProduct();
}
public class ConcreteFactoryA implements Factory {
@Override
public Product createProduct() {
return new ConcreteProductA();
}
}
public class ConcreteFactoryB implements Factory {
@Override
public Product createProduct() {
return new ConcreteProductB();
}
}
```
- 抽象工厂模式
```java
public interface ProductA {
void doSomething();
}
public interface ProductB {
void doSomething();
}
public class ConcreteProductA1 implements ProductA {
@Override
public void doSomething() {
// do something
}
}
public class ConcreteProductA2 implements ProductA {
@Override
public void doSomething() {
// do something
}
}
public class ConcreteProductB1 implements ProductB {
@Override
public void doSomething() {
// do something
}
}
public class ConcreteProductB2 implements ProductB {
@Override
public void doSomething() {
// do something
}
}
public interface AbstractFactory {
ProductA createProductA();
ProductB createProductB();
}
public class ConcreteFactory1 implements AbstractFactory {
@Override
public ProductA createProductA() {
return new ConcreteProductA1();
}
@Override
public ProductB createProductB() {
return new ConcreteProductB1();
}
}
public class ConcreteFactory2 implements AbstractFactory {
@Override
public ProductA createProductA() {
return new ConcreteProductA2();
}
@Override
public ProductB createProductB() {
return new ConcreteProductB2();
}
}
```
以上代码分别实现了简单工厂模式、工厂方法模式和抽象工厂模式。
接下来是三种单例模式的实现:
- 饿汉式单例模式
```java
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
```
- 懒汉式单例模式
```java
public class Singleton {
private static Singleton instance = null;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
```
- 双重检查锁单例模式
```java
public class Singleton {
private static volatile Singleton instance = null;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
```
最后是两种代理模式的实现:
- 静态代理模式
```java
public interface Subject {
void request();
}
public class RealSubject implements Subject {
@Override
public void request() {
// do something
}
}
public class Proxy implements Subject {
private RealSubject realSubject;
public Proxy() {
this.realSubject = new RealSubject();
}
@Override
public void request() {
// do something before
realSubject.request();
// do something after
}
}
```
- 动态代理模式
```java
public interface Subject {
void request();
}
public class RealSubject implements Subject {
@Override
public void request() {
// do something
}
}
public class DynamicProxy implements InvocationHandler {
private Object target;
public DynamicProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// do something before
Object result = method.invoke(target, args);
// do something after
return result;
}
}
public class Client {
public static void main(String[] args) {
RealSubject realSubject = new RealSubject();
DynamicProxy dynamicProxy = new DynamicProxy(realSubject);
Subject proxy = (Subject) Proxy.newProxyInstance(
realSubject.getClass().getClassLoader(),
realSubject.getClass().getInterfaces(),
dynamicProxy);
proxy.request();
}
}
```
以上就是我对你提出的问题的回答,希望能对你有所帮助。
阅读全文