Difference between revisions of "Naming convention"

From DarkWiki
Jump to: navigation, search
(Getters and setters)
(Other)
 
(23 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
 
* Getters (get, is)
 
* Getters (get, is)
 +
** Takes no parameters
 
** Does not alter the object in any way; it is constant
 
** Does not alter the object in any way; it is constant
 
** Can be called multiple times, and in any order
 
** Can be called multiple times, and in any order
** Returns a consistent result
+
** Returns a consistent single result
 +
 
 +
<syntaxhighlight lang="java">
 +
public String getName() {
 +
  return this.name;
 +
}
 +
 
 +
public boolean isAdult() {
 +
  return true;
 +
}
 +
 
 +
</syntaxhighlight>
 +
 
 +
* Setters (set)
 +
** Takes one parameter
 +
** May or may not alter the object
 +
** Only affects the object that has the setter; it does not persist anywhere else
 +
** Can be called multiple times, and in any order
 +
 
 +
<syntaxhighlight lang="java">
 +
public void setName(String name) {
 +
  this.name = name;
 +
}
 +
</syntaxhighlight>
 +
 
 +
===Finders & filters===
 +
 
 +
* Finders (find)
 +
** Takes zero or more arguments (which act as filter or sorting controls)
 +
** Does not alter the object in any way; it is constant
 +
** Can be called multiple times, and in any order
 +
** Returns a consistent, single, non-null collection result (or an empty Optional) - ''never'' null
 +
 
 +
<syntaxhighlight lang="java">
 +
public List<String> findNames() {
 +
  return Collections.emptyList();
 +
}
 +
 
 +
public Optional<Person> findById(String personId) {
 +
  return Optional.empty();
 +
}
 +
</syntaxhighlight>
 +
 
 +
===Loaders===
 +
 
 +
* Loaders (load, loadBy, getBy)
 +
** Takes zero or more parameters
 +
** Does not alter the object in any way; it is constant
 +
** Can be called multiple times, and in any order
 +
** Returns a consistent single result or throws an exception indicating it could not be found/loaded
 +
 
 +
<syntaxhighlight lang="java">
 +
public TestModel loadById(String id) {
 +
  return this.testModelRepository.findById(id).orElseThrow(()->new NotFoundExcpetion());
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
===Factories===
 +
 
 +
* Factories
 +
** Responsible for creating objects - not persisting them!
 +
** Are stateless
 +
 
 +
* Factory classes
 +
** Conform to [ObjectType]Factory naming convention
 +
 
 +
* Factory methods
 +
** Conform to create[ObjectType] naming convention
 +
 
 +
<syntaxhighlight lang="java">
 +
public Person createPerson(String name) {
 +
  Person person = new Person();
 +
  person.setName(name);
 +
  return person;
 +
}
 +
</syntaxhighlight>
 +
 
 +
===Other===
 +
 
 +
* Populator methods (imbue, populate, hydrate)
 +
** Only modify the object(s) passed as arguments
 +
** Does not affect the object's state
 +
 
 +
<syntaxhighlight lang="java">
 +
public Person imbueDefaults(Person person) {
 +
  if(person.getName()==null) {
 +
    person.setName(person.getFirstName() + " " + person.getLastName() );
 +
  }
 +
  return person;
 +
}
 +
</syntaxhighlight>
 +
 
 +
===Persistance===
 +
 
 +
* Repositories
 +
** Conform to [ObjectType]Repository naming convention
 +
** Methods such as save*, load*, find*, read*, write*, delete*
 +
 
 +
===Adapter===
 +
 
 +
An ''Adapter'' is a class that takes one or more objects and implements an interface. i.e. it adapts an X so it looks like a Y.
 +
 
 +
<syntaxhighlight lang="java">
 +
public class EmployedPersonAdapter implements IEmployedPerson {
 +
  public EmployedPersonAdapter(Person person) {
 +
    ...
 +
  }
 +
  ...
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
===Decoarator===
 +
 
 +
A ''Decoarator'' is a class that takes one object (the delegate), implements the same interface, and each method calls the delegate. i.e. it allows someone to override the behaviour of method on an instance.
 +
 
 +
<syntaxhighlight lang="java">
 +
public class AbstractPersonDecorator implements IPerson {
 +
  private IPerson delegate;
 +
  public AbstractPersonDecorator(IPerson person) {
 +
    this.delegate = person;
 +
  }
 +
 
 +
  public String getName() {
 +
    return this.delegate.getName();
 +
  }
 +
 
 +
  public int getAge() {
 +
    return this.delegate.getAge();
 +
  }
 +
}
 +
</syntaxhighlight>
 +
 
 +
<syntaxhighlight lang="java">
 +
public class AgedCappedPersonDecorator extends AbstractPersonDecorator {
 +
  private int maxAge;
 +
 
 +
  public AgedCappedPersonDecorator(IPerson person, int maxAge) {
 +
    super(person);
 +
    this.maxAge = maxAge;
 +
  }
 +
 
 +
  public int getAge() {
 +
    return Math.min(ageMax,this.delegate.getAge());
 +
  }
 +
}
 +
</syntaxhighlight>

Latest revision as of 08:18, 23 July 2021

Java naming convention

Getters and setters

  • Getters (get, is)
    • Takes no parameters
    • Does not alter the object in any way; it is constant
    • Can be called multiple times, and in any order
    • Returns a consistent single result
public String getName() {
  return this.name;
}

public boolean isAdult() {
  return true;
}
  • Setters (set)
    • Takes one parameter
    • May or may not alter the object
    • Only affects the object that has the setter; it does not persist anywhere else
    • Can be called multiple times, and in any order
public void setName(String name) {
  this.name = name;
}

Finders & filters

  • Finders (find)
    • Takes zero or more arguments (which act as filter or sorting controls)
    • Does not alter the object in any way; it is constant
    • Can be called multiple times, and in any order
    • Returns a consistent, single, non-null collection result (or an empty Optional) - never null
public List<String> findNames() {
  return Collections.emptyList();
}

public Optional<Person> findById(String personId) {
  return Optional.empty();
}

Loaders

  • Loaders (load, loadBy, getBy)
    • Takes zero or more parameters
    • Does not alter the object in any way; it is constant
    • Can be called multiple times, and in any order
    • Returns a consistent single result or throws an exception indicating it could not be found/loaded
public TestModel loadById(String id) {
  return this.testModelRepository.findById(id).orElseThrow(()->new NotFoundExcpetion());
}


Factories

  • Factories
    • Responsible for creating objects - not persisting them!
    • Are stateless
  • Factory classes
    • Conform to [ObjectType]Factory naming convention
  • Factory methods
    • Conform to create[ObjectType] naming convention
public Person createPerson(String name) {
  Person person = new Person();
  person.setName(name);
  return person;
}

Other

  • Populator methods (imbue, populate, hydrate)
    • Only modify the object(s) passed as arguments
    • Does not affect the object's state
public Person imbueDefaults(Person person) {
  if(person.getName()==null) {
    person.setName(person.getFirstName() + " " + person.getLastName() );
  }
  return person;
}

Persistance

  • Repositories
    • Conform to [ObjectType]Repository naming convention
    • Methods such as save*, load*, find*, read*, write*, delete*

Adapter

An Adapter is a class that takes one or more objects and implements an interface. i.e. it adapts an X so it looks like a Y.

public class EmployedPersonAdapter implements IEmployedPerson {
  public EmployedPersonAdapter(Person person) {
    ...
  }
  ...
}


Decoarator

A Decoarator is a class that takes one object (the delegate), implements the same interface, and each method calls the delegate. i.e. it allows someone to override the behaviour of method on an instance.

public class AbstractPersonDecorator implements IPerson {
  private IPerson delegate;
  public AbstractPersonDecorator(IPerson person) {
    this.delegate = person;
  }

  public String getName() {
    return this.delegate.getName();
  }

  public int getAge() {
    return this.delegate.getAge();
  }
}
public class AgedCappedPersonDecorator extends AbstractPersonDecorator {
  private int maxAge;

  public AgedCappedPersonDecorator(IPerson person, int maxAge) {
    super(person);
    this.maxAge = maxAge;
  }

  public int getAge() {
    return Math.min(ageMax,this.delegate.getAge());
  }
}