Interface Segregation Principle

Below is taken directly from http://www.oodesign.com/interface-segregation-principle.html

Motivation

When we design an application we should take care how we are going to make abstract a module which contains several submodules. Considering the module implemented by a class, we can have an abstraction of the system done in an interface. But if we want to extend our application adding another module that contains only some of the submodules of the original system, we are forced to implement the full interface and to write some dummy methods. Such an interface is named fat interface or polluted interface. Having an interface pollution is not a good solution and might induce inappropriate behavior in the system.

The Interface Segregation Principle states that clients should not be forced to implement interfaces they don’t use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule.

Intent

Clients should not be forced to depend upon interfaces that they don’t use.

Example

Below is an example which violates the Interface Segregation Principle. We have a Manager class which represent the person which manages the workers. And we have 2 types of workers some average and some very efficient workers. Both types of workers works and they need a daily launch break to eat. But now some robots came in the company they work as well , but they don’t eat so they don’t need a launch break. One on side the new Robot class need to implement the IWorker interface because robots works. On the other side, the don’t have to implement it because they don’t eat.

This is why in this case the IWorker is considered a polluted interface.

If we keep the present design, the new Robot class is forced to implement the eat method. We can write a dummy class which does nothing(let’s say a launch break of 1 second daily), and can have undesired effects in the application(For example the reports seen by managers will report more lunches taken than the number of people).

According to the Interface Segregation Principle, a flexible design will not have polluted interfaces. In our case the IWorker interface should be split in 2 different interfaces.

// interface segregation principle - bad example
interface IWorker {
	public void work();
	public void eat();
}

class Worker implements IWorker{
	public void work() {
		// ....working
	}
	public void eat() {
		// ...... eating in launch break
	}
}

class SuperWorker implements IWorker{
	public void work() {
		//.... working much more
	}

	public void eat() {
		//.... eating in launch break
	}
}

class Manager {
	IWorker worker;

	public void setWorker(IWorker w) {
		worker=w;
	}

	public void manage() {
		worker.work();
	}
}

Following it’s the code supporting the Interface Segregation Principle. By splitting the IWorker interface in 2 different interfaces the new Robot class is no longer forced to implement the eat method. Also if we need another functionality for the robot like recharging we create another interface IRechargeble with a method recharge.

// interface segregation principle - good example
interface IWorker extends Feedable, Workable {
}

interface IWorkable {
	public void work();
}

interface IFeedable{
	public void eat();
}

class Worker implements IWorkable, IFeedable{
	public void work() {
		// ....working
	}

	public void eat() {
		//.... eating in launch break
	}
}

class Robot implements IWorkable{
	public void work() {
		// ....working
	}
}

class SuperWorker implements IWorkable, IFeedable{
	public void work() {
		//.... working much more
	}

	public void eat() {
		//.... eating in launch break
	}
}

class Manager {
	Workable worker;

	public void setWorker(Workable w) {
		worker=w;
	}

	public void manage() {
		worker.work();
	}
}

Conclusion

If the design is already done fat interfaces can be segregated using the Adapter pattern.

Like every principle Interface Segregation Principle is one principle which require additional time and effort spent to apply it during the design time and increase the complexity of code. But it produce a flexible design. If we are going to apply it more than is necessary it will result a code containing a lot of interfaces with single methods, so applying should be done based on experience and common sense in identifying the areas where extension of code are more likely to happens in the future.

Apache Tomcat Severe Error

SEVERE: Error configuring application listener of class com.sun.faces.config.ConfigureListener
java.lang.ClassNotFoundException: com.sun.faces.config.ConfigureListener

 

Some things to note:

  1. Notice the time stamp beside the exception, this may not be an existing problem. If it happened today, it will always be shown on server startup as tomcat only creates a new log when the date changes (ie new log every day).
  2. Also, when running netbeans, although you may have cleared the logs directory in Apache Tomcat, you will also need to clean them in the catalina base directory. Right click on Tomcat in netbeans and you will see where the catalina base is located. This will then allow you to navigate to the logs to delete them.

For me, this problem was solved by adding

jsf-api.jar
jsf-impl.jar

 

to the lib directory under WEB-INF directory of my project.

 

Create SSL Certificate for HTTPS localhost on Tomcat 7 Chrome and Internet Explorer

Getting SSL working with a self signed certificate for local development was poorly documented everywhere I looked. Step by Step guide

1.Create an alias in your hosts file.

To do this you need to know your ip address. Once you do, open

C:\Windows\System32\drivers\etc\hosts

and add the entry with your ip address. EG:

hosts entry

Now, assuming you are running apache tomcat, you should be able to navigate to

http://my.gleniris.com:8084

and see the same content as localhost:8084

2. Generate an entry in the keystore file for your alias.

The part in bold must match your hosts file entry, eg, my command was (note, I am using the keystore provided with my JDK):

keytool -genkey -alias my.gelniris.com -keyalg RSA -keystore “C:\Program Files\Java\jdk1.7.0_71\jre\lib\security\cacerts”

You are now asked to enter your name. enter your name as whatever value your alias is. In my case I entered the name as my.gleniris.com

3.Configure Apache Tomcat to Allow Https connections

Navigate to server.xml which is located in your tomcat’s conf directory. Find the section “Define a SSL HTTP/1.1 Connector on port 8443″ Add the below part to it. Do not uncomment anything, this should be all you need with the italics requiring you to add in your own values. Note: The part in bold is left out of most tutorials I found which caused me delays, hence this blogpost. Note that the default password for the cacerts file which came with the JDK is ‘changeit’

<Connector port=”8443″ protocol=”org.apache.coyote.http11.Http11Protocol” maxThreads=”150″ scheme=”https” secure=”true” SSLEnabled=”true” keystoreFile=”C:/Program Files/Java/jdk1.7.0_71/jre/lib/security/cacerts” keystorePass=”changeit” clientAuth=”false” keyAlias=”my.gleniris.com” sslProtocol=”TLS”/>

Restart Apache Tomcat. Note, if you are using Netbeans (like I am) you will also have to edit the server.xml in the Catalina base. You can find where catalina base is by clicking on the Tomcat server in Netbeans.

4.Copy the certificate which you have generated

Using Chrome, attempt to navigate to the secure section of your site. For me, it was (Note the HTTPS protocol):

https://my.gleniris.com:8443

Click on the broken red padlock in the address bar and Click the Details tab > Export. Save it as the defaut .cer file type. Save it with the same name as you alias. For me this file is named:

my.gleniris.com.cer

5.Create an entry in the trusted certificate publishers directory of your machine

Open a command window and type

certmgr.msc

Expand the ‘Trusted Publishers’ directory. Using the menu bar, select action >All Tasks >Import, and import the file you created in step 4. Repeat this process in the ‘Trusted Root Certification Authorities’ directory. Restart your machine.

 

 

Note, syntax for keytool -delete

 -alias keyAlias
 -keystore keystore-name
 -storepass password

What an enormous pain in the b****x this proved to be.

The Why: http://www.larryullman.com/2012/11/14/getting-an-ssl-certificatesetting-up-https/

My Git Commit Process – squash, rebase, commit

SQUASH ALL THE COMMITS TO YOUR FEATURE BRANCH INTO ONE COMMIT
git rebase -i HEAD~4 (where 4 is the amount of commits you wish to squash)
ONCE ALL COMMITS ARE SQUASHED INTO ONE, ENSURE TO ORIGINAL BRANCH IS UP TO DATE
git checkout master
git pull origin master
NOW THAT ORIGINAL IS UP TO DATE, REBASE
git checkout myFeatureBranch
git rebase – i master
DELETE REMOTE VERSION OF FEATURE BRANCH
git push origin :myFeatureBranch
PUSH TO REMOTE REPO
git push origin myFeatureBranch

Mock Testing

Image taken from http://zeroturnaround.com/rebellabs/how-to-mock-up-your-unit-test-environment-to-create-alternate-realities/

Mock Testing explained:

Concise Example of the EasyMock testing framework:

Reflection in Java

Six years of programming with Java and I’ve never had to use reflection. I don’t know if that a good or a bad thing but below is the best, most concise explanation of Java Reflection which I have found. The commentator confuses himself at times but the core message and logic comes across well. Only 11 or so minutes long.

Reflections on The Dublin Web Summit 2014

dublin-web-summit-2014

Dublin Web Summit 2014

Web Summit Overview:

-Purpose: Investment, networking

-Stage names: Main, Marketing, Enterprise, Machine, Builders, Sports, Food,

-Talks – Talks would be individual speakers, or a panel of experts being interviewed. Speakers did not get paid and for the most part they only mentioned their product / service in passing as opposed to using the 15 / 20 minute slot as a promotion.

-My week – I spent it attending different talks. I didn’t spend any time talking to startups.

-This Presentation: I will give a brief synopsis of 3 talks which I found interesting. I will talk about two subjects that came up over and over again and I will briefly speak about different statements and information which I came across throughout the week.

Disclaimer: There were stats thrown around without stating the source or reliability. I can only relay these stats, I cannot defend them

Big Data – What is it? – Where does it come from? – Internet of   Things –  Why is it useful?

IOT – is the interconnection of uniquely identifiable embedded computing devices within the existing Internet infrastructure. Basically, it is a reference to how almost everything is connected to the internet. For example, one talk detailed how his company assign an ip address to each pane of glass in a building and can control the opacity of the window depending on how much light is desired in the room. This removes the need for a powerful A/C system as well as the hassle of blinds. The owner of the Galway Hooker brewery can controll his whole brewing process from his phone. Anyway, I digress, basically, so many devices are connected and interconnected and this is the IOT.

All of these devices, generate extremely large data sets, even when apparently idle. The amount of data being generated is known as Big Data. Big data, when analyzed can reveal patterns, trends and associations which can help with strategic decisions for business. A simplified example, take a PlayStation game, typically a shop would buy x amount of units based on how popular they felt the game would be, on how much demand they thought would exist. When the game sold out, they would order more units. While waiting to get re-stocked, customers are buying the game elsewhere.

With Big Data on their side, our game shop can now analyze web browsing patterns, social media trends, the amount of adverts appearing online, seasonal trends, local demographic information and shopping and more to make a better estimate on how many units they should buy and in what stores.

Data Scientists, at one talk I was at, a demonstration was given on how 5 differing and opposing conclusions were drawn from analyzing the same data set. So for our example, this would mean that by looking at the data in different ways, our game store would say that they were most likely to sell the biggest number of games in Leitrim, when lthey looked at the data a different way, they would conclude that they were more likely to sell more units in Cork City. Its one thing having the data but its another thing entirely to get value from it. This is were Data Scientists come in.

Data Scientists are people usually with statistical and mathematical talent whose task it is to perform large scale statistical research, analysis and modeling of almost everything. These are the people who add value to your big data. In time, software will probably be used to perform such modeling, but until then, Data Scientists are in high demand.

Data Scientists – Get the right people as opposed to may people. The most powerful data is when human gut instinct is removed.

Big Data – http://www.ibmbigdatahub.com/presentation/big-data-retail-examples-action

Featured Talk – How to be a Hacker – Pablo Holman – Main Stage

“Hackers have he genius minds that will try & do everything but follow directions”

Pablo once hacked into the FBI systems. He said that Hackers have a different way of thinking, that they don’t read the instructions before using a device. They do their best to break the device.

Pablo began by telling stories of famous hackers, one such one was Barnaby Jack who bought a couple of ATM’s on eBay, studied them and then during a presentation at a Black Hat conference in 2010,  had the machines dispense cash without effecting the account balance.

Pablo explained how these days, every new product is being shipped with a chip in order to future proof it. Basically in his opinion, everything is turning into a hackable PC.

Pablo explained that if you live in America, no matter how bad you have things, you are still in the top 14% of the world for quality of living. Pablo explained how 250,000 children die from malaria each year. Pablo said that while everyone is designing apps to solve “problems” like how to split a bar tab with your friends, Pablo is working for Intellectual Ventures Laboratory which has partnered with Bill Gates to form Global Good.

This partnership has developed products such as the vaccination cooler for malaria vaccinations. 6 months in the sun as opposed to half of the vaccinations in the Styrofoam cooler going bad within 4 hours.

His talk then ended and the point seemed to be how great he is as opposed to how to be a hacker.

I just thought this talk was interesting due to the top 14% stat and the way he belittled everyone there with a stall by saying they are not solving “real problems”.

Featured Talk – Startup Tech Talk – Paul Muller (Adjust)

Talk Main Focus – Avoid the Cloud

Reason – Cost $5.6 million dollar savings over 3 years. Paul mentioned that from his experience talking with CTO’s, Cloud Services are typically the largest cost to the CTO’s company. For Adjust (Paul’s company), their hardware is 5% of their operating cost.

Reason – Avoid optimizing your system so you don’t pay cloud services as much, instead optimize it so that it is better for end users / customers.

Reason – Once people go to the cloud, they tend not to come off it as they see the migration as too big an ordeal.

Reason – If something happens and your site / service goes down, you will have your own team of devops working on it rather than logging calls or tickets to a cloud service provider. With savings like $5.6 million over three years, you can well afford your own devops team.

Reason – You can also have this devops build customized safeguards for your service that can bolster your security. Paul told a story about ‘Code Spaces’ who had their Amazon Web Services management console hacked. When the hacker didn’t get his ransom, he had all the controls at his finger tips to wipe the system and all images and backups. all of its svn repositories—backups and snapshots—were deleted. All EBS volumes containing database files were also deleted. Code Spaces folded as it would not have been viable for them to build up from scratch again.  Paul’s team have the ability to disable such functionality.

Scott Johnston (Google Drive) – When questioned at his presentation about security concerns, Scott used the analagy that having your own services and hardware is like storing all your cash under your bed instead of using a bank. He said your hard drives can fry and that could finish you. He said how Google have 450 staff dedicated to security and there is no way any one could have as string a defence as they provide.

As an observation on the point of never getting off the cloud, Google granted every startup at the summit a $10,000 voucher for their cloud services. Amazon did the same but to the value of $1,000.

Featured Talk – The Death of the Password – Eric Schalit (Dashlane)

“There is no doubt that over time, people are going to rely less and less on passwords. “ – Bill Gates 2004

So why has Bill Gates prediction not come true yet?

Emmanuel started talking about how things become a de facto standard. For example, we all know that the QWERTY keyboard layout was devised so that keys commonly used together wouldn’t jam. However, the layout was subsequently modified by Typewriter manufactures Remington, so that their sales staff could type the word ‘Typewriter’ all from the top line of the keyboard. The rest of us stuck with this as its just become a norm.

The same has happened with passwords and this is proving hard to kill. Passwords meet 3 vital criteria, and thus, any replacement would also need to meet these criteria. The 3 things are:

1.Free / Cheap – nobody owns the password system or idea.

2.Shareable – Passwords started out so that Romans could restrict access to certain areas. If you wanted to get in, you would need to tell the sentry the password. You would have attained the password after somebody shared it with you.

3.Preserve anonymity – For example, retina scanning or fingerprint scanning does not allow you to stay anonymous.

To replace the de facto standard, a new standard would need all the existing benefits as well as some new ones. It would also need to be easy to switch to. Significant time would also need to pass to allow mass adoption.

As stated, hardware solutions don’t meet the above 3 criteria. There have been attempts to solve the issue with software, for example, Facebook connect. But not everyone thinks this is a good idea, for example, Google. Google have their own alternative, as do Twitter.

The issue with passwords is human. Humans want security and convenience. Enter tDashlane, a one stop app that you create an account for and it then has access to all your online logins. It makes it very easy to generate passwords and have them automatically change every so often. You don’t need to worry about what they are as you will be signed into Dashlane on your machine / mobile device and it looks after signing in to we sites for you.

So I tried out Dashlane and I was uncomfortable with one site having access to all my online accounts. I wouldn’t mind if it managed passwords for sites I don’t care about – eg Oracle where I need to login to every so often to get an old JVM. But I don’t like how it also stores my Gmail password where I have personal information. I am not sure I can trust one company with all of my security for all websites.

Therefore my conclusion is that passwords are very much alive and I will go on the record as saying Bill Gates is wrong.

Follow

Get every new post delivered to your Inbox.