Sunday, December 25, 2016

Java Coding- Inheritance Rules

Inheritance should be used wisely. Purpose of inheritance is to have a parent-child (Superclass-Subclass) relationship between objects. In result, it facilitates code reuse. But don't use Inheritance just for code reuse. 
Following two principles help you to use inheritance wisely-

is A? Principle

It helps you decide if you should inherit a class from other i.e. is the new class should or should not extends a super class. So before you decide to extend ask the below question-

Is a "Circle" a "Shape"?
Is a "Triangle" a "Circle"?
Is a "Cat" an "Animal"?

If answer is "Yes" then you should extend the super class.
If answer is "No" then you should NOT extend the super class.

has A? Principle

It helps you decide if a class should have a field or is it relevant to add a new field to a class? Ask below question-

Has a "Circle" has a "radius"?
Has a "Triangle" has a "radius"?

If answer is yes then you should have the field in the class otherwise NOT.


Inheritance facilitates one more Advantage that is in form of another principle-

Liskov Substitution Principle

Any child type of a parent type should be able to stand in for that parent without things blowing up. Reverse of this is not true i.e. you can not replace a child type object with it's parent type object. If you try do so i.e. by means of type casting the parent type object, there is a risk that it may blow up things.

Consider an example where "Animal" <---- "Dog" i.e. Dog extends Animal

A. Suppose a  method which accepts Animal - makeNoise(Animal a). You can always call this method with child type object i.e. makeNoise(d) where d is an object of class Dog.

B. Take another method which accepts Dog - sniffDanger(Dog d). Now you can NOT call this method with parent type object i.e. sniffDanger(a) where a is an object of class Animal. Even if you try to typecast a, something like this: sniffDanger((Dog) a), there is high possibility that it may blow up things, so not a safe approach.

Thursday, December 8, 2016

Enable debugging in AEM running in Vagrant/ VM

If you are running AEM instance in Virtual Box, in order to configure the AEM debug settings follow the below steps-

1. Open the debug port in VM Box. Go to VM Manager, select the AEM instance -> Settings -> Network -> Port Forwarding


Add the debug port 8000 to whitelist it as shown in below image-




2. Go to following location where you have installed kitchen and Run the following command-   bundle exec kitchen login aem-author-c 

OR

Alternatively go to VirtualBox and enter into the aem box by starting it in general mode as shown below-


The box console looks like this-


When asked for username/ password, enter vagrant/vagrant (Default box password)

3. If you are using vagrant then edit your kitchen environment script to change the debug port-


and you are Done!

But, if you are not using Vagrant then follow the next steps.

4. Run sudo su and Go to /opt/aem/author/crx-quickstart/bin

5. Run vi quickstart 

6. Change following line- CQ_JVM_OPTS='-server -Xmx2048m -XX:MaxPermSize=512M' 
to add following in the last
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8000,suspend=n

7. Run vi start and Repeat step 5 for this file too.

8. Stop and Start the AEM instance.

CDN | Clearing Cloudflare cache

In order to clear Cloudflare cache automatically via code, follow below steps: 1. Develop Custom TransportHandler Develop a custom Trans...