LogoLogo
v7.0.0
v7.0.0
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • What's Template Pattern
  • How to apply this method

Was this helpful?

  1. Documentation
  2. Guides
  3. App Architecture
  4. Why do Design Patterns help with the application's organization?

Template Pattern

What's Template Pattern

In this pattern, we will use an enum that will be responsible for providing a stylization for your controls, thus making your code less coupled and more practical to be developed

For example, in your application has a color pattern, font size and a particular font, you can create an enum that will represent this stylization for you and in case you need to change something in those controls you just need to change that enum.

How to apply this method

For example if in your application you have a need to create custom labels with a specific font and with different font sizes

Create a enum

You can create the enum in the root of the controller package

Structures
└── src
    └── main
        └── java
            └── com.your_company_name.your_name_app
                .
                .
                .
                └── controller
                    └── Template.java
Template.java
public enum Template {
        
/*

H1 to H4 will be used to stylize the controls with a font, bold style,
font size and forecolor and will be changed between those enum only your fontsize.
 
The parameters of the getFont () method are String font_name,
boolean boldStyle, int size in example h1 we have then:
        "Graviola Soft-Bold": font 
        false: boldStyle
        24: Font size
        0x363D86: forecolor
*/
    H1(Font.getFont("Graviola Soft-Bold", false, 24), 0x363D86),
    H2(Font.getFont("Graviola Soft-Bold", false, 20), 0x363D86),
    H3(Font.getFont("Graviola Soft-Bold", false, 18), 0x363D86),
    H4(Font.getFont("OpenSans-Bold", false, 12), 0x363D86),

    private final Font font;
    private final Integer forecolor;
    
    Template(Font font, Integer forecolor) {
        this.font = font;
        this.forecolor = forecolor;
    }

    public <T extends Control> T apply(T c) {
        if (forecolor != null) {
            c.setForeColor(forecolor);
        }
        
        if (font != null) {
            if (c instanceof Icon) {
                c.setFont(Font.getFont(c.getFont().name, false, font.size));
            } else {
                c.setFont(font);
            }
        }
        
        return c;
    }

To use the template

In your controls now you can use your previously created template to maintain a standard styling.

Label lblHeader= new Label("Header");
Template.H1.apply(lblHeader);
add(lblHeader, CENTER, TOP);

The enum template created above is just an example of how to use this pattern, you can customize it according to the needs of your application.

PreviousMVC Architecture PatternNextData Persistence: DAO Pattern.

Last updated 6 years ago

Was this helpful?