本文为自己的学习记录,记录一下自己在面试中遇到的一些手撕代码(非算法题目),也是自己复习方便
1、工厂模式
(1)简单工厂
// 抽象产品角色
public interface Product {
void getInfo();
}
// 真实产品A
public class ProductA implements Product {
@Override
public void getInfo() {
System.out.println("I am productA");
}
}
// 真实产品B
public class ProductB implements Product {
@Override
public void getInfo() {
System.out.println("I am productB");
}
}
// 工厂类,顺便在这里进行测试
public class Factory {
private Product product;
public Factory() {
}
public Product getProductA() {
product = new ProductA();
return product;
}
public Product getProductB() {
product = new ProductB();
return product;
}
public static void main(String[] args) {
Factory factory = new Factory();
Product productA = factory.getProductA();
Product productB = factory.getProductB();
productA.getInfo();
productB.getInfo();
}
}
(2)抽象工厂
// 还是使用上面的ProductA和ProductB
// 抽象工厂类
public interface Factory {
Product getProduct();
}
// 真实工厂A
public class ProductAFactory implements Factory {
@Override
public Product getProduct() {
return new ProductA();
}
}
// 真实工厂B
public class ProductBFactory implements Factory {
@Override
public Product getProduct() {
return new ProductB();
}
}
// 测试类
public class Main {
public synchronized static void main(String[] args) {
Factory factoryA = new ProductAFactory();
Product productA = factoryA.getProduct();
Factory factoryB = new ProductBFactory();
Product productB = factoryB.getProduct();
productA.getInfo();
productB.getInfo();
}
}
2、单例模式
(1)双重校验锁
public class Singleton {
private static volatile Singleton INSTANCE = null;
private Singleton(){};
public Singleton getInstance() {
if (INSTANCE == null) {
synchronized (Singleton.class) {
if (INSTANCE == null) {
INSTANCE = new Singleton();
}
return INSTANCE;
}
}
return INSTANCE;
}
}
(2)静态内部类
public class Singleton {
private Singleton(){};
static class SingletonHelper {
private static Singleton INSTANCE = new Singleton();
}
public Singleton getInstance() {
return SingletonHelper.INSTANCE;
}
}
3、策略模式
// 抽象策略类
public interface Strategy {
void getInfo();
}
// 策略A
public class StrategyA implements Strategy {
@Override
public void getInfo() {
System.out.println("I am StrategyA.");
}
}
// 策略B
public class StrategyB implements Strategy {
@Override
public void getInfo() {
System.out.println("I am StrategyB.");
}
}
// 环境角色类
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void doSomeThing() {
strategy.getInfo();
}
public static void main(String[] args) {
Context context = new Context(new StrategyA());
context.doSomeThing();
}
}
1、读写锁
(1)synchronized实现
class MyReadWriteLock{
private volatile int read;
private volatile int write;
public MyReadWriteLock(){
read = 0;
write = 0;
}
public synchronized void readLock() throws InterruptedException {
while (write != 0) {
wait();
}
read ++;
}
public synchronized void unlockRead() {
read --;
notifyAll();
}
public synchronized void writeLock() throws InterruptedException {
while (write != 0 || read != 0) {
wait();
}
write ++;
}
public synchronized void unlockWrite() {
write --;
notifyAll();
}
}
(2)信号量实现
class ReaderAndWriter{
private Semaphore readSemaphore = new Semaphore(1);
private Semaphore writerSemaphore = new Semaphore(1);
private int count = 0;
public void readLock() throws InterruptedException {
readSemaphore.acquire();
if (count == 0) {
writerSemaphore.acquire();
}
++count;
readSemaphore.release();
}
public void readUnlock() throws InterruptedException {
readSemaphore.acquire();
--count;
if (count == 0) {
writerSemaphore.release();
}
readSemaphore.release();
}
public void writeLock() throws InterruptedException {
writerSemaphore.acquire();
}
public void writeUnlock() {
writerSemaphore.release();
}
}
2、生产者消费者
1、信号量实现
import java.util.concurrent.Semaphore;
public class ProduceAndConsume {
private int cacheSize = 0;
private Semaphore mutex;
private Semaphore full;
private Semaphore empty;
public ProduceAndConsume(int size) {
mutex = new Semaphore(1);
full = new Semaphore(0);
empty = new Semaphore(size);
}
public void produce() throws InterruptedException {
empty.acquire();
mutex.acquire();
cacheSize ++;
System.out.println("produce..");
mutex.release();
full.release();
}
public void consume() throws InterruptedException {
full.acquire();
mutex.acquire();
cacheSize --;
System.out.println("consume");
mutex.release();
empty.release();
}
}
1、快排(最好情况nlogn,最坏n2,数字正序或逆序)
public void quickSort(int[] nums, int left, int right) {
if (left >= right) return;
int key = nums[left];
int pLow = left;
int pHigh = right;
while (pLow < pHigh) {
// 注意要从右边开始,是因为跟哨兵的选择有关
while (nums[pHigh] >= key && pLow < pHigh) {
--pHigh;
}
while (nums[pLow] <= key && pLow < pHigh) {
++pLow;
}
if (pLow < pHigh) {
int tmp = nums[pLow];
nums[pLow] = nums[pHigh];
nums[pHigh] = tmp;
}
}
nums[left] = nums[pLow];
nums[pLow] = key;
quickSort(nums,left,pLow-1);
quickSort(nums,pLow+1,right);
}