在编写软件过程中,程序员面临着来种子耦合性、内聚性及可维护性,可扩展性,重用性、灵活性等多方面的挑战, 设计模式是为了让程序具有更好的
设计模式原则,其实就是程序员在编程时,应当遵守的原则,也是各种设计模式的基础(设计模式为什么这样设计的依据)
设计模式常用七大原则:
基本介绍
对类来说,即一个类应该只负责一项职责。如果类A负责两个不同的职责:职责1,职责2。当职责1需求改变而改变A时,可能造成职责2的执行错误,所以需要将A的粒度分解为A1,A2。
举例:
public class SingleResponsibility1 {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.run("摩托车");
vehicle.run("汽车");
vehicle.run("飞机");
}
}
//交通工具类
//方式1
// 1.在方式1 的run 方法中违反了单一职责原则
// 2.解决方案:根据交通工具的运行方式不同,分解成不同的类即可
class Vehicle{
public void run(String vehicle){
System.out.println(vehicle + "在公路上跑。。。");
}
}
public class SingleResponsibility2 {
public static void main(String[] args) {
RoadVehicle roadVehicle = new RoadVehicle();
roadVehicle.run("汽车");
roadVehicle.run("摩托车");
AirVehicle airVehicle = new AirVehicle();
airVehicle.run("飞机");
}
}
//方案2的分析:
//1.遵守了单一职责原则
//2.但是这样做花销很大
//3.改进:直接修改Vehicle类,改动的代码会比较少 ==> 方案3
class RoadVehicle{
public void run (String vehicle){
System.out.println(vehicle + "在公路运行");
}
}
class AirVehicle{
public void run (String vehicle){
System.out.println(vehicle + "在天空运行");
}
}
class WaterVehicle{
public void run (String vehicle){
System.out.println(vehicle + "在水中运行");
}
}
public class SingleResponsibility3 {
public static void main(String[] args) {
Vehicle2 vehicle2 = new Vehicle2();
vehicle2.run("汽车");
vehicle2.runAir("飞机");
vehicle2.runWat("轮船");
}
}
//方式三:
//1.这种修改方法没有对原来的类做大修改,只是修改方法
//2.这里虽然没有在类这个级别上遵守单一职责原则,但是在方法这个级别上仍然是遵守单一职责原则
class Vehicle2{
public void run(String vehicle){
System.out.println(vehicle + "在公路上跑。。。");
}
public void runAir(String vehicle){
System.out.println(vehicle + "在天空运行");
}
public void runWat(String vehicle){
System.out.println(vehicle + "在水中运行");
}
}
基本介绍
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该简历在最小的接口上
应用实例:
类A通过接口依赖于B,类C通过接口依赖于D
interface Interface1{
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1{
@Override
public void operation1() {
System.out.println("B中实现了operation1");
}
@Override
public void operation2() {
System.out.println("B中实现了operation2");
}
@Override
public void operation3() {
System.out.println("B中实现了operation3");
}
@Override
public void operation4() {
System.out.println("B中实现了operation4");
}
@Override
public void operation5() {
System.out.println("B中实现了operation5");
}
}
class D implements Interface1{
@Override
public void operation1() {
System.out.println("D中实现了operation1");
}
@Override
public void operation2() {
System.out.println("D中实现了operation2");
}
@Override
public void operation3() {
System.out.println("D中实现了operation3");
}
@Override
public void operation4() {
System.out.println("D中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D中实现了operation5");
}
}
class A{
public void depend1(Interface1 i){
i.operation1();
}
public void depend2(Interface1 i){
i.operation2();
}
public void depend3(Interface1 i){
i.operation3();
}
}
class C {
public void depend1(Interface1 i){
i.operation1();
}
public void depend4(Interface1 i){
i.operation4();
}
public void depend5(Interface1 i){
i.operation5();
}
}
应传统方法的问题和使用接口隔离原则改进
public class Segregation2 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B()); //A类通过接口去依赖B类
a.depend2(new B());
a.depend3(new B());
}
}
interface Interface1{
void operation1();
}
interface Interface2{
void operation2();
void operation3();
}
interface Interface3{
void operation4();
void operation5();
}
class B implements Interface1 , Interface2{
@Override
public void operation1() {
System.out.println("B中实现了operation1");
}
@Override
public void operation2() {
System.out.println("B中实现了operation2");
}
@Override
public void operation3() {
System.out.println("B中实现了operation3");
}
}
class D implements Interface1 , Interface3{
@Override
public void operation1() {
System.out.println("D中实现了operation1");
}
@Override
public void operation4() {
System.out.println("D中实现了operation4");
}
@Override
public void operation5() {
System.out.println("D中实现了operation5");
}
}
class A{
public void depend1(Interface1 i){
i.operation1();
}
public void depend2(Interface2 i){
i.operation2();
}
public void depend3(Interface2 i){
i.operation3();
}
}
class C {
public void depend1(Interface1 i){
i.operation1();
}
public void depend4(Interface3 i){
i.operation4();
}
public void depend5(Interface3 i){
i.operation5();
}
}