1. Preface
Classes have two main levels, one is the functional level and the other is the implementation level.
Functional hierarchy, generally applied to current classes that do not meet diverse business needs, allows subclasses to inherit (specifically) the parent class, and adds features that are not in the parent class (typically new methods), which is a hierarchy resulting from the addition of new features.
Implementation hierarchy, commonly seen in subclasses to inherit abstract classes or interfaces, overrides abstract methods in abstract classes or interfaces, abstract classes (interfaces) declare methods only, and the sharing of specific tasks requires subclasses to share.Equivalent to subclasses, they just push the slogan propagated by the parent and the bull into execution, and assign the assigned tasks to real work, but it does not add new functions, it just implements the abstract method of the parent, which is the level of implementation of the class.
If these two levels are implemented in the same class, they will be mixed together, interact with each other, have a high degree of coupling, and are not conducive to later expansion.Bridging mode separates the two levels and implements them in two classes. Modification of one level will not affect the implementation of the other.
2. Code implementation
1). Define a common POJO entity class Product first
import java.math.BigDecimal; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; @Setter @Getter @ToString @NoArgsConstructor public class Product { private Integer id;//product id private String name;// Product Name private String description;// describe private BigDecimal price;// Price public Product(String name) { super(); this.name = name; } public Product(BigDecimal price) { super(); this.price = price; } public Product(Integer id, String name, String description) { super(); this.id = id; this.name = name; this.description = description; } public Product(Integer id, String name, String description, BigDecimal price) { super(); this.id = id; this.name = name; this.description = description; this.price = price; } }
2). Define a Dao layer interface that operates on the entity class Product.This interface has a basic abstract method of add-delete check.
import java.util.List; public interface ProductDao { int addProduct(Product product); int deleteProduct(int productId); int modifyProduct(Product product); List<Product> findProducts(Product product); }
3). Add Realization
As customer demand for databases changes, you may not only use one database at the end of the software life cycle, you may want to write ProductDao implementation classes for data persistence operations on different database platforms
/** *Implementation in Oracle Database * @author Administrator * */ public class ProductDaoOrancleImpl implements ProductDao { @Override public int addProduct(Product product) { System.out.println("Save a product in Orancle database" +product); return 1; } @Override public int deleteProduct(int productId) { System.out.println("Deleted a product in Orancle database with id"+productId); return 1; } @Override public int modifyProduct(Product product) { System.out.println("A product was modified in the Orancle database and then"+product"); return 1; } @Override public List<Product> findProducts(Product product) { Product p1=new Product(1,'washing machine','cheap washing machine'); Product P2 = new Product (2,'T-shirt','breathable T-shirt'); System.out.println("Products queried from Orancle database are: {"+p1+"},{"+p2+"}"); return Arrays.asList(p1,p2); } }
import java.util.Arrays; import java.util.List; /** *Implementation in mysql database * @author Administrator * */ public class ProductDaoMysqlImpl implements ProductDao{ @Override public int addProduct(Product product) { System.out.println("Save a product in mysql database" +product); return 1; } @Override public int deleteProduct(int productId) { System.out.println("Deleted a product in mysql database with id"+productId); return 1; } @Override public int modifyProduct(Product product) { System.out.println("A product was modified in the mysql database and then"+product"); return 1; } @Override public List<Product> findProducts(Product product) { Product p1=new Product(1,'washing machine','cheap washing machine'); Product P2 = new Product (2,'T-shirt','breathable T-shirt'); System.out.println("Products queried in mysql database are: {"+p1+"},{"+p2+"}"); return Arrays.asList(p1,p2); } }
4. Write a concrete class that implements bridging and abstraction
The key to this class is the delegation mechanism, which delegates the member variable productDao to complete the actual business processing.Of course, productDao must be an instance object of the implementation class of the ProductDao interface, because the interface cannot be instantiated.This member variable, productDao, is the key to bridging, linking the methods the interface is implemented to methods that may appear to have new functionality in a subclass of ProductDaoBridge.
import java.util.List; public class ProductDaoBridge { //Decorated with final, cannot reference other ProductDao objects after initialization protected final ProductDao productDao; public ProductDaoBridge(ProductDao productDao) { super(); this.productDao = productDao; } public int addProduct(Product product) { return productDao.addProduct(product); } int deleteProduct(int productId) { return productDao.deleteProduct(productId); } int modifyProduct(Product product) { return productDao.modifyProduct(product); } List<Product> findProducts(Product product){ return productDao.findProducts(product); } }
5). Additional features
In business processing, data is frequently checked before deletion. A new method can be added to the subclass to process the deletion operation together.
import java.util.List; public class MultiProductDaoBridge extends ProductDaoBridge { public MultiProductDaoBridge(ProductDao productDao) { super(productDao); } /** * Delete all queries after they are found * * @param product query criteria */ //final modification prevents being overridden by subclasses public final int[] findProductThenDelete(Product product) { List<Product> products = this.productDao.findProducts(product); int[] delOks = new int[products.size()]; for (int i = 0; i < products.size(); i++) { delOks[i] = productDao.deleteProduct(products.get(i).getId()); } return delOks; } }