桥梁设计模式实现
我为桥梁设计模式创建了一个示例。实施此功能还是我错过的东西是正确的吗?
方案:有抽象类的房屋,它具有混合类(平坦和公寓)和油漆的抽象方法涂料是另一个具有抽象的油漆方法的抽象类(用于绘画房屋)。和一个彩色拾音器的枚举。
abstract class Home {
private String homeType;
private Paint paint;
public Home(String type, Paint paint) {
this.homeType = type;
this.paint = paint;
}
abstract void paintTheHome();
public String getHomeType() {
return homeType;
}
public Paint getPaint() {
return paint;
}
}
public class Flat extends Home{
public static final String Type="Flat";
public Flat(Paint paint) {
super(Type,paint);
}
@Override
void paintTheHome() {
getPaint().paint(this);
}
}
public class Apartment extends Home {
public static final String Type="Apartment";
Apartment(Paint paint){
super(Type,paint);
}
@Override
void paintTheHome() {
getPaint().paint(this);
}
}
public abstract class Paint {
private Color color;
private String brand;
public Paint(Color color, String brand) {
this.color=color;
this.brand = brand;
}
public Color getColor() {
return color;
}
public String getBrand() {
return brand;
}
abstract void paint(Home home);
}
public class AsianPaint extends Paint{
public static final String brand = "Asian Paint";
public AsianPaint(Color color) {
super(color,brand);
}
@Override
void paint(Home home) {
System.out.println("Painting Started for "+home.getHomeType());
System.out.println("Brand of Color"+this.getBrand());
System.out.println("Name of Color"+this.getColor());
System.out.println("painting Ended");
}
}
public class ShalimarPaint extends Paint{
public static final String brand = "Shalimar Paint";
public ShalimarPaint(Color color) {
super(color,brand);
}
@Override
void paint(Home home) {
System.out.println("Painting Started for "+home.getHomeType());
System.out.println("Brand of Color"+this.getBrand());
System.out.println("Name of Color"+this.getColor());
System.out.println("painting Ended");
}
}
public enum Color {
RED("Red"),
GREEN("Green"),
BLUE("Blue"),
WHITE("White"),
YELLOW("Yellow");
String colorName;
Color(String string) {
this.colorName = string;
}
String getColor(){
return colorName;
}
}
public class client {
public static void main(String[] args) {
Home home = new Apartment(new AsianPaint(Color.RED));
home.paintTheHome();
home = new Flat(new ShalimarPaint(Color.GREEN));
home.paintTheHome();
}
}
I have created one example for bridge design pattern. is it correct way to implement this or there is something I missed it?
Scenario : there is abstraction class Home which have abstract method paint implement by concreate classes(Flat and Apartment) and paint is another abstract class which having abstract method of paint(to paint the house). and one enum for color pickup.
abstract class Home {
private String homeType;
private Paint paint;
public Home(String type, Paint paint) {
this.homeType = type;
this.paint = paint;
}
abstract void paintTheHome();
public String getHomeType() {
return homeType;
}
public Paint getPaint() {
return paint;
}
}
public class Flat extends Home{
public static final String Type="Flat";
public Flat(Paint paint) {
super(Type,paint);
}
@Override
void paintTheHome() {
getPaint().paint(this);
}
}
public class Apartment extends Home {
public static final String Type="Apartment";
Apartment(Paint paint){
super(Type,paint);
}
@Override
void paintTheHome() {
getPaint().paint(this);
}
}
public abstract class Paint {
private Color color;
private String brand;
public Paint(Color color, String brand) {
this.color=color;
this.brand = brand;
}
public Color getColor() {
return color;
}
public String getBrand() {
return brand;
}
abstract void paint(Home home);
}
public class AsianPaint extends Paint{
public static final String brand = "Asian Paint";
public AsianPaint(Color color) {
super(color,brand);
}
@Override
void paint(Home home) {
System.out.println("Painting Started for "+home.getHomeType());
System.out.println("Brand of Color"+this.getBrand());
System.out.println("Name of Color"+this.getColor());
System.out.println("painting Ended");
}
}
public class ShalimarPaint extends Paint{
public static final String brand = "Shalimar Paint";
public ShalimarPaint(Color color) {
super(color,brand);
}
@Override
void paint(Home home) {
System.out.println("Painting Started for "+home.getHomeType());
System.out.println("Brand of Color"+this.getBrand());
System.out.println("Name of Color"+this.getColor());
System.out.println("painting Ended");
}
}
public enum Color {
RED("Red"),
GREEN("Green"),
BLUE("Blue"),
WHITE("White"),
YELLOW("Yellow");
String colorName;
Color(String string) {
this.colorName = string;
}
String getColor(){
return colorName;
}
}
public class client {
public static void main(String[] args) {
Home home = new Apartment(new AsianPaint(Color.RED));
home.paintTheHome();
home = new Flat(new ShalimarPaint(Color.GREEN));
home.paintTheHome();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论