创建型 工厂 抽象工厂 单利 建造者 原型
结构型 适配器 装饰 代理 外观 桥接 组合 享元
行为型 策略 模块方法 观察者 迭代子模式 责任链模式 命令模式 备忘录模式 状态模式 访问者模式 中介者模式 解释器模式。
1 工厂模式
1.1 普通工厂
//先定义接口
interface GcInt{
void Send();
}
public class Gc1 implements GcInt{
@Override
public void Send() {
Log.e("1","1");//标明log1
}
}
class Gc2 implements GcInt{
@Override
public void Send() {
Log.e("2","2");//标明log2
}
}
//工厂
class GcText{
public GcInt get(int i){
switch (i){
case 1:
return new Gc1();
case 2:
return new Gc2();
}
return null;
}
}
//测试类
class Text{
public void getData(){
GcText gcText = new GcText();
GcInt gcInt = gcText.get(1);
gcInt.Send();
GcInt gcInt2 = gcText.get(2);
gcInt2.Send();
//执行结果 先打印log1 在打印log2
}
}
1.2 多个工厂(防止在使用普通工厂时出现传值错误)
//先定义接口
interface GcInt{
void Send();
}
public class Gc1 implements GcInt{
@Override
public void Send() {
Log.e("1","1");//标明log1
}
}
class Gc2 implements GcInt{
@Override
public void Send() {
Log.e("2","2");//标明log2
}
}
//工厂
class GcText{
public GcInt get1(){
return new Gc1();
}
public GcInt get2(){
return new Gc2();
}
}
//测试类
class Text{
public void getData(){
GcText gcText = new GcText();
gcText.get1();
gcText.get2();
//执行结果 先打印log1 在打印log2
}
}
1.3 静态工厂(不需要实例化工厂对象降低消耗)
//先定义接口
interface GcInt{
void Send();
}
public class Gc1 implements GcInt{
@Override
public void Send() {
Log.e("1","1");//标明log1
}
}
class Gc2 implements GcInt{
@Override
public void Send() {
Log.e("2","2");//标明log2
}
}
//工厂
class GcText{
public static GcInt get1(){
return new Gc1();
}
public static GcInt get2(){
return new Gc2();
}
}
//测试类
class Text{
public void getData(){
GcText.get1();
GcText.get2();
//执行结果 先打印log1 在打印log2
}
}
创建多个工厂类 如果需要添加新的功能 只需要添加新的工厂就可以
//先定义接口
interface GcInt{
void Send();
}
public class Gc1 implements GcInt{
@Override
public void Send() {
Log.e("1","1");//标明log1
}
}
class Gc2 implements GcInt{
@Override
public void Send() {
Log.e("2","2");//标明log2
}
}
//定义工厂接口
interface Provider{
GcInt getGc();
}
//工厂1
class GcText1 implements Provider{
@Override
public GcInt getGc() {
return new Gc1();
}
}
//工厂2
class GcText2 implements Provider{
@Override
public GcInt getGc() {
return new Gc2();
}
}
//测试类
class Text{
public void getData(){
Provider gcText1 = new GcText1();
GcInt gc = gcText1.getGc();
gc.Send();
//执行结果 打印log1
}
保证对象在内存的唯一性使用当中需要保证现场安全
3.1 双重校验锁( 如果第一条线程走到了2实例化对象需要时间,另外一条线程走到了1对象在没有完成实例化之前就已经不是null了 会返回一个不完整的对象)
public class Dl1 {
private Dl1(){}
private static Dl1 dl1=null;
public static Dl1 getDl1(){
if(dl1==null){ //1
synchronized (getDl1()){
if(dl1==null){
dl1=new Dl1(); //2
}
}
}
return dl1;
}
}
3.2采用内部类的方法 虚拟机可以保存一个类被加载的时候线程是互斥的
public class Dl1 {
private Dl1(){}
private static class Myclass{
private static Dl1 dl1=new Dl1();
}
public static Dl1 getDl1(){
return Myclass.dl1;
}
}
将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
//java bean
public class Jzz {
private String head;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
}
//建造者给对象设置数据
class Builder{
private Jzz jzz;
public Builder(){
jzz = new Jzz();
}
public void setH(){
jzz.setHead("hand");
}
public Jzz buil(){
setH();
return jzz;
}
}
//测试类
class MText{
public void text(){
Builder builder = new Builder();
builder.buil();
}
}
5原型模式
将一个对象通过克隆和复制产生一个新的对象
浅复制:复制之后 只有基本数量类型的是重新创建的 引用数据类型的会指向之前的对象
深复制: 复制之后 基本数据类型和引用数据类型都是重新创建的
// 需要实现Cloneable接口
public class YX implements Cloneable{
//浅复制
public Object clone() throws CloneNotSupportedException {
YX clone = (YX) super.clone();
return clone;
}
//深复制
public Object deepClone() throws IOException, ClassNotFoundException {
//用二进制写入当前对象
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(this);
//用二进制写出当前对象
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
return objectInputStream.readObject();
}
}
6.1 类适配器(通过Spq类将待适配的类适配到接口当中)
class Spq extends Soure implements Targ {
@Override
public void text1() {
Log.e("1","1");
}
}
interface Targ{
void text1();
void text2();
}
//待适配的类
class Soure{
public void text2(){
Log.e("2","2");
}
}
//测试类
class Adapter{
Targ spq= new Spq();
spq.text1();
spq.text2();
}
6.2对象适配器 class Spq implements Targ {
private Soure soure;
public Spq(Soure soure){//将需要适配的类传进来
this.soure=soure;
}
@Override
public void text1() {
Log.e("1","1");
}
@Override
public void text2() {
soure.text2();
}
}
interface Targ{
void text1();
void text2();
}
//待适配的类
class Soure {
public void text2(){
Log.e("2","2");
}
}
//测试类
class Adapter{
Targ spq= new Spq(new Soure());
spq.text1();
spq.text2();
}
6.3接口适配器(当需要实现一个接口但是不想实现所有的方法)public abstract class Spq implements Targ {
public void text1() {
}
public void text2() {
}
}
interface Targ{
void text1();
void text2();
}
//测试类
class Text1 extends Spq{
@Override
public void text1() {
super.text1();
}
}
装饰和被装饰的类实现同一个接口,装饰类持有被装饰类的对象
interface Sourceable{
void inter1();
}
//被装饰类
class ZsText1 implements Sourceable{
@Override
public void inter1() {
Log.e("1111","11111");
}
}
//装饰类
class ZsText2 implements Sourceable{
private Sourceable sourceable;
public ZsText2(Sourceable zstext1){//装饰类持有被装饰类的对象
this.sourceable=sourceable;
}
@Override
public void inter1() {
Log.e("222","222");
sourceable.inter1();
}
}
//测试类
public class Zs {
Sourceable zstext2= new ZsText2( new ZsText1());
zstext2.inter1();//执行结果 先打印 1111 在打印222
}
给一个已经存在的类添加新的功能
interface DlSourceable{
void text();
}
class DlText1 implements DlSourceable{
@Override
public void text() {
Log.e("1","1");
}
}
//给DlText1添加新的功能
class DlText2 implements DlSourceable{
private DlText1 dlText1;
public DlText2(){
this.dlText1 = new DlText1();
}
@Override
public void text() {
dlText1.text();
Dad();
}
public void Dad(){
Log.e("2","2");
}
}
//测试类
public class DaiLi {
public void dl(){
DlSourceable dlText2 = new DlText2();
dlText2.text();
}
}
将多个类整合到一个类中 一起管理
class WgText1{
public void get(){
Log.e("1","1");
}
}
class WgText2{
public void get(){
Log.e("2","2");
}
}
//管理类
class WgText3{
private WgText1 wgText1;
private WgText2 wgText2;
public WgText3(){
wgText1 = new WgText1();
wgText2 = new WgText2();
}
public void get(){
wgText1.get();
wgText2.get();
}
}
//测试类
public class Wg {
WgText3 wgtext3= new WgText3();
wgtext3.get();
}
将抽象化与实例化独立分开通过一个桥梁连接
public class Text1 implements Inter{
@Override
public void text() {
Log.e("1","1");
}
}
public interface Inter {
void text();
}
//测试类和 text类 的桥梁
public class GetText {
private Inter inter;
public GetText(Inter inter){
this.inter=inter;
}
public Inter get (){
return inter;
}
public void text(){
inter.text();
}
}
//测试类
public class Qliang {
public void dd(){
GetText myGet = new GetText(new Text1());
myGet.text();
}
}
将多个对象组合到一起使用
public class ZuHe {
private String name;
List<ZuHe> list=new ArrayList<ZuHe>();
public ZuHe(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ZuHe> getList() {
return list;
}
public void setList(List<ZuHe> list) {
this.list = list;
}
public void add(ZuHe zh){
list.add(zh);
}
public void remove(ZuHe zh){
list.remove(zh);
}
}
class ZhText{
private ZuHe zuHe;
public ZhText(String name){
zuHe = new ZuHe(name);
}
public void Text(){
ZhText a = new ZhText("a");
ZuHe b = new ZuHe("b");
ZuHe c = new ZuHe("c");
b.add(c);
a.zuHe.add(b);
}
}
13策略模式
将需要用到的算法封装起来,通过接口为实现类提供方法
interface ICalculator{
int get(int a);
}
//将需要用到的算法封装起来
public class CeLue implements ICalculator{
@Override
public int get(int a) {
return a+a;
}
}
//测试类
class ClText{
public void text(){
ICalculator ceLue = new CeLue();
ceLue.get(2);
}
}
一个抽象类,通过另一个类继承抽象类重新抽象方法,通过抽象类对子类进行调用
abstract class Abs{
abstract int myText(int a);
public int Text(int i){
return myText(i+i);
}
}
public class Mk extends Abs{
@Override
int myText(int a) {
return a;
}
}
//测试类
class MkText{
public void text(){
Abs mk = new Mk();
mk.Text(1);
}
}
对象A依赖对象B 当B发生变化时通知A
interface Obs{
void up();
}
//观察者
class Observer1 implements Obs{
@Override
public void up() {
Log.e("1","1");
}
}
interface Sub{
void add(Obs obs);
void del(Obs obs);
}
//订阅
public class Gcz implements Sub{
private List list=new ArrayList<Obs>();
@Override
public void add(Obs obs) {
obs.up();
list.add(obs);
}
@Override
public void del(Obs obs) {
obs.up();
list.remove(obs);
}
}
class MyGcz extends Gcz{
@Override
public void del(Obs obs) {
del(obs);
}
}
//测试类
class GText{
public void text(){
Observer1 observer1 = new Observer1();
Gcz myGcz = new MyGcz();
myGcz.add(observer1);
myGcz.del(observer1);
}
}
迭代集合遍历所有的对象
17责任链模式
18命令模式
将军发命令 命令传递 士兵执行 3者之间的关系
interface Command{
void exe();
}
//将军发命令
class Invoker{
private Command command;
public Invoker(Command command){
this.command=command;
}
public void start(){
command.exe();
}
}
//命令
class Mycommand implements Command {
private Receiver receiver;
public Mycommand(Receiver receiver){
this.receiver=receiver;
}
@Override
public void exe() {
receiver.text();
}
}
//需要执行的士兵
class Receiver{
public void text(){
Log.e("收到","收到");
}
}
//测试类
public class Ml {
public void text(){
Receiver receiver = new Receiver();
Mycommand mycommand = new Mycommand(receiver);
Invoker invoker = new Invoker(mycommand);
invoker.start();
}
将一个类的某种状态保存到另外一个类备忘录中 用于恢复到之前的状态 备忘录通过另外一个类保存
//原始类
class YS{
private String name;
private Bfl bfl;
public YS(String name){
this.name=name;
}
public void setString(String name){
this.name=name;
}
public String getString(){
return name;
}
public Bfl getBfl(){
bfl = new Bfl(name);
return bfl;
}
public String getNmae(Bfl bfl){
return bfl.getName();
}
}
//备忘录类
public class Bfl {
private String name;
public Bfl(String name){
this.name=name;
}
public String getName(){
return name;
}
}
//保存备忘录的类
class Bflgl{
private Bfl bfl;
public Bflgl(Bfl bfl){
this.bfl=bfl;
}
public void setBfl(Bfl bfl){
this.bfl=bfl;
}
public Bfl getBfl(){
return bfl;
}
}
//测试类
class BfText{
public void text(){
YS ys = new YS("a");//name 是a
Bfl bfl = ys.getBfl();
Bflgl bflgl = new Bflgl(bfl);
ys.setString("z");//现在 name是z
ys.getNmae(bflgl.getBfl());//恢复成a
}
}
向为A类添加新的功能或者方法 但是A类代码不能改
interface Visitor{
void visit(Subject fwtext2);
}
class FwText1 implements Visitor{
@Override
public void visit(Subject fwtext2) {
//在这里拿到被访问的对象
}
}
interface Subject {
void accept(Visitor fwtext1);
}
//被访问的类
class FwText2 implements Subject{
@Override
public void accept(Visitor fwtext1) {
fwtext1.visit(this);
}
}
//测试类
public class Fwz {
public void text(){
Visitor fwText1 = new FwText1();
Subject fwText2 = new FwText2();
fwText2.accept(fwText1);
}
}
21状态模式
需要根据对象的某一个属性 区分功能
class State{
private String name;
public String getString (){
return name;
}
public void setString(String name){
this.name=name;
}
public void type1( ){
Log.e("1","1");
}
public void type2( ){
Log.e("2","2");
}
}
class Context {
private State state;
public Context(State state){
this.state=state;
}
public void type(){
if(state.getString().equals("1")){
state.type1();
}else{
state.type1();
}
}
}
//测试类
public class Zt {
public void text(){
State state = new State();
Context context = new Context(state);
state.setString("1");
context.type();
state.setString("2");
context.type();
}
}
22中介者模式
两个之间有联系 会相互持有对象 通过一个中间进行分离
interface Mediator{
void getUser();
}
class My implements Mediator{
@Override
public void getUser() {//是 user1 和user2 的中间类
User1 user1 = new User1();
User2 user2 = new User2();
}
}
class User1{
public void work(){
Log.e("1","1");
}
}
class User2{
public void work(){
Log.e("2","2");
}
}
public class Zjz {//测试类
public void text(){
Mediator my = new My();
my.getUser();
}
}
23解析器模式
面向对象编译器的开发中