package com.jy.lejutaobao.testDemo; /** * 懒汉单例模式 多种实现 */ public class SingleLazy { private static SingleLazy instance=null; private SingleLazy() {} /** * 方式一 * 线程不安全 * @return */ public static SingleLazy getNoSafeInstance(){ if(instance == null){ instance = new SingleLazy(); } return instance; } /** * 方式二,线程安全 * @return */ public static synchronized SingleLazy getSafeInstance(){ if(instance == null){ instance = new SingleLazy(); } return instance; } /** * 方式三 线程安全 * 双重检查 * @return */ public static SingleLazy getSafeInstance2(){ //第一次检查 if(instance == null){ // 假设多个线程同时执行到synchronized(SingleLazy.class)这一行,则第二次检查来防止重复new对象 synchronized (SingleLazy.class){ //第二次检查 if(instance == null) { instance = new SingleLazy(); } } } return instance; } /** * 方式四 * 使用关键字volatile修饰保证有序性和可见性,禁止CPU指令重排 */ private static volatile SingleLazy instance2=null; public static SingleLazy getSafeInstance3(){ //第一次检查 if(instance2 == null){ // 假设多个线程同时执行到synchronized(SingleLazy.class)这一行,则第二次检查来防止重复new对象 synchronized (SingleLazy.class){ //第二重检查 if(instance2 == null) { instance2 = new SingleLazy(); } } } return instance2; } }
public class Singleton { private static Singleton instance=new Singleton(); private Singleton(){} public static Singleton getInstance() { return instance; } }
public class SingleBest { //私有构造方法 private SingleBest() {} //静态内部类 private static class SingletonBestHolder { //静态初始化 private static SingleBest instance = new SingleBest(); } //获得对象实例 public static SingleBest getInstance() { return SingletonBestHolder.instance; } }
public abstract class AbstractFactory { abstract CarDTO createCarDTO(int type); }
Public Factory1 extend AbstractFactory{ CarDTO createCarDTO(int a){ Switch(a){ Case 1:new CarDTO1(); Case 2:new CarDTO2(); } } }
/*生产电脑的抽象类*/ public abstract class ComputerBuilder { protected Computer computer=new Computer(); public abstract void buildBrand(); public abstract void buildCpu(); public abstract void buildRam(); public abstract void buildMotherboard(); public abstract void buildHardDisk(); public abstract void buildDisplay(); public Computer getComputer() { return computer; } }
public class Computer { private String brand; private String cpu; private String ram; private String motherboard; private String hardDisk; private String display; public Computer() { } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } public String getMotherboard() { return motherboard; } public void setMotherboard(String motherboard) { this.motherboard = motherboard; } public String getHardDisk() { return hardDisk; } public void setHardDisk(String hardDisk) { this.hardDisk = hardDisk; } public String getDisplay() { return display; } public void setDisplay(String display) { this.display = display; } @Override public String toString() { return "Computer{" "brand='" brand '\'' ", cpu='" cpu '\'' ", ram='" ram '\'' ", motherboard='" motherboard '\'' ", hardDisk='" hardDisk '\'' ", display='" display '\'' '}'; } }
public class ASUSComputerBuilder extends ComputerBuilder{ @Override public void buildBrand() { computer.setBrand("ASUS"); } @Override public void buildCpu() { computer.setCpu("Intel 酷睿 i9 13900K"); } @Override public void buildRam() { computer.setRam("金士顿骇客神条FURY 16GB DDR4 3200(HX432C18FB/16-SP)"); } @Override public void buildMotherboard() { computer.setMotherboard("华硕TUF GAMING B660M-PLUS WIFI D4 供电模式:10 1相 主芯片组:Intel B6"); } @Override public void buildHardDisk() { computer.setHardDisk("希捷BarraCuda 2TB 7200转 256MB SATA3(ST2000DM008) 台式机械硬"); } @Override public void buildDisplay() { computer.setDisplay("飞利浦279C9 4K显示器,支持HDR400,65W反向充电,四边微边设计"); } }
public class LenoveoComputerBuilder extends ComputerBuilder{ @Override public void buildBrand() { computer.setBrand("Lenovo"); } @Override public void buildCpu() { computer.setCpu("Intel 酷睿 i9-13900KF"); } @Override public void buildRam() { computer.setRam("联想16GB DDR4 2666 "); } @Override public void buildMotherboard() { computer.setMotherboard("技嘉B660M GAMING DDR4 主芯片组:Intel B660 CPU插槽:LGA 1700 内存类"); } @Override public void buildHardDisk() { computer.setHardDisk("三星980 PRO NVMe M.2 TLC闪存架构,NVMe M.2接口,Elpis控制器"); } @Override public void buildDisplay() { computer.setDisplay("联想TE24-20 "); } }
public class ComputerDirector { private ComputerBuilder computerBuilder; private Computer computer; public void setComputerBuilder(ComputerBuilder computerBuilder) { this.computerBuilder = computerBuilder; } public Computer getComputer() { computerBuilder.buildBrand(); computerBuilder.buildCpu(); computerBuilder.buildRam(); computerBuilder.buildMotherboard(); computerBuilder.buildHardDisk(); computerBuilder.buildDisplay(); return computerBuilder.getComputer(); } public static void main(String[] args) { ComputerDirector computerDirector=new ComputerDirector(); ASUSComputerBuilder asusComputerBuilder=new ASUSComputerBuilder(); LenoveoComputerBuilder lenoveoComputerBuilder=new LenoveoComputerBuilder(); /** * 让华硕电脑建造者制造电脑 */ computerDirector.setComputerBuilder(asusComputerBuilder); /** * 获得华硕电脑 */ Computer computerAsus=computerDirector.getComputer(); System.out.println(computerAsus); /** * 让联想电脑建造者制造电脑 */ computerDirector.setComputerBuilder(lenoveoComputerBuilder); /** * 获得联想电脑 */ Computer computerLenovo=computerDirector.getComputer(); System.out.println(computerLenovo); } }
/** * 创建一个接口A **/ public interface A { void doSomething(); } /** *创建一个被代理的类C1实现接口A **/ public class C1 implements A{ @Override public void doSomething() { System.out.println("我要买电脑"); } } /** *创建一个代理类C2 实现接口A 方法中用C1去调用接口. **/ public class C2 implements A{ private C1 c1=new C1(); @Override public void doSomething() { doBefore(); c1.doSomething(); doAfter(); } /** * 前置服务 **/ public void doBefore(){ System.out.println("-----进行前置服务-----"); } /** * 后置服务 **/ public void doAfter(){ System.out.println("-----进行后置服务-----"); } }
/** * 1、创建接口定义Subject */ public interface Subject { void sellPhone(); } /** * 2、创建被代理类SubjectImpl 实现接口Subject */ public class SubjectImpl implements Subject{ @Override public void sellPhone() { System.out.println("买华为手机"); } } import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * 3、创建动态代理类 */ public class SubjectProxy implements InvocationHandler { private Subject subject; public SubjectProxy(Subject subject) { this.subject = subject; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { doBefore(); Object invoke = method.invoke(subject, args); doAfter(); return invoke; } public Object getInvokeHandler() { return Proxy.newProxyInstance(subject.getClass().getClassLoader(), subject.getClass().getInterfaces(), this); } /** * 前置服务 **/ public void doBefore(){ System.out.println("-----进行前置服务-----"); } /** * 后置服务 **/ public void doAfter(){ System.out.println("-----进行后置服务-----"); } } /** * 测试动态代理 */ public class TestSubjectDemo { public static void main(String[] args) { SubjectImpl subject = new SubjectImpl(); SubjectProxy subjectProxy = new SubjectProxy(subject); Subject proxyInstance = (Subject)subjectProxy.getInvokeHandler(); proxyInstance.sellPhone(); } }
public class A { public void fun(int a,int b){ System.out.println(a " " b "=" (a b)); } } public class B extends A{ @Override public void fun(int a,int b){ System.out.println(a "-" b "=" (a-b)); } }