码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式总结

时间:2017-09-30 00:25:20      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:对象   sys   通过   xtend   turn   span   过程   工厂方法模式   oid   

1 设计原则

        1 单一责任原则,一个类应该只有一个责任,如果有多个责任,责任就会耦合,如实现逻辑和界面的责任分开。

        2 开闭原则:对扩展开发,对修改关闭。我们可以扩展模块的功能,但是不必改动原模块的代码 如使用接口和抽象类,关键是抽象化

       3 里氏代换原则:任何基类出现的地方,子类一定可以出现,是对开闭原则的补充,开闭原则关键是抽象,而里氏代换的基类和子类就是抽象的具体化。

       4 依赖倒转原则:依赖于抽象,不要依赖于具体,就是对抽象编程,可以降低模块间的耦合

      5 接口隔离原则,客户端不应该依赖他不需要的接口。一个对的另一个类的依赖应该建立在最小接口上,使用多个专门接口比使用单一接口好,如用户使用订单查询,他就应该使用一个订单查询接口

       6 合成复用原则,尽量使用组合和聚合关系,少用继承,新对象通过委派调用已有对象的方法达到复用的目的

       7 最少知道法则:一个对象应该对其他对象尽可能少的了解。尽可能低的降低耦合,使得系统模块相互独立,如果必须联系可以通过中介。中介模式和门面模式就是最少知道法则的运用

 

2 对象的创建模式  

          对象的创建需要很多资源,把对象的创建和对象的使用分开,

        1 工厂方法模式,设计4个角色,抽象产品,具体产品,抽象工厂,具体工厂,模式很好的符合开闭原则,如果有新产品的增加,只需要增加一个创建具体产品的具体工厂即可,把创建产品的行为推迟到了子类。

技术分享
abstract class Dog{
    public abstract void eat();
}

class WhiteDog extends Dog{

    @Override
    public void eat() {
        // TODO Auto-generated method stub
        System.out.println("whiteDog eat");
    }
}

abstract class DogFactory{
    public abstract Dog createDog();
}

class WhiteDogFactory extends DogFactory {

    @Override
    public Dog createDog() {
        // TODO Auto-generated method stub
        return new WhiteDog();
    }
    
}
View Code

         2 抽象工厂模式, 涉及4个角色, 抽象产品,具体产品,抽象工厂,具体工厂,和工厂方法模式不同的是,抽象工厂可以创建具有多个产品等级结构的的子系统,而这些子系统形成一个,一个的产品族,如果使用工厂方法模式,则需要多个产品等级结构的的抽象工厂。用在系统中,等级结构个数不变化中。

技术分享
interface Gardener{
    Fruit createFruit();
    Vegetable createVegetable();
}

class NorthernGardener implements Gardener{

    @Override
    public Fruit createFruit() {
        // TODO Auto-generated method stub
        return new NorthernFruit();
    }

    @Override
    public Vegetable createVegetable() {
        // TODO Auto-generated method stub
        return new NorthernVegetable();
    }
}

class TropicalGardener implements Gardener{

    @Override
    public Fruit createFruit() {
        // TODO Auto-generated method stub
        return new TropicalFruit();
    }

    @Override
    public Vegetable createVegetable() {
        // TODO Auto-generated method stub
        return new TropicalVegetable();
    }
}
interface Fruit{}
interface Vegetable{}
class NorthernFruit implements Fruit{}
class TropicalFruit implements Fruit{}

class NorthernVegetable implements Vegetable{}
class TropicalVegetable implements Vegetable{}
View Code

       3 单例模式  单例模式能确保一个类只有一个实例,并且自行实例化,向整个系统提供这个实例 其中Java   runtime就是一个单例类

技术分享
class Runtime {
    private static Runtime currentRuntime = new Runtime();
    private Runtime(){}
    public static Runtime getRuntime() {
        return currentRuntime;
    }
}

class Runtime {
    private static Runtime currentRuntime = null;
    private Runtime(){}
    public static synchronized Runtime getRuntime() {
        if (currentRuntime == null) {
            currentRuntime = new Runtime();
        }
        return currentRuntime;
    }
}
View Code

     4 建造者模式, 涉及4个角色, 抽象建造者(定义产品各个成分建造过程,和返回产品),具体建造者, 产品,和指导者,客户端和指导者通信,可以将一个产品表象和建造过程分开,适合创建比较复杂对象。如果一个零件依赖于另一个零件,也可以使用

技术分享
class Person {
    private String head;
    private String body;
    private String foot;
    public String getHead() {
        return head;
    }
    public void setHead(String head) {
        this.head = head;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getFoot() {
        return foot;
    }
    public void setFoot(String foot) {
        this.foot = foot;
    }
}
class Man extends Person {
    public Man() {
        System.out.println("man build");
    }
}
interface PersonBuilder {
    void buildhead();
    void buildbody();
    void buildfoot();
    Person buildPerson();
}

class Manbuilder implements PersonBuilder {
    private Person p;
    
    public Manbuilder() {
        super();
        this.p = new Man();
    }

    @Override
    public void buildhead() {
        // TODO Auto-generated method stub
        p.setHead("man head");
    }

    @Override
    public void buildbody() {
        // TODO Auto-generated method stub
        p.setBody("man body");
    }

    @Override
    public void buildfoot() {
        // TODO Auto-generated method stub
        p.setFoot("man foot");
    }

    @Override
    public Person buildPerson() {
        // TODO Auto-generated method stub
        return p;
    }
}

class PersonDirect {
    public Person constructPerson(PersonBuilder p) {
        p.buildbody();
        return p.buildPerson();
    }
}
View Code

     5 原型模式,涉及3个角色, 客户端(提出创建对象请求),抽象原型角色(给出具体原型需要接口),具体原型角色,通过一个原型对象指明创建对象的类型,通过复制对象的方法创建对象。

技术分享
interface My extends Cloneable {
    public Object clone();
}

class CMy implements My{
    public Object clone() {
        try {
            return super.clone();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }
}
class Client{
    My getMy(My m) {
        return (My) m.clone();
    }
}
View Code

 

设计模式总结

标签:对象   sys   通过   xtend   turn   span   过程   工厂方法模式   oid   

原文地址:http://www.cnblogs.com/whesuanfa/p/7612621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!