Structural model of design pattern

Structural model of design pattern

Structural model of design pattern

Bridge mode
Do not use class inheritance as much as possible, and use composition/aggregation as much as possible.
Description: The high dependency of inheritance method subclass and parent class limits the reuse and program flexibility.

Choose different interface implementationsChoose different business types

import org.junit.Test; public class bridge mode { @Test public void test () { ColocalateFactory a = new BlackFactory(); a.ColocalateMarking(); ColocalateFactory b = new WhilteFactory(); b.ColocalateMarking(); } } interface ColocalateFactory { void ColocalateMarking () ; } class BlackFactory implements ColocalateFactory { @Override public void ColocalateMarking () { //TODO Auto-generated method stub System.out.println( "Producing black chocolate" ); } } class WhilteFactory implements ColocalateFactory { @Override public void ColocalateMarking () { //TODO Auto-generated method stub System.out.println( "Producing white chocolate" ); } } Copy code

Adapter mode
Description: The maintenance personnel have the same or similar processing functions, but the methods are different. Make a unified call to the interface.

  • When injecting classes that need to be adapted, use the set method instead of new to instantiate the object to prevent solidification.
  • The method of the adapted class and the method of the class to be adapted are not in a parallel relationship but in a calling relationship.
public interface Charger { void GetPower () ; } //Charger A public class ChargerA implements Charger { @Override public void GetPower () { //TODO Auto-generated method stub System.out.println( "Use 220V for charging" ); } } //Charger B public class ChargerB implements Charger { @Override public void GetPower () { //TODO Auto-generated method stub System.out.println( "Use 200V for charging" ); } } //Universal Charger import org.junit.Test; public class MultCharger implements Charger { private Charger charger; public void setCharger (Charger charger) { this .charger = charger; } @Override public void GetPower () { //TODO Auto-generated method stub this .charger.GetPower(); } @Test public void test () { Charger a = new ChargerA(); Charger b = new ChargerB(); MultCharger Multcharger = new MultCharger(); Multcharger.setCharger(a); Multcharger.GetPower(); Multcharger.setCharger(b); Multcharger.GetPower(); } } Copy code

Decorator mode
Description: Dynamically expand the function of an object without changing the original class file and using inheritance. (This determines that the class method cannot be added directly)

  • The adapter model adaptation object and the adapter object may not have the relationship of realizing a unified interface (ie, sibling class). The purpose is: compatible
  • Decorator pattern: It is an alternative to inheritance, which is relatively more flexible. The purpose is: to enhance
import org.junit.Test; public class decorator pattern { @Test public void test () { Soldier s = new SmallSoldier(); BigSoldier b = new BigSoldier(); b.setSoldier(s); b.fighting(); } } interface Soldier { void fighting () ; } class SmallSoldier implements Soldier { @Override public void fighting () { //TODO Auto-generated method stub System.out.println( "Ordinary soldiers are fighting" ); } } class BigSoldier implements Soldier { private Soldier soldier; public void setSoldier (Soldier soldier) { this .soldier = soldier; } @Override public void fighting () { //TODO Auto-generated method stub soldier.fighting(); System.out.println( "Remote Soldiers are configured to assist" ); } } Copy code

Combination mode
Using recursive thinking
Description: Combine objects into a tree structure to represent the "part-whole" hierarchical structure.
Supplement: It also provides three methods for adding child nodes, removing child nodes, and displaying the structure.
Application scenario: where a tree structure appears, such as file directory display, multi-level directory display and other tree structure data operations.
Method: Realize by inheriting abstract class

public abstract class Node { protected String name; //Name //The constructor assigns the name public Node (String name) { this .name = name; } //New node: file node does not have this method, directory node rewrites this method public void addNode (Node node) throws Exception { throw new Exception( "Invalid exception" ); } //Remove the node public void removeNode (Node node) throws Exception { throw new Exception( "Invalid exception" ); } //Display node: Both files and directories implement this method abstract void display () ; } //File node public class Filer extends Node { public Filer (String name) { super (name); //TODO Auto-generated constructor stub } @Override void display () { //TODO Auto-generated method stub System.out.println(name); } } //Directory node public class Noder extends Node { //Internal node list (including files and sub- directories) List<Node> nodeList = new ArrayList<Node>(); public Noder (String name) { super (name); //TODO Auto-generated constructor stub } //New node public void addNode (Node node) throws Exception { nodeList.add(node); } //Remove node public void removeNode (Node node) throws Exception { nodeList.remove(node); } //Recursively print the name downward @Override void display () { System.out.println(name); for (Node node:nodeList){ node.display(); } } } //Test class import java.io.File; public class Clienter { public static void createTree (Node node) throws Exception { File file = new File(node.name); File[] f = file.listFiles(); for (File fi: f){ //Judging whether the node is a file if (fi.isFile()){ //Use the absolute path to name the node Filer filer = new Filer(fi.getAbsolutePath()); node.addNode(filer); } //Judging whether the node is a directory if (fi.isDirectory()){ //Use the absolute path to name the node Noder noder = new Noder(fi.getAbsolutePath()); node.addNode(noder); createTree(noder); //Use recursive tree structure } } } public static void main (String[] args) { Node noder = new Noder( "G://WeGame" ); try { createTree(noder); } catch (Exception e) { e.printStackTrace(); } noder.display(); } } Copy code

Flyweight mode
Description: Use a class to store shared resources, and run sharing technology to effectively support the reuse of a large number of fine-grained objects.
Method:
Provide methods for adding put and get through Map collection storage (HashMap)

public class Student { private String name; private int age; public Student (String name, int age) { this .name=name; this .age=age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge ( int age) { this .age = age; } @Override public String toString () { return "Student [name=" + name + ", age=" + age + "]" ; } } import java.util.HashMap; import org.junit.Test; public class StudentFactory { static HashMap <String,Student>map = new HashMap<>(); public static Student getStudent (String name) { if (map.get(name)!= null ) { return map.get(name); } return null ; } public static void addStudent (Student s) { map.put(s.getName(), s); } public static void display () { System.out.println(map.entrySet()); } @Test public void test001 () { StudentFactory.addStudent( new Student( "luo" , 18 )); StudentFactory.addStudent( new Student( "li" , 17 )); Student s = StudentFactory.getStudent( "luo" ); System.out.println(s); StudentFactory.display(); } } Copy code

The appearance model
does not change the content of the old class, and reorganizes the business through an associated method of the appearance class.
It is generally used between the subsystem and the visit to shield complex subsystem calls from the visit. The simple calling method provided by the refreshing appearance class is adopted, and the specific implementation is called by the appearance class to the subsystem. (Business logic integration)

import org.junit.Test; public class appearance mode { private FactoryA a = new FactoryA(); private FactoryB b = new FactoryB(); public void lookplay () { a.funcA(); b.funcB(); } @Test public void test () { new appearance mode().lookplay(); } } class FactoryA { public void funcA () { System.out.println( " Confess to others" ); } } class FactoryB { public void funcB () { System.out.println( "Agree to others' confession" ); } } Copy code

Proxy mode
Description: Provide a proxy for the third-party object to control the access of the object (third-party circumvention behavior)
Note: The adapter is one-to-many, and the proxy is a one-to-one relationship.

public interface FactoryInterface { void Breakmaking () ; } public class Factory implements FactoryInterface { @Override public void Breakmaking () { //TODO Auto-generated method stub System.out.println( "Production Bread" ); } } import org.junit.Test; public class AgentFactory implements FactoryInterface { FactoryInterface f = new Factory(); @Override public void Breakmaking () { //TODO Auto-generated method stub f.Breakmaking(); } @Test public void test () { new AgentFactory().Breakmaking(); } } Copy code