Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a structural pattern?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "what is a structural model". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the structural model"?

Bridging mode

The goal of the bridging pattern is to decouple the abstraction from the implementation so that the two can change independently. The bridging mode serves its purpose by using inheritance in public interfaces and implementations.

For example, the mobile phone can have multiple brands, then the mobile phone can be abstracted; each mobile phone can have a variety of implementations, such as different colors, different sizes, different performance and different systems, and so on.

Abstraction: abstract class

Implementation: an abstract implementation class

Refined: an extended abstract class

Specific Implementation: concrete implementation class.

Bridge mode UML class diagram

Bridging mode

Abstraction

Public abstract class AbstractionPhone {Implementor implementor; public Implementor getImplementorAppearance () {return implementor;} public void setImplementorAppearance (Implementor implementor) {this.implementor = implementor;} public abstract void operation ();}

Refined

Public class RefineAbstractionOnePlus extends AbstractionPhone {@ Override public void operation () {System.out.println ("one plus phone"); implementor.operation ();}} public class RefinedAbstractionPhoneApple extends AbstractionPhone {@ Override public void operation () {System.out.println (iPhone); implementor.operation ();}}

Implementor

Public abstract class Implementor {public abstract void operation ();}

Concrete Implementor

Public class ConcreteImplementorColor extends Implementor {@ Override public void operation () {System.out.println ("implementation of Mobile phone related Color");}} public class ConcreteImplementorSize extends Implementor {@ Override public void operation () {System.out.println ("implementation of Mobile phone size");}}

Client

Public class Client {public static void main (String [] args) {AbstractionPhone phone = new RefineAbstractionOnePlus (); phone.setImplementorAppearance (new ConcreteImplementorColor ()); phone.operation (); phone.setImplementorAppearance (new ConcreteImplementorSize ()); phone.operation ();}} combination mode

As the name implies, the composition pattern is to combine a group of objects into a complex single whole, such as combining objects into a tree or graphic structure.

The simplest and most common is the distribution of people within the company, all the employees are very complex, but from the CEO to the lowest level of employees will form a tree structure.

Combination mode

Component

Public abstract class ComponentEmployee {String name; / / name String position; / / position String salary; / / salary / / report personnel situation public void report () {String str = "Employee {" >

Composite

Public class CompositeLeader extends ComponentEmployee {/ / this is supposed to be private, and then it is appropriate to obtain it externally through get, in order to demonstrate List subComponentEmployees; public CompositeLeader (String name, String position, String salary) {super (name, position, salary); / / new a List collection of employees at the next level subComponentEmployees = new ArrayList ();} @ Override public void addEmployee (ComponentEmployee componentEmployee) {subComponentEmployees.add (componentEmployee) } @ Override public void deleteEmployee (ComponentEmployee componentEmployee) {subComponentEmployees.remove (componentEmployee);} @ Override public void report () {System.out.println ("my situation:"); super.report (); System.out.println ("the situation of my employees"); for (ComponentEmployee e: subComponentEmployees) {e.report ();}

Leaf

Public class LeafStaff extends ComponentEmployee {public LeafStaff (String name, String position, String salary) {super (name, position, salary);} / / No staff is available, so add and delete operations are empty. Note the empty implementation @ Override public void addEmployee (ComponentEmployee componentEmployee) {} @ Override public void deleteEmployee (ComponentEmployee componentEmployee) {} @ Override public void report () {super.report ();}}.

It is not safe to notice that there are two empty implementations in the leaf node (because there are no other employees at the bottom of the staff). Quite simply, just delete the two abstract methods in Component, and then add new methods in Composite instead of overriding them.

Appearance mode (facade mode)

The purpose of facade mode is to provide a single unified interface for complex subsystems, so that clients only need to know the results, not how each subsystem works.

For example, although Party A puts forward the demand to the product manager, he only wants the finished product. As for how the internal deployment of the company is completed, there is no need for the customer to know.

Facade: subsystem interface

SubSystem: classes defined in the subsystem

Facade mode UML class diagram

Facade mode

Facade

Public class FacadeLeader {private SubSystemArt subSystemArt = new SubSystemArt (); private SubSystemDevelopment subSystemDevelopment = new SubSystemDevelopment (); private SubSystemOperations subSystemOperations = new SubSystemOperations (); / / exposed to the outside world, the outside world does not know who is doing it inside and doing public void needArt () {subSystemArt.art ();} public void needDevelop () {subSystemDevelopment.develop ();} public void needOperation () {subSystemOperations.operate () }}

SubSystem

Public class SubSystemArt {public void art () {System.out.println ("the art department is drawing");} public class SubSystemDevelopment {public void develop () {System.out.println ("the development department is developing");}} public class SubSystemOperations {public void operate () {System.out.println ("the operation and maintenance department is testing!") ;}}

Client

Public class Client {public static void main (String [] args) {FacadeLeader facadeLeader = new FacadeLeader (); System.out.println ("We need this requirement"); facadeLeader.needArt (); facadeLeader.needDevelop (); facadeLeader.needOperation ();}} share meta mode

Sharing meta-mode sounds high-end, but it is actually a mode of sharing objects. The goal is to reduce memory footprint by sharing state between similar objects.

For example, if the soldiers in the King attack, double resist and move at a certain speed within a certain period of time, then the shared meta-mode is used to reduce the generation of objects, resulting in less memory consumption. For those who have finished playing master, it can be used as a non-shared state because it is not repetitive.

Flyweight: abstract share metaclass

Concrete Flyweight: a shared meta-object that shares status with its peers

Unshared Concrete Flyweight: a shared meta-object whose state is not shared

Flyweight Factory: share meta-factory class

Enjoy meta-schema UML class diagram

Sharing meta-mode

Flyweight

Public abstract class FlyweightSoldier {public abstract void play ();}

Concrete Flyweight

Public class ConcreteFlyweightNormalSoldier extends FlyweightSoldier {String type; public ConcreteFlyweightNormalSoldier (String type) {this.type = type;} @ Override public void play () {System.out.println ("generate Bing:" + type);}}

Unshared Concrete Flyweight

Public class UnsharedConcreteFlyweightGeneral extends FlyweightSoldier {@ Override public void play () {System.out.println ("dragons generated according to different circumstances");}}

Flyweight Factory

Public class FlyweightFactory {private Hashtable flyweights = new Hashtable (); public ConcreteFlyweightNormalSoldier getSoldier (String key) {if (! flyweights.contains (key)) {flyweights.put (key, new ConcreteFlyweightNormalSoldier (key));} return (ConcreteFlyweightNormalSoldier) flyweights.get (key);} public UnsharedConcreteFlyweightGeneral getGeneral (String key) {flyweights.put (key, new UnsharedConcreteFlyweightGeneral ()); return (UnsharedConcreteFlyweightGeneral) flyweights.get (key);}}

Note here that because ordinary soldiers are shared, there is no need to new a soldier and return directly when there is key. For special soldiers, regardless of whether they have key or not, they will new a soldier put to enter.

Client

Public class Client {public static void main (String [] args) {FlyweightFactory factory = new FlyweightFactory (); for (int I = 0; I < 3; iBing +) {System.out.println ("No." + I + 1 + "Bo Bing Line"); factory.getSoldier ("close combat"). Play (); factory.getSoldier ("Archer"). Play () Factory.getSoldier ("mage soldiers"). Play ();} / / only three objects for (int I = 0; I < 2; iBing +) {System.out.println ("1st" + I + "wave special line"); factory.getGeneral ("minus double anti-dragons"). Play () Factory.getGeneral ("reduced attack Speed Dragon"). Play ();} / created 7 objects here, 3 + 4}} so far, I believe you have a deeper understanding of "what is a structural pattern". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report