简述:
对类来说,及一个类应该只负责一项职责,如类A负责两个不同职责:职责1,职责2。当职责1需求变更而改变A时,可能造成职责2执行错误,所以需要将类A的粒子分解为A1,A2。可在方法级别上(其实不是真正的遵守,倘若职责简单可用),类级别上遵守,以实际业务为准。
注意事项:
简述:
接口隔离后:
public class Segregation1 {
public static void main(String[] args) {
A a=new A();
a.depend1(new B());
a.depend2(new B());
a.depend3(new B());
C c=new C();
c.depend1(new D());
c.depend2(new D());
c.depend3(new D());
}
}
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 depend2(Interface3 i){
i.operation4();
}
public void depend3(Interface3 i){
i.operation5();
}
}
简述:
public class DependencyInversion {
public static void main(String[] args) {
Person person=new Person();
person.receive(new Email());
person.receive(new WeChat());
}
}
interface IReceiver{
public String getInfo();
}
class Email implements IReceiver{
public String getInfo(){
return "电子邮件信息:HelloWorld!";
}
}
class WeChat implements IReceiver{
public String getInfo() {
return "微信:Hello Baby!";
}
}
class Person{
public void receive(IReceiver receiver){
System.out.println(receiver.getInfo());
}
}
通过接口传递实现依赖:
public class DependencyInversion02 {
public static void main(String[] args) {
ChangHong changHong=new ChangHong();
OpenAndClose openAndClose=new OpenAndClose();
openAndClose.open(changHong);
}
}
interface IOpenAndClose{
public void open(ITV tv);
}
interface ITV{
public void play();
}
class ChangHong implements ITV{
@Override
public void play() {
System.out.println("长虹电视打开!!");
}
}
//实现接口
class OpenAndClose implements IOpenAndClose{
@Override
public void open(ITV tv) {
tv.play();
}
}
通过构造方法依赖传递:
public class DependencyInversion03 {
public static void main(String[] args) {
MeiDi meiDi=new MeiDi();
OpenAndClose2 openAndClose2=new OpenAndClose2(meiDi);
openAndClose2.open();
}
}
interface IOpenAndClose2{
public void open();
}
interface ITV2{
public void play();
}
class MeiDi implements ITV2{
@Override
public void play() {
System.out.println("美的电视打开!!");
}
}
class OpenAndClose2 implements IOpenAndClose2 {
public ITV2 tv;
public OpenAndClose2(ITV2 tv) {
this.tv = tv;
}
@Override
public void open() {
tv.play();
}
}
通过setter方法传递:
public class DependencyInversion04 {
public static void main(String[] args) {
TCL tcl=new TCL();
OpenAndClose3 openAndClose3=new OpenAndClose3();
openAndClose3.setTv(tcl);
openAndClose3.open();
}
}
interface IOpenAndClose3{
public void open();
}
interface ITV3{
public void play();
}
class TCL implements ITV3{
@Override
public void play() {
System.out.println("TCL电视打开!");
}
}
class OpenAndClose3 implements IOpenAndClose3{
private ITV3 tv;
public void setTv(ITV3 tv) {
this.tv = tv;
}
@Override
public void open() {
tv.play();
}
}
注意事项:
简述:
B类原继承A类,改进后:
class Base{}
class A extends Base{
public int func1(int n1,int n2){
return n1-n2;
}
}
class B extends Base{
private A a=new A();
public int func1(int n1, int n2) {
return n1+n2;
}
public int func2(int n1,int n2){
return func1(n1, n2)+9;
}
public int func3(int n1,int n2){
return a.func1(n1,n2);
}
}
简述:
例子(绘制图形):
class GraphicEditor{
public void drawShape(Shape s){
if (s.m_type==1) drawRectangle(s);
else if (s.m_type==2) drawCircle(s);
}
public void drawRectangle(Shape r){
System.out.println("绘制矩形");
}
public void drawCircle(Shape r){
System.out.println("绘制圆形");
}
}
class Shape{
int m_type;
}
class Rectangle extends Shape{
Rectangle(){
super.m_type=1;
}
}
class Circle extends Shape{
Circle(){
super.m_type=2;
}
}
我们发现,倘若要加一个新的图形,会有很多的修改,不符合OCP原则。
改进后:
public class OCPTest {
public static void main(String[] args) {
GraphicEditor graphicEditor=new GraphicEditor();
graphicEditor.drawShape(new Circle());
graphicEditor.drawShape(new Rectangle());
}
}
class GraphicEditor{
public void drawShape(Shape s){
s.draw();
}
}
abstract class Shape{
int m_type;
public abstract void draw();
}
class Rectangle extends Shape{
Rectangle(){
super.m_type=1;
}
@Override
public void draw() {
System.out.println("绘制矩形");
}
}
class Circle extends Shape{
Circle(){
super.m_type=2;
}
@Override
public void draw() {
System.out.println("绘制圆形");
}
}
class Triangle extends Shape{
Triangle(){
super.m_type=2;
}
@Override
public void draw() {
System.out.println("绘制三角形");
}
}
简述:
小结:
原则是尽量使用合成/聚合的方式,而不是使用继承。
因为继承关系的耦合度太强,所以一般设计时我们会将关系转换为依赖,聚合或者是组合。
依赖(合成):
如有一个类A,中间有3个方法,一个类B只想用其中的一个方法,可以将A设为方法的参数类型,然后用a调A中的方法。
聚合:
如有一个类A,中间有3个方法,一个类B只想用其中的一个方法,还可以在类B中设一个A的属性,然后用A中的方法。
**组合:**在类B中实例化了一个类A,A a=new A();。
UML Unified modeling language 统一建模语言
是一种用于软件系统分析和设计的语言工具,它用于帮助团建开发人员进行思考和记录思路的结果。
dependency 依赖
Generalization 泛化(继承)
Implementation 实现
Association 关联
Aggregation 聚合
Composite 组合
下一篇:学习笔记【23种设计模式-第二节:Singleton单例模式】