Fried cold rice Series: decorative mode in design mode

Source: Internet
Author: User
Fried cold rice Series: design mode decorative model abstract: Original source: http: www. cnblogs. comAlandre sediment tile pulp Carpenter hope to reprint, keep abstract, thank you! A good piano is an art, and a good article is also an art. I. What is the decorative model Abstract Factory Model II. the structural roles and responsibilities of the decorative Model III. the implementation of the decorative Model I. What is the abstract factory model decoration (Decorator) model and a series of fried cold meals: design mode decoration 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:

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.