用于商城系统中商品的分类 1.可以用多层继承结构实现,如: 缺点:扩展性不好,不推荐使用 违反单一职责原则 2.使用桥接模式:扩展性强,可以修改任意一个维度
/** * 品牌 * @author Administrator * */ public interface Brand { void sale(); } class Lenovo implements Brand{ @Override public void sale() { // TODO 自动生成的方法存根 System.out.println("销售联想电脑"); } } class Dell implements Brand{ @Override public void sale() { // TODO 自动生成的方法存根 System.out.println("销售Dell电脑"); } } /** * 类型 * @author Administrator */ public class Computer { protected Brand brand; public Computer(Brand brand) { this.brand = brand; } public void sale() { brand.sale(); } } class DeskTop extends Computer{ public DeskTop(Brand brand) { super(brand); } @Override public void sale() { // TODO 自动生成的方法存根 System.out.println("销售台机"); super.sale(); } } class LapTop extends Computer{ public LapTop(Brand brand) { super(brand); } @Override public void sale() { // TODO 自动生成的方法存根 super.sale(); System.out.println("销售笔记本"); } } public class Client { public static void main(String[] args) { //销售联想的台式机 Computer c=new DeskTop(new Lenovo()); c.sale(); //销售联想的笔记本 Computer c2=new LapTop(new Lenovo()); c2.sale(); } }