Dependency Inversion Principle multiplexing principle in Synthesis Example ---

Dependency Inversion Principle and multiplexing principles Synthesis

 

 

Providing a variety of different sizes of brushes in a drawing software, and may specify a different color to the brush, a designer of the brush was designed as shown above in FIG. By analysis, it was found to increase the types and brush color will make the number of classes in the system increases dramatically, please reconstruct synthesized according to the above design principles and reuse Dependency Inversion Principle.

 

Rely on the reverse principle : the program depends on the abstract interface, do not depend on the specific implementation. Simply put, it is to ask for an abstract program, not to achieve programming, thus reducing the coupling between the client and the implementation module.

Synthesis multiplexing principle : it requires a software reuse, to try to achieve using other association or polymeric composition, followed before considering the use of inheritance to achieve

FIG reconstructed class:

 

Dependency Inversion Principle Synthesis multiplexing principle: synthetic Dependency Inversion Principle and multiplexing principle, according to Pen write two abstract classes size color Size and Color ; underlying concrete class extends the abstract class (generalization), high Pen class of Size and Color polymerized from (aggregation relationship);

 

 

Code:

Model abstract classes

public abstract class Size {
public abstract void sizePen();
}

Specific model class

public class SmallPen extends Size{
//小型
public void sizePen() {
System.out.println("小型");
}
}
public class MiddlePen extends Size{
//中型
public void sizePen() {
System.out.println("中型");
}
}
public class BigPen extends Size{
//大型
public void sizePen() {
System.out.println("大型");
}
}

颜色抽象类

public abstract class Color {
public abstract void colorPen();
}

具体颜色类

public class GreenPen extends Color{
public void colorPen() { System.out.println("绿色"); } }

public class RedPen extends Color{

 
 

public void colorPen() {

 
 

System.out.println("红色");

 
 

}

 
 

}

 

钢笔

public class Pen {
//钢笔
private Size size;
private Color color;
public Size getSize() {
 return size;
}
public void setSize(Size size) {
 this.size = size;
}
public Color getColor() {
 return color;
}
public void setColor(Color color) {
 this.color = color;
}
}

 

聚合钢笔

public class MianClass {
public static void main(String[] args) {
//颜色大小随意组合
Pen pen=new Pen();
//组合小型红色钢笔
pen.setSize(new SmallPen());
pen.setColor(new RedPen());
//组合大型绿色钢笔
pen.setSize(new BigPen());
pen.setColor(new GreenPen());
}
}

 

 

Guess you like

Origin www.cnblogs.com/sengzhao666/p/12031473.html