Photo by James Pond on Unsplash

Things to consider when designing objects

public interface Worker {
public void work();
}

public class ConstructionWorker implements Worker {
private List<Tool> tools;

public ConstructionWorker(final List<Tool> tools) {
this.tools = tools;
}

@Override
public void work() {
// use tools
}
}
public class ApiClient {
private String username;
private String password;

public ApiClient(final String username, final String password) {
this.username = username;
this.password = password;
}
}
class Credentials {
private String username;
private String password;
// constructor and getters}public class ApiClient {
private Credentials credentials;

public ApiClient(final Credentials credentials) {
this.credentials = credentials;
}
}
class Credentials {
private String username;
private String password;
// constructor and getters}public class ApiClient {
private Optional<Credentials> credentials;
private RestTemplate restTemplate;

public ApiClient(final Optional<Credentials> credentials,
final RestTemplate restTemplate) {
this.credentials = credentials;
this.restTemplate = restTemplate;
}
public String fetchData() {
// if credentials are present add to HttpHeader
// perform work
}
}
class Credentials {
private String username;
private String password;
// constructor and getters}public class ApiClient {
private Credentials credentials;
private RestTemplate restTemplate;

public ApiClient(final Credentials credentials,
final RestTemplate restTemplate) {
this.credentials = credentials;
this.restTemplate = restTemplate;
}
public String fetchData() {
// add credentials to HttpHeader
// perform work
}
}
class StaticClass {
public static void staticMethod() {}
}
public class MyClass {
// constructor and getters
public String doWork() {
StaticClass.staticMethod();
// do some other work
}
}
class StaticClass {
public static void staticMethod() {}
}
public class MyClass {
private StaticClass dependency;
// constructor and getters public String doWork() {
this.dependency.staticMethod();
// do some other work
}
}
public class Resource {
// constructor and getters
public String getDataById(final String id) {
// fetch data by id
return jsonEncode(data);
}
public String jsonEncode(final Object data) {
// compicated work
}
}
public interface Encoder {
public String encode(final Object data);
}
public class JsonEncoder implementes Encoder {
@Override
public String encode(final Object data) {
// encode as json
}
}

public class Resource {
private final Encoder encoder;

// constructor and getters
public String getDataById(final String id) {
// fetch data by id
return encoder.encode(data);
}
}
public class Service {
// constructor and getters
public List<Object> getData() {
final Calendar calendar = new GregorianCalendar(pdt);
return findDataInWeek(calendar.get(Calendar.WEEK_OF_YEAR))
}
public List<Object> findDataInWeek(int weekNum) {
}
}
public class Service {
private Calendar calendar;
// constructor and getters public List<Object> getData() {
return findDataInWeek(calendar.get(Calendar.WEEK_OF_YEAR))
}
public List<Object> findDataInWeek(int weekNum) {
}
}
public class Service {    private boolean shouldReportErrors = true;    // constructor and getters
// only setter for shouldReportErrors
public String doWork() {
// ...
if (shouldReportErrors) { <- changes behavior based on value
// report error(s)
}
// ...
}
}
public class Fighter {
private final int rank;
public Fighter(final int rank) {
if (rank < 0) {
throw new InvalidArgumentException('Fighter rank should be positive');
}
this.rank = rank;
}
}
A & B may produce different results if both have exposed the same operation for fetching common data through A1 & B1
public class FileDownloadService {
private final String protocol;
private final String host;
private final String port;
public FileDownloadService(final String protocol, final String host, final String port) {
if (!isProtocolValid(protocol)) {
throw new InvalidArgumentException('...');
}
if (!isHostValid(host)) {
throw new InvalidArgumentException('...');
}
if (!isPortValid(port)) {
throw new InvalidArgumentException('...');
}
this.protocol = protocol;
this.host = host;
this.port = port;
}
}
public class FileServer {
private final String protocol;
private final String host;
private final String port;
public FileServer(final String protocol, final String host, final String port) {
if (!isProtocolValid(protocol)) {
throw new InvalidArgumentException('...');
}
if (!isHostValid(host)) {
throw new InvalidArgumentException('...');
}
if (!isPortValid(port)) {
throw new InvalidArgumentException('...');
}
this.protocol = protocol;
this.host = host;
this.port = port;
}
}
public class FileDownloadService {
private final FileServer fileServer;
public FileDownloadService(final FileServer fileServer) {
this.fileServer = fileServer;
}
}
public class Product {
private final BigDecimal amount;
private final String currency;
// other properties
public Product(final BigDecimal amount, final String currency, // other properties) {

}
}
public class Money {
private final BigDecimal amount;
private final String currency;
}
public class Product {
private final Money money;
// other properties
public Product(final Money money, // other properties) {

}
}
public interface Encoder {
public String encode(final Object data);
}
public class JsonEncoder implementes Encoder {
@Override
public String encode(final Object data) {
// encode as json
}
}
public class Resource {
private final Encoder encoder;

// constructor and getters
public String encodeData(final Object data) {
// fetch data by id
return encoder.encode(data);
}
}
public interface Encoder {
public String encode(final Object data);
}
public class JsonEncoder implementes Encoder {
@Override
public String encode(final Object data) {
// encode as json
}
}
public class Resource {

// constructor and getters
public String encodeData(final Encoder encoder, final Object data) {
// fetch data by id
return encoder.encode(data);
}
}
public class LocalDate {
public static parse(final String date) { ... }
}
public class Fighter {
private final int rank;
private final Position position
public Fighter(final int rank, final Position position) {
if (rank < 0) {
throw new InvalidArgumentException('Fighter rank should be positive');
}
this.rank = rank;
this.position = position;
}
public void move(final Direction direction) {
this.position = this.position.move(direction);
}
public Position currentPosition() {
return this.position;
}
}
public class Money {
private final BigDecimal amount;
private final String currency;
// constructor and getters public Money withdraw(final BigDecimal amount) {
if (this.amount - amount < 0) {
// throw InvalidWithdrawalException
}

// process withdrawal
}
}
public class Money {
private final BigDecimal amount;
private final String currency;
}
public class Product {
public Money amount;
public boolean shouldApplyDiscount() { // ...
}
public Money fixedDiscountAmount() { // ...
}
}
amount = new Money();
if (product.shouldApplyDiscount()) {
amount = product.getAmount().substract(product.fixedDiscountAmount());
}
public class Money {
private final BigDecimal amount;
private final String currency;
}
public class Product {
public Money amount;
private boolean shouldApplyDiscount() { // ...
}
private Percentage discount() { // ...
}
public Money getNetAmount() {
if (shouldApplyDiscount()) {
return this.discount().applyTo(this.amount);
}
}
}
amount = product.getNetAmount();
public interface Encoder {
public List<String> encode(final Object data);
}
public class JsonEncoder implementes Encoder {
@Override
public List<String> encode(final Object data) {
// encode as json
}
}
public class MultipleTypeEncoder implementes Encoder {
private final List<Encoder> encoders;
// constructor and getters @Override
public List<String> encode(final Object data) {
final List<String> result = new ArrayList<>();
for(final Encoder encoder : encoders) {
result.add(encoder.encode(data));
}
return result;
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store