@BIGBALLON
2015-06-29T12:33:16.000000Z
字数 10619
阅读 2117
说实话对于面向对象的研究并不是很深,理解自然也不是很深刻。
软件结构体系这门课还是计算机系的老师来给我们带的课,却是本学期所有老师里面教得最好的课了,如果看不懂UML图和不了解设计模式,真的很难说自己是软件工程专业毕业的学生,惭愧。
下面开始小结下设计模式吧。
设计模式是一套被反复使用、多人知晓的、经过分类编目的、代码设计经验的总结
每一个设计模式描述一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次一次地使用该方案而不必做重复劳动。
| 设计原则名称 | 设计原则简介 |
|---|---|
| 单一职责原则 (SRP) | 类的职责要单一,不能将太多的职责放在一个类中。 |
| 开放封闭原则 (OCP) | 软件实体对扩展是开放的,对修改是关闭的 |
| 里氏代换原则 (LSP) | 一个可以接受基类对象的地方必然可以接受一个子类对象 |
| 依赖倒置原则 (DIP) | 要针对抽象层编程,而不要针对具体类编程 |
| 接口隔离原则 (ISP) | 使用多个专用接口来取代一个统一的接口 |
| 合成复用原则 (CRP) | 尽量多使用组合和聚合关联关系,尽量少使用甚至不使用继承关系 |
| 迪米特法则 (LoD) | 一个软件实体对其他实体的引用越少越好 |
创建型模式,就是创建对象的模式,抽象了实例化的过程。它帮助一个系统独立于如何创建、组合和表示它的那些对象。
创建型模式关注的是对象的创建,将创建对象的过程进行了抽象和封装,作为客户程序仅仅需要去使用对象,而不再惯性创建对象过程中的逻辑。
当你想使用new关键字的时候,就可以考虑创建型模式
结构型模式(Structural Pattern)描述如何将类或者对象结合在一起形成更大的结构,就像搭积木,可以通过简单积木的组合形成复杂的、功能更为强大的结构。
行为型模式分为类行为型模式和对象行为型模式两种:
Factory Method Pattern: Define an interface for creating an object, but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses
工厂方法模式又称为工厂模式,也叫做虚拟构造器(Virtual Constructor)模式或者多态工厂(polymorphic Factory)模式
简单工厂模式,可以根据参数的不同类型返回不同类的实例
简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有相同的父类
简单工厂模式的核心:一个工厂,多种厂品
由于只有一个工厂,不用产生具体的实例,可以直接用静态方法,因此简单工厂模式也经常称为静态工厂模式(static factory pattern)


public interface Human{public void getColor(); //每个人种的皮肤都有相应的颜色public void talk(); //人类会说话}public class YellowHuman implements Human{public void getColor(){System.out.println("黄色");}public void talk(){System.out.println("黄种人,双字节");}}public class HumanFactory{public static <T extends Human> T createHuman( Class<T> c ){Human human = null;try{human = (Human)CLass.forName(c.getName()).newInstance();}catch( Exception e ){System.out.println("人种生成错误");}return (T)human;}}public NvWa{public static void main( String[] args ){System.out.println("---造出第一个人---");Human whiteHuman = HumanFactory.createHuman( whiteHuman.class );whiteHuman.getColor();whiteHuman.talk();System.out.println("---造出第二个人---");Human blackHuman = HumanFactory.createHuman( blackHuman.class );blackHuman.getColor();blackHuman.talk();System.out.println("---造出第三个人---");Human yellowHuman = HumanFactory.createHuman( yellowHuman.class );yellowHuman.getColor();yellowHuman.talk();}}

public abstract class AbstractHumanFactory{public abstract <T extends Human> T createHuman( Class<T> c );}public class HumanFactory extends AbstractHumanFactory{public <T extends Human> T createHuman( Class<T> c ){Human human = null;try{human = (Human)Class.forName( c.getName() ).newInstance();}catch ( Exception e ){System.out.println("人种生成错误");}return (T)human;}}public class NvWa{public static void main( String[] args ) {//声明阴阳八卦炉AbstractHumanFactory YinYangLu = new HumanFactory();Human whiteHuman = YinYangLu.createHuman(whiteHuman.class);whiteHuman.getColor();whiteHuman.talk();}}
Abstract Factory Pattern: Provide an interface for creating families of ralated or dependent objects without specifying their concrete classes
抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。
抽象工厂模式又称为工具(Kit)模式
抽象工厂模式中的具体工厂不只是创建的一种产品,它负责创建一族产品
当一个工厂等级结构可以创建出分别属于不同产品等级结构的一个产品族中的所有对象时,抽象工厂模式比工厂方法模式更为简单、更有效率

//抽象产品:人种接口的实现public interface Human{public void getColor(); //每个人种的皮肤都有相应的颜色public void talk(); //人类会说话public void getSex();}//子抽象产品:各色人种的接口的实现public abstract class AbstractYellowHuman implements Human{public void getColor(){System.out.println("黄色");}public void talk(){System.out.println("黄种人,双字节");}}//具体产品类public class FemaleYellowHuman extends AbstractYellowHuman{public void getSex(){System.out.println("黄种女性");}}public class MaleYellowHuman extends AbstractYellowHuman{public void getSex(){System.out.println("黄种男性");}}//抽象工厂public interface HumanFactory{public Human createYellowHuman();public Human createWhiteHuman();public Human createBlackHuman();}//具体工厂public class FemaleFactory implements HumanFactory{public createYellowHuman(){return new FemaleYellowHuman();}.........}public class MaleFactory implements HumanFactory{public createYellowHuman(){return new FemaleYellowHuman();}.........}public class NvWa{public static void main(String[] args) {HumanFactory maleHumanFactory = new MaleFactory();HumanFactory femaleHumanFactory = new FemaleFactory();Human maleYellowHuam = maleHumanFactory.createYellowHuman();maleYellowHuam.getColor();maleYellowHuam.talk();maleYellowHuam.getSex();}}
Facade Pattern: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
外观模式又翻译为门面模式,它是一种对象结构型模式


//子系统角色类public class AirConditioner{public void on(){...}public void off(){...}}public class Television{...}public class Fan{...}public class Light{private String position;public Light( String position ){this.position = position;}public void on(){}public void off(){}}//外观角色类public class GenralSwitchFacade{private Light lights[] = new Light[4];private Fan fan;private AirConditioner ac;private Television tv;public GenralSwitchFacade(){lights[0] = new Light( "左前");lights[1] = new Light( "右前");lights[2] = new Light( "左后");lights[3] = new Light( "右后");fan = new Fan();ac = new AirConditioner();tv = new Television();}public void on(){lights[0].on();lights[1].on();lights[2].on();lights[3].on();fan.on();ac.on();tv.on();}public void off(){lights[0].off();lights[1].off();lights[2].off();lights[3].off();fan.off();ac.off();tv.off();}}
Iterator Pattern: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
迭代器模式(Iterator Pattern) :提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor)。迭代器模式是一种对象行为型模式。
一个聚合对象,如一个列表(list)或者一个集合(set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构。
针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我们并不希望在聚合对象的抽象层接口中充斥着各种不同遍历的操作。
怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,这就是迭代器模式所要解决的问题。
怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,这就是迭代器模式所要解决的问题。


//抽象迭代器public interface TVIterator{void setChannel( int i );void next();void previous();boolean isLast();Objects currentChannel();boolean isFirst();}//抽象聚合类public interface Television{TVIterator createIterator();}//具体聚合类和具体迭代类public class TCLTelevision implements Television{private Objects[] obj = {"湖南卫视","北京卫视","上海卫视","湖北卫视","黑龙江卫视"};public TVIterator createIterator(){return new TCLIterator();}class TCLIterator implements TVIterator{private int currentIndex = 0;public void next(){if( currentIndex < obj.length ){currentIndex++;}}public void previous(){if( currentIndex > 0 ){currentIndex--;}}public void setChannel( int i ){currentIndex = i;}public Objects currentChannel(){return obj[currentIndex];}public boolean isFirst(){...}public boolean isLast(){...}}}
Observer Pattern: Define a one-to-many dependency between objects so tha when one object changes state, all its dependents are notified and updated automatically
观察者模式:定义对象间的一种一对多的依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新。观察者模式又叫做发布-视图(Model/View)模式,源-监听器(Source/Listener)模式或从属者(Dependents)模式。观察者模式是一种对象行为型模式


import java.util.*;//抽象目标public abstract class MySubject{protected ArrayList observers = new ArrayList();public void attach( MyObserver observer ){observers.add( observer );}public void detach( MyObserver observer ){observers.remove( observer );}public abstract void cry(); //抽象通知方法}//抽象观察者public interface MyObserver{void response();}//具体目标public class Cat extends MySubject{public void cry(){System.ou.println( "猫叫!" );for (Object obs:observers) {((MyObserver)obs).response();}}}//具体观察者public class Mouse implements MyObserver{public void response(){System.ou.println( "老鼠跑!" );}}public class Dog implements MyObserver{public void response(){System.ou.println( "狗叫!" );}}//客户端public class Client{public static void main(String[] args) {MySubject subject = new Cat();MyObserver obs1, obs2, obs3;obs1 = new Mouse();obs2 = new Mouse();obs3 = new Dog();subject.attach(obs1);subject.attach(obs2);subject.attach(obs3);subject.cry();}}

//抽象目标类public interface IHanFeiZi{public void haveBreakfast();public void haveFun();}//抽象观察者类public interface ILiSi{public update( String context );}//具体目标类public class HanFeiZi implements IHanFeiZi{private ILiSi liSi = new LiSi();public void haveBreakfast(){System.out.println( "韩非子:开始吃饭了...")this.liSi.update( "韩非子在吃饭");}public void haveFun(){System.out.println( "韩非子:开始娱乐了...")this.liSi.update( "韩非子在娱乐");}}//具体观察者类public class LiSi implements ILiSi{public void update( String str ){System.out.println( "李斯:观察到韩非子活动,开始向老板汇报了...");this.reportToQiShiHuang( str );System.out.println( "李斯:汇报完毕...");}private void reportToQiShiHuang( String reportContext ){System.out.println( "李斯:报告,秦老板!韩非子有活动了..."+ reportContext );}}//客户端public class Client{public static void main(String[] args) {HanFeiZi hanFeiZi = new HanFeiZi();hanFeiZi.haveBreakfast();hanFeiZi.haveFun();}}
软件开发模型
渐增模型:由一组有计划的、循环渐增的、不断改进的过程版本组成。
各个中间版本可以并行开发。
演化模型(原型模型):通过系统各个可执行的中间版本以渐增的形式来开发系统的,但是客户需求可以分步逐渐了解,不用在初始时就确定。
螺旋模型:螺旋模型沿着螺线旋转,在四个象限上分别表达四个方面的活动,制定计划、风险分析、实施工程和客户评估
螺旋模型将瀑布模型与快速原形模型结合起来,并且加入两种模型均忽略了的风险分析。
优点:对可选方案和约束条件的强调有利于已有软件的重用,也有助于把软件质量作为软件开发的一个重要目标;
维护和开发之间并没有本质区别。
缺点:风险驱动的
主要适用于内部开发的大规模软件项目

