Fried cold rice series: Design Mode decoration mode, fried cold rice Design Mode

Source: Internet
Author: User

Fried cold rice series: Design Mode decoration mode, fried cold rice Design Mode
Fried cold rice series: Decorative mode in Design Mode

Abstract: Original Source: http://www.cnblogs.com/Alandre/ sediment tile pulp carpenter hopes to reprint, retain abstract, thank you!

A good piano is an art, and a good article is also an art.

I. What is the abstract factory mode of the decoration mode?

Ii. Structural roles and responsibilities of the decoration Model

3. decoration mode implementation

1. What is the abstract factory model?

The Decorator mode is also called the packaging mode. Objects can be extended in a way that is transparent to the client. This is a replacement solution for inheritance relationships.

Ii. Structural roles and responsibilities of the decoration Model

The following are my opinions on this image. Component is an object interface or abstract class. The ConcreteComponent on the left is its implementation (function ). Decorator decoration, the so-called decorative abstract class is to implement the interface class, and then add the DoSomething module.

The following describes the implementation of each decoration. If you need to combine multiple functions, instead of combining them with each other, you can combine objects through parent class abstraction. For details, refer to the implementation diagram of the following case.

Abstract component role: an abstract interface, which is the parent interface of the decoration class and decoration class.

The specific component role is the implementation class of the abstract component.

Abstract decoration role: contains a reference to a component and defines an interface consistent with the abstract component.

Specific decorative role: the Implementation class of abstract decorative roles. Responsible for specific decoration.

3. decoration mode implementation

Local class diagram:

Specific implementation:

Car

public interface Car {        public void show();        public void run();}
RunCar
Public class RunCar implements Car {public void run () {System. out. println ("can run");} public void show () {this. run ();}}

CarDecorator

public abstract class CarDecorator implements Car{    private Car car;        public Car getCar() {        return car;    }    public void setCar(Car car) {        this.car = car;    }    public CarDecorator(Car car) {        this.car = car;    }        public abstract void show();}

 

The above is equivalent to setting up a good shelf, which needs to be implemented later.

FlyCarDecorator

Public class FlyCarDecorator extends CarDecorator {public FlyCarDecorator (Car car) {super (car);} public void show () {this. getCar (). show (); this. fly ();} public void fly () {System. out. println ("Flying");} public void run (){}}

SwimCarDecorator

Public class SwimCarDecorator extends CarDecorator {public SwimCarDecorator (Car car) {super (car) ;}public void show () {this. getCar (). show (); this. swim ();} public void swim () {System. out. println ("");} public void run (){}}

 

Then test the code

MainClass

public class MainClass {    public static void main(String[] args) {        Car car = new RunCar();                car.show();        System.out.println("---------");                Car swimcar = new SwimCarDecorator(car);        swimcar.show();        System.out.println("---------");                Car flySwimCar = new FlyCarDecorator(swimcar);        flySwimCar.show();    }}

Run the command to obtain the following results:

Can run --------- can run can swim can fly
4. Thanks to the knowledge sources and summary

I. What is the abstract factory mode of the decoration mode?

Ii. Structural roles and responsibilities of the decoration Model

3. decoration mode implementation

From: java Design Mode

If the above article or link is helpful to you, don't forget to click "like" in the article button or in the lower right corner of the page. You can also click the "share" floating button on the right side of the page to let more people read this article.

Calligraphy is the art of mind-catching

Humble calligraphy appreciation:


Decorator mode example in Design Mode

The Decorator dynamically adds actions to objects.
Here, DarkRoast, Mocha, and Whip all implement the Decorator of the Beverage interface.
Beverage beverage2 = new DarkRoast (); // beverage2 is DarkRoast
Beverage2 = new Mocha (beverage2); // adds Mocha behavior to beverage2
Beverage2 = new Mocha (beverage2); // adds Mocha behavior to beverage2
Beverage2 = new Whip (beverage2); // Add Whip to beverage2
Let's look at the specific logic.
It should be that the cost () method of each Decorator calls the cost () of Decoratee before calling the additional behavior.

Java 23 design patterns, which are commonly used

Factory mode, factory method mode, Singleton mode, Facade mode, Observer mode, and Bridge mode are commonly used, different projects have different design directions, and the design patterns that can be referenced are also different. There is no definite number, but the above models use more.

I looked for other models and listed them.

======================================
Common Java Design Patterns
Creation Mode

1. FACTORY-try to catch up with MM. The chicken wings of McDonald's and the chicken wings of KFC are what MM loves. Although the taste is different, whether you take MM to McDonald's or KFC, just say "four chicken wings" to the waiter. McDonald's and KFC are Factory for chicken wings production.

Factory model: the customer class and the factory class are separated. A consumer needs a product at any time and only requests from the factory. Consumers can accept new products without modification. The disadvantage is that the factory class must be modified when the product is modified. For example, how to create and provide data to clients.

2, BUILDER-MM is the most love to hear is "I love you" this sentence, to see different places of MM, to be able to use their dialect to say this sentence to her oh, I have a multi-language translation machine with a key for each language above. To see MM, I only need to press the corresponding key, it will be able to say "I love you" in the corresponding language, and foreign MM can also easily develop, This is my "I love you" builder. (This must be better than the US military's translation server in Iraq)

Construction mode: separates the internal appearance of a product from the product generation process, so that a building process generates product objects with different internal appearances. The construction mode allows the product's internal appearance to change independently, so that the customer does not have to know the details of the product's internal composition. The construction model enforces a step-by-step construction process.

3. factory method-ask MM to go to McDonald's for a hamburger. Different MM has different tastes. It is annoying to remember every one. I usually use the Factory Method mode, take the MM to the waiter and say "I want a hamburger". What kind of hamburger should I ask the MM to tell the waiter directly.

Factory method mode: the Core factory category is no longer responsible for the creation of all products. Instead, it submits the specific creation work to the subclass to become an abstract factory role, it is only responsible for providing the interface that must be implemented by a specific factory class, without touching the details of which product class should be instantiated.

4. PROTOTYPE-chat with MM via QQ, so I must talk about some affectionate words. I have collected a lot of emotional words, and I just need to copy the words and put them in QQ, this is my prototype. (100 yuan a copy, do you want)

Original Model mode: specify the type of the object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model mode allows you to dynamically add or remove product classes. The product class does not need to have any predefined level structure. The original model mode applies to any level structure. The disadvantage is that each class must have a clone method.

5. SINGLETON-I have 6 beautiful wives. My husband is me. I am Sigleton, our husband. All they need to say is "husband, that's me (I had a dream just now. How can this be a good thing)

Singleton mode: Singleton mode ensures that a class has only one instance, and instantiate the instance and provides the singleton mode to the entire system. The Singleton mode can only be used when there is a real "Single Instance" demand.

Structural Mode

6. ADAPTER-I met Sarah, a beautiful girl from Hong Kong at a friend's party. But I can't speak Cantonese, and she can't speak Mandarin. I have to turn to my friend kent for help, as an Adapter between me and Sarah, Sarah and I can talk to each other (I don't know if he will play with me)

Adapter (transformer) mode: converts an interface of a class into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together. The adaptation class can be returned according to the parameters... the remaining full text>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.