DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Building a Rust Command Line Interface to Chat With Llama 3.2
  • A Beginner’s Guide to GraphQL Interfaces and Unions
  • Contexts in Go: A Comprehensive Guide
  • The Power of Refactoring: Extracting Interfaces for Flexible Code

Trending

  • How to Convert XLS to XLSX in Java
  • Monolith: The Good, The Bad and The Ugly
  • Agentic AI for Automated Application Security and Vulnerability Management
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 1

Programming to an Interface

Make sure to account for the variable parts of your applications.

By 
Shamik Mitra user avatar
Shamik Mitra
·
Sep. 03, 16 · Tutorial
Likes (29)
Comment
Save
Tweet
Share
55.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will  discuss design principles and tools based on which design patterns are created. 

The very first principle is “Programming to an Interface”. What does that mean? Let's try to understand this principle in detail.

In general, if you look at any business solution for a problem statement, you can find two parts:

  • Fixed
  • Variable

The fixed part is some kind of boilerplate or constant declaration code, but when we use this design pattern we actually take care of the variable part.

All design patterns are discovered to maintain variable parts in an efficient way, so that the application welcomes changes without breaking. If your code is not flexible with future enhancements or changes, then your code is not up to the mark.

The question is, in Java, how judiciously can you maintain your variable parts?

To answer that, we can say variable parts are different ways that an outcome can be achieved, and the system must be ready to accept them all. More specifically, we know the type of the operation (say, "display functionality", but it can have many implementations (maybe it is a projector or monitor). Most importantly, requirements in software development are to meet clients' ever-changing needs. So I can say functionality is constant, but its implementation can change anytime based on the client, so always design based on a contract rather than an implementation. 

Let’s take an example. A computer monitor is  designed for display purposes. So I can say the computer is a product and the computer monitor is a part or module of the computer which is responsible for display operation.

Now, later on, client needs change and now they want to display on a projector.

So if our solution is not capable of welcoming this need, it will be nothing but a waste of a product.

According to new needs, what we can analyze is it will perform the same operation, display, but the module should change from computer monitor to projector.

The display module of a computer should be flexible, so we can change it easily, or we can change it dynamically (at runtime). We can say the display module is like a strategy, and a client can change the strategy at any time. Technically, the display is a function or contract, but it can have multiple implementations, so it's better to create this contract as an interface, and every implementation will provide its own display mechanism so at runtime, the Computer can call it.

So our Java solution looks like the following:

interface displayModule
{
public void display();
}


public class Monitor implements displayModule
{
  public void display()
  {
 	 System.out.println(“Display through Monitor”);
  }
}

public class Projector implements displayModule
{
  public void display()
  {
 	 System.out.println(“Display through projector”);
  }
}



public class Computer
{
 displayModule dm;// programming through interface as variable part

public void setDisplayModule(displayModule dm)
{
this.dm=dm;
}

public void display()
{
dm.display();
}

public static void main(String args[])
{
Computer cm =new Computer();
displayModule dm = new Monitor();
displayModule dm1 = new Projector();
cm. setDisplayModule(dm);
cm. display();
cm. setDisplayModule(dm1);
cm. display();

}

}

Output:
  Display through Monitor
  Display through projector.  

Look the implemented solution: we know Display Module should be flexible and we know the problem statement is display Operation, but in the future, the client may change the display operation style. One thing is for sure: computer should have a display module, but we don’t know what that piece of equipment will be.

So we create an interface called displayModule, and all display equipment must implement that interface and provide its own implementation of the display operation.

In the computer class here I create a has-A relation (composition) called displayModule so that the display module can change frequently as per the client's needs, so we always make this strategy as abstract or high-level as possible so we can change it on the fly with actual implementation.

Remember to always code through an interface so you can change your strategy at runtime with actual implementation. Figure out the variable parts of your problem statement and make it abstract so we can change strategy further.  The interface in "programming through the interface" means the java interface or abstract class.

Interface (computing)

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Rust Command Line Interface to Chat With Llama 3.2
  • A Beginner’s Guide to GraphQL Interfaces and Unions
  • Contexts in Go: A Comprehensive Guide
  • The Power of Refactoring: Extracting Interfaces for Flexible Code

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!