设计模式

开头先说这里所有的东西都是从实验楼那里学习的,这些就权当是笔记了,很不全面有兴趣去实验楼看看吧。

适配器模式Adapter:
首先Adapter类是继承旧的接口,然后在Adapter里面实现新接口的注入,然后override旧接口的方法,然后用新接口的方法来覆盖实现旧接口。
适配器模式的用途:使某个继承了旧接口的类能实现新接口的功能

public class PluginAdapter implements CnPluginInterface{
    private EnPluginInterface enPluginInterface;

    public PluginAdapter(EnPluginInterface enPluginInterface){
        this.enPluginInterface = enPluginInterface;
    }

    @Override
    public void chargeWith2Pins() {
        enPluginInterface.chargeWith3Pins();
    }
}

装饰者模式DecoratorPattern:
个人理解是由父类到子类的属性的保存和附加。
代码如下

public abstract class Girl {
    String description = "no particular";

    public String getDescription(){
        return description;
    }
}


public class AmericanGirl extends Girl {
    public AmericanGirl() {
        description = "+AmericanGirl";
    }
}


public class ChineseGirl extends Girl {
    public ChineseGirl() {
        description = "+ChineseGirl";
    }
}


public abstract class GirlDecorator extends Girl {
    public abstract String getDescription();
}


public class GoldenHair extends GirlDecorator {

    private Girl girl;

    public GoldenHair(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+with golden hair";
    }

}


public class Tall extends GirlDecorator {

    private Girl girl;

    public Tall(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+is very tall";
    }

}


// 检验一下
public class Test {

    public static void main(String[] args) {
        Girl g1 = new AmericanGirl();
        System.out.println(g1.getDescription());

        GoldenHair g2 = new GoldenHair(g1);
        System.out.println(g2.getDescription());

        Tall g3 = new Tall(g2);
        System.out.println(g3.getDescription());

        // 你也可以一步到位
        // Girl g = new Tall(new GoldenHair(new AmericanGirl())); 
    }
}

还是下面讲的比较清楚- -:

抽象接口(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。 具体构件(Concrete
Component)角色:定义一个将要接收附加责任的类。
装饰(Decorator)角色:持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(Concrete Decorator)角色:负责给构件对象”贴上”附加的责任。

装饰者模式

单例模式:一个类能返回对象的一个引用(永远是同一个)和一个获得该唯一实例的方法(必须是静态方法)。

public class Singleton_1 {
//线程不安全的懒汉模式
    String name ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private static Singleton_1 singleton;
    private Singleton_1(){
    }
    public static Singleton_1 getSingleton_1(){
        if (singleton==null){
            singleton = new Singleton_1();
        }
        return singleton;
    }

    public void print(){
        System.out.println(name);
    }
}

public class Test {


    public static void main(String[] args) {
        Singleton_1 a= Singleton_1.getSingleton_1();
        Singleton_1 b = Singleton_1.getSingleton_1();
        b.setName("adsfadsf");
        a.setName("dfdfdfdf");
        a.print();
        b.print();
    }
}

运行结果
dfdfdfdf
dfdfdfdf
几种实现形式
单例模式的实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值