java程序的设计原则
6大原则:
单一职责:一个类和方法只做一件事。
开闭原则:对修改关闭,对扩展开发。
里氏替换原则:子类可扩展新方法,但不可修改父类已有方法(父类已提供了具体实现的方法)。
依赖倒置:依赖于抽象,而非具体实现,即面向接口编程(如方法参数,类属性使用接口声明,这样可接收任何子类)。
接口隔离:使用多个隔离的接口定义抽象,降低耦合。
最少知道/迪米特原则:降低类之间的依赖,聚合,组合等。
1:装饰设计模式
装饰模式是结构型设计模式的其中一种,用来对现有类进行功能的增强,可能有的同学说,增强我可以使用继承,也可以使用AOP啊,是的,当然可以,但是前者会增加继承体系的复杂度,增加后期系统的维护难度,而AOP本身使用的复杂度就比较高,而且严重依赖spring(这个准确来说不是个问题,因为现在的项目可能没有不使用spring的了吧!)。装饰模式主要包含如下的角色:
抽象构件角色(Component):需要被装饰类的顶层接口。
具体构建角色(ConcreteComponent):需要被装饰的类。
抽象装饰角色(Decorator):继承抽象构件,并包含具体构建角色实例,然后通过子类扩展具体功能。
具体装饰角色(ConcreteDecorator):继承抽象装饰角色,进行装饰增强。
实际使用时可以根据实际情况灵活调整,并非必须保证每个角色都有提供。
UML类图如下:
接下来我们将上图的UML翻译为真正的代码实现:
源码 。
1.1:Component
// 抽象构件角色
interface Component {
void operation();
}
1.2:ConcreteComponent
// 具体构件角色
class ConcreteComponent implements Component {
public ConcreteComponent() {
System.out.println("创建具体构件角色");
}
public void operation() {
System.out.println("调用具体构件角色的方法operation()");
}
}
1.3:Decorator
// 抽象装饰角色
class Decorator implements Component {
private Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
1.4:ConcreteDecorator
// 具体装饰角色
class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
public void operation() {
super.operation();
addedFunction();
}
public void addedFunction() {
System.out.println("为具体构件角色增加额外的功能addedFunction()");
}
}
1.5:测试
public class DecoratorPattern {
public static void main(String[] args) {
Component p = new ConcreteComponent();
p.operation();
System.out.println("---------------------------------");
Component d = new ConcreteDecorator(p);
d.operation();
}
}
输出:
接下来我们通过一个实际的场景来看下如何使用。
2:莫莉卡·安斯兰
源码 。
2.1:场景分析
以下偷了个懒,参考文章写的很棒,直接复制过来了(外话:呸,不要脸就不要脸,还说的这么冠冕堂皇)!!!
在《恶魔战士》中,游戏角色“莫莉卡·安斯兰”的原身是一个可爱少女,但当她变身时,会变成头顶及背部延伸出蝙蝠状飞翼的女妖,当然她还可以变为穿着漂亮外衣的少女。这些都可用装饰器模式来实现,在本实例中的“莫莉卡”原身有 setImage(String t) 方法决定其显示方式,而其 变身“蝙蝠状女妖”和“着装少女”可以用 setChanger() 方法来改变其外观,原身与变身后的效果用 display() 方法来显示(点此下载其原身和变身后的图片),图 2 所示是其结构图:
2.2:程序实现
2.2.1:Morrigan->Component
interface Morrigan {
void display();
}
2.2.2:Original->ConcreteComponent
class Original extends JFrame implements Morrigan {
private static final long serialVersionUID = 1L;
private String t = "Morrigan0.jpg";
public Original() {
super("《恶魔战士》中的莫莉卡·安斯兰");
}
public void setImage(String t) {
this.t = t;
}
public void display() {
this.setLayout(new FlowLayout());
JLabel l1 = new JLabel(new ImageIcon("src/main/java/dongshi/daddy/decorator/morrigan/" + t));
this.add(l1);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
2.2.3:Changer->Decorator
class Changer implements Morrigan {
protected Morrigan m;
public Changer(Morrigan m) {
this.m = m;
}
public void display() {
m.display();
}
}
2.2.4:Girl可爱少女和Succubus女妖->ConcreteDecorator
// 具体装饰角色:少女
class Girl extends Changer {
public Girl(Morrigan m) {
super(m);
}
public void display() {
setChanger();
super.display();
}
public void setChanger() {
((Original) super.m).setImage("Morrigan2.jpg");
}
}
// 具体装饰角色:女妖
class Succubus extends Changer {
public Succubus(Morrigan m) {
super(m);
}
public void display() {
setChanger();
super.display();
}
public void setChanger() {
((Original) super.m).setImage("Morrigan1.jpg");
}
}
2.2.5:测试
public class MorriganAensland {
public static void main(String[] args) {
Morrigan m0 = new Original();
m0.display();
Morrigan m1 = new Succubus(m0);
m1.display();
Morrigan m2 = new Girl(m0);
m2.display();
}
}
运行如下:
违规点击这里 。