public interface Alldriver {
public void drive(ICar car);
}
public interface ICar {
public void run();
}
public class Benc implements ICar {
@Override
public void run() {
System.out.println("奔驰车飞驰-------------");
}
}
public class Hmw implements ICar {
@Override
public void run() {
System.out.println("宝马车飞驰-------------");
}
}
public class Clients {
public static void main(String[] args) {
Driver driver = new Driver();
driver.drive(new Hmw());
//这里体现了依赖倒置原则!
//在Driver 的drive方法里面传入ICar类型的对象,这样他所有的子接口创建的对象都能传进来,提高了司机的功能多样性。
driver.drive(new Benc());
}
}
public class Driver implements Alldriver {
@Override
public void drive(ICar car) {
System.out.println("司机开车");
car.run();
}
}
接口隔离原则
public class Star {
/**
* 本案例中,明星类并没有直接和粉丝、公司接触,而是通过经纪人在其中充当桥梁
* 实现见面和合作。
* 这便是迪米特原则!
*/
class Agents{
Star1 p = new Star1("迪丽热巴");
void meet(Fans fans){
System.out.println("" + fans.getName() + "和" + p.getName() + "见面!");
}
void cooperate(Company c){
System.out.println("" + c.getName() + "和" + p.getName() + "艺人商谈合作事项!");
}
}
class Star1{
private String name;
public Star1(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Fans{
private String name;
public Fans(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Company{
private String name;
public Company(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
Star s = new Star();
Agents a = s.new Agents();
a.meet(s.new Fans("小九"));
a.cooperate(s.new Company("*电视台"));
}
}
迪米特原则
public interface Butity {
void goodLook();
void goodTemper();
void goodFigure();
}
//一个接口中包含了太多的方法,代码的耦合度太高,淫荡改为多个接口实现这些方法。
/**
*这里建议改为:
* public interface Temper{
* void goodTemper();
* }
* public interface Figure{
* void goodFigure();
* }
* public interface Look{
* void goodLook();
* }
* 这样改完,上面的Butity接口就不需要了。
*
*/
public class Girl1 implements Butity{
@Override
public void goodLook() {
System.out.println("脸蛋倾国倾城---------");
}
@Override
public void goodTemper() {
System.out.println("爱everything!");
}
@Override
public void goodFigure() {
System.out.println("很瘦,但该有肉的地方都有肉!");
}
}
public interface Search {
void search(Butity girl);
}
public class Searcher implements Search{
@Override
public void search(Butity girl) {
System.out.println("发现一位美女!");
girl.goodFigure();
girl.goodLook();
girl.goodTemper();
}
}
public class Test {
public static void main(String[] args) {
Searcher p = new Searcher();
p.search(new Girl1());
}
}
DP单例模式
public class Emperor {
private static Emperor emperor = null;
private Emperor() {
}
public static Emperor getInstance(){
if(emperor == null){
emperor = new Emperor();
}
return emperor;
}
/**
* 从这个方法之后,皇帝的对象就不再改变了
*/
public void say(){
System.out.println("我是皇帝xxx,有事启奏,无事退朝----------");
}
}
public class Minister {
void meet() {
//大臣上朝
Emperor emperor = Emperor.getInstance();
//每次大臣上朝的对象都是同一个皇上
for (int i = 0; i < 5; i++) {
System.out.println("大臣们上朝!");
emperor.say();
}
}
}
public class Test {
public static void main(String[] args) {
Minister m = new Minister();
m.meet();
}
}