Java设计模式
文章平均质量分 75
关于Java开发中常用的设计模式分析及我见
键圣
一个热衷于不惑的非专业不靠谱程序员
展开
-
类与类之间的关系
在java以及其他的面向对象设计模式中,类与类之间主要有6种关系,他们分别是:依赖、关联、聚合、组合、继承、实现。他们的耦合度依次增强。1. 依赖(Dependence) 依赖关系的定义为:对于两个相对独立的对象,当一个对象负责构造另一个对象的实例,或者依赖另一个对象的服务时,这两个对象之间主要体现为依赖关系。定义比较晦涩难懂,但在java中的表现还是比较直观的:类原创 2017-05-21 23:39:36 · 862 阅读 · 0 评论 -
软件设计六大原则
单一职责原则(SRP:Single responsibility principle): There should never be more than one reason for a class to change. 也就是说一个类只负责一件事。好处: 1.类的复杂性降低,一个类负责什么事,一目了然。 2.类的可读性提高,复杂性降低,原创 2017-05-21 23:36:52 · 1166 阅读 · 0 评论 -
单例模式
定义Ensure a class has only one instance, and provid a global point of access to it.(确保莫一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。)实现方法 1.提供private构造函数 2.提供一个可以访问private的实例公共的方法实现方式第一种形式原创 2017-05-22 08:04:52 · 365 阅读 · 0 评论 -
工厂模式&抽象工厂模式
工厂模式工厂模式总的来说就是使用工厂方法来代替new创建对象。主要用来封装对象创建过程的复杂性。可分为三类:1)简单工厂模式(Simple Factory) 2)工厂方法模式(Factory Method) 3)抽象工厂模式(Abstract Factory)简单工厂模式建立一个工厂(一个函数或一个类方法)来制造新的对象。/** * Created by du原创 2017-05-23 23:48:32 · 749 阅读 · 0 评论 -
模板方法模式
定义Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. template Method lets subclasses redefine certain steps of algorithm without changing the algorithm's struct原创 2017-05-24 00:04:07 · 617 阅读 · 0 评论 -
观察者模式
观察者模式定义:Define a one-to-many dependency between objects so that when one object changes state, all its dependency are notified and updated automatically.翻译过来就是,定义一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象原创 2017-03-23 10:30:01 · 418 阅读 · 0 评论 -
建造者模式
定义Separate thr construction of a complex objrct from its representation so that the sanem construction process can create different representations.(将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示)类图原创 2017-06-07 16:06:50 · 501 阅读 · 0 评论 -
原型模式
定义Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. (用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象)通用类图原型模式主要用于对象的复制,它的核心是就是类图中的原型类P原创 2017-06-07 17:11:29 · 373 阅读 · 0 评论 -
适配器模式
定义Convert thr interface of a class into another interface clients expect. Adapter lets classes work together taht couldn't otherwise because of incompatible interfaces.(将一个类的接口变成客户端所期待的另一种接口,从而使原创 2017-06-12 11:22:52 · 576 阅读 · 0 评论 -
桥梁模式
定义Decouple an abstraction from its implementation so taht the two can vary independently.(将抽象和实现解耦,使得两者可以独立地变化)通用类图 角色分析:Abstraction --抽象化角色它的主要职责是定义出该角色的行为,同时保存一个对实现化角色的引用,该原创 2017-06-13 16:02:34 · 642 阅读 · 0 评论